Search code examples
sqlsql-servert-sqljoin

How to trace out values from three different tables SQL JOIN?


I have 3 tables like below

table_1

securityno name    price
1           a11    12.12
2           z11    44.4

table_2

name      identifier Mainprice
a11_bond  NO34         11
z22_bond  NO98         22

table_3
securityno name    identifier 
1           a11    NO34         
2           z11    NO98         

I want to check whether table_1 is having correct price or not as per table_2. I just want to display output table_1 data and Mainprice column from table_2.

securityno name    price Mainprice
1           a11    12.12 11
2           z11    44.4  22

I was trying like

select * from table_1 left join table_2 on what about table_3?

failed to use 3 tables.


Solution

  • Try:

    SELECT  
        t1.*,  
        t2.Mainprice  
    FROM table_1 AS t1  
    LEFT JOIN table_3 AS t3  
       ON t1.securityno = t3.securityno AND t1.name = t3.name  
    INNER JOIN table_2 AS t2  
       ON t2.identifier = t3.identifier