2012-02-10 85 views
2

的SQL語句下面用來工作,幾年前,現在不和我得到這個錯誤信息:1054 - 未知列「XXX」中「的條款」

1054 - 未知列' Promotion_Card_details.Card_id對條款‘

」的’這是我的SQL語句:

SELECT DISTINCT Promotion_Card_details.Card_id, 
     Promotion_Merchant_details.Merchant_id, 
     Promotion_details.Promotion_id, 
     Issuer_details.IssuerName, 
     Card_details.CCType, 
     PaymentType_details.PaymentTypeName, 
     Merchant_details.MerchantName, 
     PromotionText, 
     PromotionEndDate 
FROM Promotion_details 
INNER JOIN Card_details ON (Card_details.Card_id=Promotion_Card_details.Card_id) 
INNER JOIN Issuer_details ON (Issuer_details.Issuer_id=Card_details.Issuer_id) 
INNER JOIN PaymentType_details ON (PaymentType_details.PaymentType_id=Card_details.PaymentType_id) 
INNER JOIN Merchant_details ON (Merchant_details.Merchant_id=Promotion_Merchant_details.Merchant_id) 
INNER JOIN Promotion_Merchant_details ON (Promotion_Merchant_details.Promotion_id=Promotion_details.Promotion_id) 
INNER JOIN Promotion_Card_details ON (Promotion_details.Promotion_id=Promotion_Card_details.Promotion_id) 
WHERE ((Promotion_details.Promotion_id = '13' OR Promotion_details.Promotion_id = '14' 
      OR 
     Promotion_details.Promotion_id = '15' OR Promotion_details.Promotion_id = '16' OR 
     Promotion_details.Promotion_id = '17' OR Promotion_details.Promotion_id = '18' OR 
     Promotion_details.Promotion_id = '19' OR Promotion_details.Promotion_id = '20' OR 
     Promotion_details.Promotion_id = '21' OR Promotion_details.Promotion_id = '22' OR 
     Promotion_details.Promotion_id = '23' OR Promotion_details.Promotion_id = '24' OR 
     Promotion_details.Promotion_id = '25' OR Promotion_details.Promotion_id = '361' 
     OR Promotion_details.Promotion_id = '364' OR Promotion_details.Promotion_id = '382')) 

任何幫助將不勝感激,因爲我已經試過周圍的某些部分括號,但它仍然有不修復。

+0

你可以發佈你的表的DESC? – 2012-02-10 15:50:59

+0

有人重新命名或刪除了「Promotion_Card_details」表中的字段嗎? – Crontab 2012-02-10 15:54:25

+1

此外,它會使查詢變得更具可讀性,可以將您的'WHERE'子句更改爲使用'IN'而不是'OR'的長列表。 – Crontab 2012-02-10 15:55:10

回答

8

JOIN s是左關聯的,所以給定的ON子句只能引用已經提到的表。因此,這些重新排序:

FROM Promotion_details 
    INNER JOIN Card_details ON (Card_details.Card_id=Promotion_Card_details.Card_id) 
    INNER JOIN Issuer_details ON (Issuer_details.Issuer_id=Card_details.Issuer_id) 
    INNER JOIN PaymentType_details ON (PaymentType_details.PaymentType_id=Card_details.PaymentType_id) 
    INNER JOIN Merchant_details ON (Merchant_details.Merchant_id=Promotion_Merchant_details.Merchant_id) 
    INNER JOIN Promotion_Merchant_details ON (Promotion_Merchant_details.Promotion_id=Promotion_details.Promotion_id) 
    INNER JOIN Promotion_Card_details ON (Promotion_details.Promotion_id=Promotion_Card_details.Promotion_id) 

這個順序:

FROM Promotion_details 
    INNER JOIN Promotion_Card_details ON (Promotion_details.Promotion_id=Promotion_Card_details.Promotion_id) 
    INNER JOIN Card_details ON (Card_details.Card_id=Promotion_Card_details.Card_id) 
    INNER JOIN Issuer_details ON (Issuer_details.Issuer_id=Card_details.Issuer_id) 
    INNER JOIN PaymentType_details ON (PaymentType_details.PaymentType_id=Card_details.PaymentType_id) 
    INNER JOIN Promotion_Merchant_details ON (Promotion_Merchant_details.Promotion_id=Promotion_details.Promotion_id) 
    INNER JOIN Merchant_details ON (Merchant_details.Merchant_id=Promotion_Merchant_details.Merchant_id) 
+0

謝謝我試過了。無論我如何重新排列所有INNER JOIN語句,第一個INNER JOIN將始終給出錯誤#1054 - 'on子句'中的未知列'XXX' – James 2012-02-10 15:58:33

+0

@James:啊,我沒注意到還有其他錯誤的依賴關係。但它仍然是同樣的問題。我已經更新了我的答案。 – ruakh 2012-02-10 15:59:33

+0

對不起,我發現上面我的SQL語句中有一個輸入錯誤,我認爲我放在那裏替換的錯誤現在是正確的,它會拋出'on子句'#1054 - 未知列'Promotion_Card_details.Card_id' – James 2012-02-10 16:05:48