i have two tables in my db,
Table A
a_id,email,col2,col3
and
Table B
b_id,email
what i want to do is formulate a single query that can chooses the column names per boolean.
i mean i have a set of a_id's that i want to fetch data for from Table A,
for a particular a_id, if the email exists in Table B, fetch col2, else fetch col 3
i want to formulate a query开发者_Python百科 something like below, but in a efficient way :)
select
if (email exists table B)
col 2,
else
col 3
as title
from Table A
where a_ids in (1,2,3,4,5,6,7)
Try ths one -
SELECT a.a_id, IF(b_id IS NULL, col3, col2) FROM tablea a
LEFT JOIN tableb b ON a.a_id = b.b_id
WHERE a.a_id IN (1,2,3,4,5,6,7);
精彩评论