|
|
|
| Здравствуйте. Есть такой запрос:
SELECT
mt_motodeclaration.author_id AS author_id,
mt_motoproducer.prod_name AS prod_name,
mt_motoproducer.alt_name AS alt_prod_name,
mt_motomodel.alt_name AS alt_model_name,
mt_motomodel.model_name AS model_name,
mt_motomodification.modif_name AS modif_1_name,
mt_motodeclaration.modif_name AS modif_2_name
FROM
mt_motodeclaration,
mt_motoproducer,
mt_motomodification,
mt_motomodel
WHERE
mt_motomodification.id = mt_motodeclaration.modif_id AND
mt_motoproducer.id = mt_motodeclaration.prod AND
mt_motomodel.id = mt_motodeclaration.model_id AND
mt_motodeclaration.id = '$decl_id' AND
mt_motodeclaration.status = '0'
|
Однако, случается, что в таблице mt_motodeclaration поле modif_id равно нулю и в таблице mt_motomodification нет строки с id равным 0. Соответственно, когда так случается, запрос не возвращает ничего. Подскажите, как написать запрос, чтоб в случае, если modif_id равно нулю, запрос возвращал значения всех остальных полей, которые указаны в запросе? | |
|
|
|
|
|
|
|
для: Zdraff
(24.10.2009 в 13:04)
| | есть такая операция JOIN
tbl2 JOIN tbl1 ON tbl2.id = tbl1.tbl2_id
|
Которую следует применять вместо всех этих Ваших mt_motodeclaration, mt_motoproducer WHERE mt_motoproducer.id = mt_motodeclaration.prod
И есть её разновидность LEFT JOIN
tbl2 LEFT JOIN tbl1 ON tbl2.id = tbl1.tbl2_id
|
Так вот. Вам нужна цепочка LEFT JOIN | |
|
|
|
|
|
|
|
для: Trianon
(24.10.2009 в 13:14)
| | Спасибо. А скажите, как в этой ситуации использовать псевдонимы полей? Т.е. есть запрос:
SELECT
mt_motodeclaration.author_id AS author_id
FROM
mt_motodeclaration
LEFT JOIN
mt_motoproducer ON mt_motodeclaration.prod = mt_motoproducer.id
WHERE
mt_motodeclaration.status = '0'
|
Как показать отфетченный prod ? | |
|
|
|
|
|
|
|
для: Zdraff
(24.10.2009 в 13:33)
| | псевдонимы полей можно использовать совершенно спокойно, но всё же надежнее их делать отличными от имен полей и таблиц, вовлеченных в запрос.
Дабы не вносить путаницу. | |
|
|
|
|
|
|
|
для: Trianon
(24.10.2009 в 13:53)
| | Спасибо, Trianon, Вы очень помогли. | |
|
|
|