gpt4 book ai didi

mysql - 如何同时使用WHERE BETWEEN和GROUP BY

转载 作者:行者123 更新时间:2023-12-01 00:03:22 25 4
gpt4 key购买 nike

如何在同一查询中使用where betweengroup by

  SELECT `sales-store-id`,
(SUM (`gen-acute-sales-after-returns`) + SUM(`gen- chronic-sales-after-returns`))
AS `totalGenericSales` ,
(SUM (`eth-acute-sales- after-returns`) + SUM(`eth-chronic-sales-after-returns`) + SUM(`others-sales-
after-returns`))
AS `totalEthicalSales`
FROM `sales` GROUP BY `sales-store-id`
WHERE `date-s` BETWEEN '2018-09-01' AND '2018-10-01'

这会导致 SQL 错误,所以我尝试用 AND 代替 WHERE 但这也没有正确地对结果进行分组,它只给出了 2 个具有相同 ID 的结果

   SELECT `sales-store-id`,
(SUM (`gen-acute-sales-after-returns`) + SUM(`gen- chronic-sales-after-returns`))
AS `totalGenericSales` ,
(SUM (`eth-acute-sales- after-returns`) + SUM(`eth-chronic-sales-after-returns`) + SUM(`others-sales-
after-returns`))
AS `totalEthicalSales`
FROM `sales` GROUP BY `sales-store-id`
AND `date-s` BETWEEN '2018-09-01' AND '2018-10-01'

我在这里错过了什么?

最佳答案

WHERE 子句应该出现在 SQL 语句中的 GROUP BY 子句之前。

SQL 语言定义的子句顺序是:

  • 选择
  • 来自
  • 加入
  • 哪里
  • 分组依据
  • 拥有
  • 订购方式
  • 限制(MySQL 扩展)

关于mysql - 如何同时使用WHERE BETWEEN和GROUP BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53045396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com