2013-04-09 32 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

相關問題