2015-01-11 74 views
1

我對SQL非常陌生,並嘗試使用SQL Server進行多個連接。SQL Multiple Joins返回錯誤'關鍵字'JOIN'附近語法不正確'。'

鑑於

c_slpn

"ID Name Age Salary" 
'1 Abe  61 140000' 
'2 Bob  34 44000' 
'5 Chris 34 40000' 
'7 Dan  41 52000' 
'8 Ken  57 115000' 
'11 Joe  38 38000' 

c_cust

"ID Name City Industry Type" 
'4 Comp1 pleasant J' 
'6 Comp2 oaktown  J' 
'7 Comp3 jackson  B' 
'9 Comp4 Jackson  B' 

c_ordr

"Number order_date cust_id salesperson_id Amount" 
'10  8/2/96  4  2  540' 
'20  1/30/99  4  8  1800' 
'30  7/14/95  9  1  460' 
'40  1/29/98  7  2  2400' 
'50  2/3/98  6  7  600' 
'60  3/2/98  6  7  720' 
'70  5/6/98  9  7  150' 

我應該找到「列出銷售人員有一個訂單與‘器Comp1’,但下面的腳本拋出一個錯誤:

Line 6 'Incorrect syntax near the keyword 'JOIN'.

查詢:

SELECT * 
FROM c_cust 
JOIN C_ordr ON C_ordr.cust_id= c_cust.ID 
WHERE NAME = 'Comp1' 
JOIN c_slpn ON c_slpn.id=c_ordr.cust_id 

請幫我......,我卡住了: -P [這是我的第一個問題,我失去的東西]

+1

您正在使用哪兩種_mutually exclusive RDBMS_? – potashin

回答

2

WHERE條款去JOIN後:

SELECT * 
FROM c_cust 
JOIN C_ordr ON C_ordr.cust_id = c_cust.ID 
JOIN c_slpn ON c_slpn.id = c_ordr.salesperson_id 
WHERE c_cust.NAME = 'Comp1' 

順便說一下,你也可以在這樣的條款中做到,如C_ordr.cust_id = c_cust.ID AND c_cust.NAME = 'Comp1'

+0

感謝您的答覆@notulysses但使用查詢給了我空白輸出.. 'SELECT *' 'FROM c_cust' '加入C_ordr ON C_ordr.cust_id = c_cust.ID' 「JOIN ON c_slpn.id c_slpn \t = c_ordr.cust_id' 'WHERE c_cust.NAME ='Comp1''仍然困惑...;( – ajufsd

+0

您確定有條件匹配的記錄嗎? – potashin

+0

是的,c_cust.Name有Comp1有c_cust.id 4匹配cust_id.cust_id。AND cust_id.salesperson_id match c_slpn.ID。(@IsabelHM感謝編輯) – ajufsd

0

不確定它是否只是拼寫錯誤,但表名不一致。

SELECT * 
FROM c_cust 
JOIN c_ordr 
ON c_ordr.cust_id= c_cust.ID 
JOIN c_slpn 
ON c_slpn.id=c_ordr.cust_id 
WHERE c_cust.Name= 'Comp1' 
+0

立即更正查詢。 'JOIN c_slpn ON c_slpn.id = c_ordr.salesperson_id'。我無意中鍵入了c_ordr的列名:p謝謝您查看我的查詢:D – ajufsd

相關問題