Answer the question
In order to leave comments, you need to log in
SELECT and JOIN when there is no value in the table?
Hello. How to select data from two tables with a join, but from the first table all the data, and from the second by id from the first: if such a record is found, then it, and if not, then 0. It seems that you can use IF or IFNULL, but something not so it turns out
UPD: Table example on sqlfiddle: sqlfiddle.com/#!9/f30a13/10
select descr, above, if(count(discount) > 0, discount, 0) from time_discounts left join categories on time_discounts.cat_idx = categories.cat_idx where categories.tar_id = 87 and time_discounts.tar_id = 87;
Answer the question
In order to leave comments, you need to log in
LEFT JOIN selects all records from the left table and their corresponding ones from the right table. So either use RIGHT JOIN or change the join order of the tables.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question