0
votes

I have three tables:

Table1: COL1 COL2
Table2: COL2 COL3 COL5
Table3: COL3 COL4

And I want to select the COL1 from Table1, COL5 from TABLE2 and COL4 from Table3

It likes two join table, but when I use the following query it doesn't work, COL5 is blank.

SELECT Table1.COL1, Table2.COL5, Table3.COL4 

FROM table3
INNER JOIN table2 ON table3.col3 = table2.col3
INNER JOIN table1 ON table1.col2 = table2.col2

Please help.

PS I copied a previous similar example but different output.

1
provide your sample data and expected output in table format - Fahmi
Your query looks fine, only issue can be data. It will be good if you share sample data. - PSK
If you are getting rows, but COL5 is blank, then COL5 simply contains null or space only. It can also be a display issue, e.g. the value in COL5 starts with a line feed character and your tool doesn't show the following text. - Thorsten Kettner

1 Answers

0
votes

It seems that there isn't relational data between table 3 and table 2

SELECT Table1.COL1, Table2.COL5, Table3.COL4

table3.col3, table2.col3 --if this comes up as null then Table2.COL5 would be null
table1.col2, table2.col2 --if this comes up as null then Table2.COL5 would also be null

FROM table3
LEFT OUTER JOIN table2 ON table3.col3 = table2.col3
LEFT OUTER JOIN table1 ON table1.col2 = table2.col2

With an INNER JOIN all join conditions will need to be true in order to return results.

Consider changing your INNER JOIN order or use LEFT OUTER JOIN.