1

I am not too familiar with SQL, but I need to select from 3 tables.

I can go:

SELECT * FROM tbl1, tbl2, tbl3 WHERE ID=3

but there is a chance that tbl3 is does not have any rows with ID 3, and chances are that tbl1 has a few.

tbl2 should have only one row.

I would still like to get the rows in the other tables.

How can I accomplish this ?

Thanks up ahead!

1
  • 2
    what's the table structure? is there any relationship between the tables? Commented Sep 6, 2011 at 23:12

2 Answers 2

1

How do the three tables relate? The condition 'WHERE ID=3' is a little ambiguous - does ID exist in all three tables, and you want all records from each table where ID = 3?

The bottom line is you need to use a LEFT JOIN to join the tables together, but it's hard to give an example without knowing how the tables relate.

Making some assumptions:

SELECT *
FROM tbl1
LEFT JOIN tbl2 ON tbl2.tbl1_id = tbl1.id
LEFT JOIN tbl3 ON tbl3.tbl1_id = tbl1.id
WHERE tbl1.id = 3;
Sign up to request clarification or add additional context in comments.

1 Comment

well, all three tables have a row named b_ID, table1 might 0 or more instances of b_ID=3 for example, whereas table3 might not have any at all. From what I read here: w3schools.com/sql/sql_join_left.asp this is the answer, thanks
0

Do seperate queries of each table, then do a row count of those queries, then with the results
you can do if elseif else statements to do wat you want to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.