0
以下是情況。我需要連接訂單表和消息表。但我只對第一條消息感興趣(最低的消息ID)。表格之間的連接是orderid。SQL - GROUP BY - HAVING - MISSING ROWS
$result = $this->db->executeS('
SELECT o.*, c.iso_code AS currency, s.name AS shippingMethod, m.message AS note
FROM '._DB_PREFIX_.'orders o
LEFT JOIN '._DB_PREFIX_.'currency c ON c.id_currency = o.id_currency
LEFT JOIN '._DB_PREFIX_.'message m ON m.id_order = o.id_order
LEFT JOIN '._DB_PREFIX_.'carrier s ON s.id_carrier = o.id_carrier
LEFT JOIN jtl_connector_link l ON o.id_order = l.endpointId AND l.type = 4
WHERE l.hostId IS NULL AND o.date_add BETWEEN DATE_SUB(NOW(), INTERVAL 1 WEEK) AND NOW()
GROUP BY o.id_order
HAVING MIN(m.id_message)
LIMIT '.$limit
);
此查詢適用於目前爲止。但是現在沒有消息的訂單缺失。
謝謝你的幫助! Markus
您在'HAVING'中沒有做任何比較,所以最好將其刪除 –
謝謝。我想我誤解了HAVING min(m.id_message)爲「只返回具有最小messageid的行」。 – mysecondworld
跳過GROUP BY和HAVING子句,改爲執行'ORDER BY m.id_message LIMIT 1'。 – jarlh