Click to See Complete Forum and Search --> : Need a MySQL query (this one's easy for advanced people)


multimediocrity
08-06-2009, 12:12 AM
I need a query that can display only the rows which have the correct 'color' variable in a corresponding table.

If I have these tables:


Table 1:

id num color
1 1 yes
2 2 yes
3 3 yes
4 4 no
5 5 no
6 6 yes



Table 2:

id num answer
1 1 345
2 2 456
3 3 567
4 4 875
5 5 342
6 6 801


I want to display from Table 2 all data in the 'answer' field UNLESS the corresponding 'num' in Table 1 has 'yes' in the 'color' field.

So I want to display only:
875
342

Thanks!

stuffonmymate
08-06-2009, 05:31 AM
Assuming that the table2.num corresponds to table1.id try the following

SELECT t2.answer FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.num WHERE t1.color = 'no';