Crosswords2 mins ago
SQL query advice
4 Answers
Hi,
I asked for help from you guys with an SQL query yesterday and someone replied and helped really helped me, I was given the following query
select T1.src_prod_ofr, T1. source_code, T2.cit_code
FROM table1 T1
LEFT JOIN table2 T2
ON T1.src_prod_ofr = T2.src_prod_ofr
Question: If I wanted to add another table to the query how would I go about doing this? How would I write the query?
I asked for help from you guys with an SQL query yesterday and someone replied and helped really helped me, I was given the following query
select T1.src_prod_ofr, T1. source_code, T2.cit_code
FROM table1 T1
LEFT JOIN table2 T2
ON T1.src_prod_ofr = T2.src_prod_ofr
Question: If I wanted to add another table to the query how would I go about doing this? How would I write the query?
Answers
Best Answer
No best answer has yet been selected by Chris100682. Once a best answer has been selected, it will be shown here.
For more on marking an answer as the "Best Answer", please visit our FAQ.Add it as another JOIN. Say Table3 is the name of the 3rd table. For instance :
SELECT T1.src_prod_ofr, T1. source_code, T2.cit_code
FROM Table1 T1
LEFT JOIN Table2 T2
ON T1.src_prod_ofr = T2.src_prod_ofr
JOIN Table3 T3
ON T2.cit_code = T3.cit_code
This would only return rows where Table3's cit_code is the same as Table2's cit_code. To return all records, do another LEFT JOIN instead of the normal JOIN.
SELECT T1.src_prod_ofr, T1. source_code, T2.cit_code
FROM Table1 T1
LEFT JOIN Table2 T2
ON T1.src_prod_ofr = T2.src_prod_ofr
JOIN Table3 T3
ON T2.cit_code = T3.cit_code
This would only return rows where Table3's cit_code is the same as Table2's cit_code. To return all records, do another LEFT JOIN instead of the normal JOIN.