Click to See Complete Forum and Search --> : mySQL help please


ccurle
01-16-2007, 10:57 PM
I have to query 3 tables from a database. I am just using abbreviated table description since the tables are very large

Table A
ID_A
Name_A

Table B
ID_B
Name_B

Table C
ID_C
ShowName
ID_A
ID_B

Now, I need to query all 3 and have Table A and B show the name

Table C is the main table that I need. Table A and B are reference tables.

If I do a select TableA.Name_A, TableB.Name_B, TableC.ShowName WHERE TableC.ID_A = TableA.ID_A and TableC.ID_C = TableB.ID_B

It only shows the records in Table C that have a reference to Table A and B. If there is nothing in those fields, then it doesn't show. How can I query C to show all the records and the ID_A and ID_B to show their names if there is data, if not, just leave blank?

aussie girl
01-16-2007, 11:10 PM
use FROM tableA, tableB,tableC in your query between SELECT and WHERE

ccurle
01-17-2007, 12:23 AM
I am sorry, I do have that, I forgot to post that.

What happens is if there is no ID_A or ID_B in Table C, then it skips that record for Table C and I need the record to show even if there is no ID_A or ID_B in Table C.

aussie girl
01-17-2007, 12:52 AM
you could do some research on inner and left joins or you could use after your where clause
AND tableC.ID_A IS NULL OR tableC.ID_B IS NULL it's not pretty but it should work

chazzy
01-17-2007, 06:52 AM
you need to use a left join. something like this probably


SELECT table1.* FROM table1
LEFT JOIN table2 ON table1.id=table2.id
WHERE table2.id IS NULL;

ccurle
01-17-2007, 09:19 AM
you need to use a left join. something like this probably


SELECT table1.* FROM table1
LEFT JOIN table2 ON table1.id=table2.id
WHERE table2.id IS NULL;


Will this work if there is data in table2.id?

chazzy
01-17-2007, 02:20 PM
try it without the where clause.

ccurle
01-17-2007, 02:49 PM
I used a left outer join and it worked perfectly.