2013-02-24 54 views
0

上的未知列我有兩個查詢運行在同一個表上,除了第二個失敗並添加了一個左連接。左連接失敗,在條款

成功運行

SELECT * 
FROM paypal_ipn_orders 
LEFT join itemkey on paypal_ipn_orders.item_number = itemkey.item_id 
WHERE packing_slip_printed = 0 
AND LOWER(payment_status) = 'completed' 
AND address_name <> '' 
order by itemkey.sort_id; 

然而,這第二個查詢用「‘關於條款’在未知列‘paypal_ipn_orders.item_number’」失敗,但它運行良好與開出左側加入。 Paypal_ipn_orders表包含item_number字段。

SELECT * 
FROM paypal_ipn_orders as table1, 
(SELECT payer_email, COUNT(*) as count FROM paypal_ipn_orders GROUP BY payer_email ORDER BY count DESC) as table2 
LEFT join itemkey on table1.item_number = itemkey.item_id 
WHERE table1.payer_email = table2.payer_email 
and packing_slip_printed = 0 
AND LOWER(payment_status) = 'completed' 
AND address_name <> '' 
ORDER BY count DESC, auction_multi_item, item_number; 

表1後的逗號是必需的,因爲(SELECT ...是FROM子句的一部分

一個有趣的注意,我改變的順序FROM子句:。

SELECT * 
FROM (SELECT payer_email, COUNT(*) as count FROM paypal_ipn_orders GROUP BY payer_email ORDER BY count DESC) as table1, 
paypal_ipn_orders as table12 
LEFT join itemkey on table2.item_number = itemkey.item_id 
WHERE table2.payer_email = table1.payer_email 
and packing_slip_printed = 0 
AND LOWER(payment_status) = 'completed' 
AND address_name <> '' 
ORDER BY count DESC, auction_multi_item, item_number; 

現在的錯誤信息是:'where clause'中的未知列'table2.payer_email'。但是這將不會在左連接外運行。給出where子句消息中的同一未知列。

僅供參考 - 這工作:

SELECT payer_email, COUNT(*) as count 
FROM paypal_ipn_orders 
WHERE packing_slip_printed = 0 
AND LOWER(payment_status) = 'completed' 
AND address_name <> '' 
GROUP BY payer_email 
ORDER BY count DESC; 

回答

0
FROM paypal_ipn_orders table1, 

你的意思是... FROM paypal_ipn_orders AS table1 (...

另請注意,逗號在table1,的末尾。我不確定這是否是有意的?

+0

對不起,它無法同我們沒有「‘爲’表1」我也曾嘗試表名「paypal_ipn_orders」具有相同的結果。 – 2013-02-24 13:30:58

+0

好的,如果您仍然遇到問題,我會稍後再仔細研究。 – 2013-02-24 17:15:20

+0

謝謝約翰,對我來說這仍然是一個神祕的東西。 – 2013-02-24 17:56:21

0

這到底是什麼工作:

SELECT * 
     FROM (SELECT payer_email, COUNT(*) as count 
      FROM paypal_ipn_orders 
      Where packing_slip_printed = 0 
      AND LOWER(payment_status) = 'completed' 
      AND address_name <> '' 
      GROUP BY payer_email 
      ORDER BY count DESC) as table1, 
     paypal_ipn_orders as table2 
     LEFT join itemkey on table2.item_number = itemkey.item_id 
     WHERE table1.payer_email = table2.payer_email 
     and packing_slip_printed = 0 
     AND LOWER(payment_status) = 'completed' 
     AND address_name <> '' 
     ORDER BY count DESC, auction_multi_item, sort_id 
+0

感謝發佈!對不起,我沒有機會看,但我很高興你明白了。 – 2013-02-27 18:42:53