我有两个针对同一个表运行的查询,除了第二个因添加左连接而失败。
成功运行
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;
然而,第二个查询因“'on 子句'中的“未知列'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;
table1 后面的逗号是必需的,因为 (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 子句'中的未知列'table2.payer_email'。但这不会在没有左连接的情况下运行。在 where 子句消息中给出相同的 Unknown 列。
仅供参考 - 这有效:
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;