当一个查询语句同时出现了where,group by,having,order by的时候,执行顺序和编写顺序是:
1.执行where xx对全表数据做筛选,返回第1个结果集。
2.针对第1个结果集使用group by分组,返回第2个结果集。
3.针对第2个结果集中的每1组数据执行select xx,有几组就执行几次,返回第3个结果集。
4.针对第3个结集执行having xx进行筛选,返回第4个结果集。
5.针对第4个结果集排序。
原文链接:https://blog.csdn.net/rj532029887/article/details/39260977
SELECT
s.id AS id,
c.channel AS channel_id,
SUM(s.adPv )AS adPv,
SUM(s.charge) AS charge,
SUM(s.click) AS click,
SUM(s.pageviews) AS pageviews,
SUM(s.visitor) AS visitor,
s.date AS date
FROM
spend AS s
LEFT JOIN consulting_channel AS c ON s.channel_id = c.id
WHERE
s. STATUS = '0'
AND s.date >= '2019-8-24'
AND s.date <= '2019-8-27'
AND s.channel_id = '1'
GROUP BY
s.date
ORDER BY
s.date DESC