我有以下查询,它给了我正确的结果。但是超级慢。让它变慢的是
AND a.id IN (SELECT id FROM st_address GROUP BY element_id)
部分。查询应该显示我们从哪些国家获得了多少订单。
一个人可以有多个地址,但在这种情况下,我们只想要一个。
否则它将多次计算订单。也许有更好的方法来实现这一目标?一个人或某事的独特连接?
SELECT cou.title_en, COUNT(co.id), SUM(co.price) AS amount
FROM customer_order co
JOIN st_person p ON (co.person_id = p.id)
JOIN st_address a ON (co.person_id = a.element_id AND a.element_type_id = 1)
JOIN st_country cou ON (a.country_id = cou.id)
WHERE order_status_id != 7 AND a.id IN (SELECT id FROM st_address GROUP BY element_id)
GROUP BY cou.id