如何从三个不同的表中查找值sql JOIN?

时间:2013-04-09 18:24:40

标签: sql sql-server tsql join

我有3张如下表

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         

我想根据table_1检查table_2是否具有正确的价格 我只想显示table_1

中的输出Mainprice数据和table_2
securityno name    price Mainprice
1           a11    12.12 11
2           z11    44.4  22

我当时想要

  

从table_1中选择*左连接table_2关于table_3的内容?

未能使用3张表。

请帮忙。

2 个答案:

答案 0 :(得分:2)

尝试:

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  

答案 1 :(得分:2)

简单使用INNER JOIN

SELECT T1.*,
       T2.mainprice
FROM TABLE1 T1
INNER JOIN Table3 T3 ON T3.securityno = T1.securityno
INNER JOIN Table2 T2 ON T2.identifier = T3.identifier

DEMO