我有表所示:SQL查詢選擇
表:因素
FactorID SellerID ContractorID
---------------------------------
1 -1 2
2 1 -1
3 -1 1
4 2 -1
表:賣家
SellerID SellerName
---------------------
1 SellerA
2 SellerB
3 SellerC
4 SellerD
表承辦單位:
ContractorID ContractorName
-------------------------------
1 W
2 X
3 Y
4 Z
我想使查詢返回一些像這樣的:
FactorID SellerID ContractorID ContractorName SellerName
------------------------------------------------------------------
1 -1 2 X NULL
2 1 -1 NULL SellerA
3 -1 1 W NULL
4 2 -1 NULL SellerB
這是我的查詢,但無法正常工作:
SELECT Factos.* ,
Seller.sellerName AS sellerName ,
Contractor.Contractor AS contractorName
FROM Fctors ,
Seller ,
Contractor
WHERE Seller.SellerID = CASE WHEN Factors.ContrarID = -1 THEN Factors.SellerID
ELSE ''
END
AND Contractor.ContractorID = CASE WHEN Factors.SellerID = -1 THEN Factors.ContractorID
ELSE ''
END
您是否收到任何錯誤?你正在選擇'從Fctors'而不是'from Factors' - 你是如何在查詢中做到這一點的? – Alexander