2013-04-09 59 views
0

我有3个表像下面如何从三个不同的表中追踪数据sql JOIN?

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数据和从table_2

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

我试图像

select * from table_1 left join table_2关于table_3呢?

未能使用3个表格。

请大家帮忙。

回答

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 
+0

均出自值'table_1'因此需要使用'LEFT JOIN' 若有't2.identifier = t3.identifier'' identifier'不匹配'table_1'中的公司名称也需要显示? – Neo 2013-04-17 16:26:01

+0

尝试更改的代码,并让我知道它是否有效。如果您可以为您提到的问题添加样本数据将有所帮助 – Ram 2013-04-17 18:24:36

+0

如果代码有效,您可以将其标记为接受的答案? – Ram 2013-05-13 19:49:09

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

+0

FROM'table_1'中的所有值都需要使用'LEFT JOIN' 如果任何't2.identifier = t3.identifier''标识符'不匹配'table_1'中的公司名称,还需要显示? – Neo 2013-04-17 16:26:31