b_city 是城市表
b_basis 基础数据表
想实现查询基础数据表中的每个城市订单和交易额的总和,基础数据表中没有的城市 则赋值为0(城市表城市是全的,基础数据表中城市不全)
还有模糊搜索的条件( cb.addtime BETWEEN '2019-01-14' AND '2019-10-14' AND cb.brandname ='1号花店(西安)')。
下面这个用的LEFT JOIN b_basis cb ON cb.c_id =c.id 查询速度太慢。
SELECT
c.city AS city,
COALESCE(SUM(ordercount),0) AS ordercount,
COALESCE(SUM(paytransaction),0) AS paytransaction
FROM
b_city c
LEFT JOIN b_basis cb ON cb.c_id =c.id AND cb.addtime BETWEEN '2019-01-14' AND '2019-10-14' AND cb.brandname ='1号花店(西安)'
WHERE c.id>0 GROUP BY c.city ORDER BY SUM(ordercount) DESC limit 0,15;