2

The SQL statement below used to work a few years ago, now it doesn't and I get this error message:

1054 - Unknown column 'Promotion_Card_details.Card_id' in 'on clause'

This is my SQL statement:

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') )

Any help would be greatly appreciated as I have tried parentheses around certain parts, but it still hasn't fixed it.

5
  • Could you post a DESC of your table? Commented Feb 10, 2012 at 15:50
  • Did someone rename or delete the field in the Promotion_Card_details table?
    – WWW
    Commented Feb 10, 2012 at 15:54
  • 1
    Also, it'd make the query a bit more readable to change your WHERE clause to use IN instead of a long list of OR's.
    – WWW
    Commented Feb 10, 2012 at 15:55
  • Promotion_Card_details is a table with a primary key, and two foreign keys: promotion_id from the promotion_details table and card_id frmo the card_details table. Similarly, the Promotion_Merchant_details table has a primary key, and two foreign keys, etc.
    – James
    Commented Feb 10, 2012 at 16:01
  • Hi Alex K, my SQL statement is being generated by Perl dynamically, so although its all Promotion_details.Promotion_id in the example above, it can be mixed with Merchant_details.Merchant_id and other constraints in other examples.
    – James
    Commented Feb 10, 2012 at 16:04

1 Answer 1

11

JOINs are left-associative, so a given ON clause can only refer to tables that have already been mentioned. So, reorder these:

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)

into this order:

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) 
5
  • Thanks I tried it. It doesn't matter how I re-order all my INNER JOIN statements, the first INNER JOIN will always give the error #1054 - Unknown column 'XXX' in 'on clause'
    – James
    Commented Feb 10, 2012 at 15:58
  • @James: Ah, I hadn't noticed that there are other mis-ordered dependencies. But it's still the same problem. I've updated my answer now.
    – ruakh
    Commented Feb 10, 2012 at 15:59
  • Yeh sorry I noticed a typo in my SQL statement above, and I think the one I put up there to replace, is correct now, which throws the #1054 - Unknown column 'Promotion_Card_details.Card_id' in 'on clause'
    – James
    Commented Feb 10, 2012 at 16:05
  • Wow, that's fantastic ruakh. It worked! I can't believe it was about re-ordering the INNER JOIN statements.
    – James
    Commented Feb 10, 2012 at 16:09
  • thanks for that: "a given ON clause can only refer to tables that have already been mentioned." my query looked perfectly fine but I did not know that! Commented Mar 5, 2015 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.