gpt4 book ai didi

mysql - 按月分组仅返回两个表的四月份

转载 作者:行者123 更新时间:2023-11-29 17:41:20 25 4
gpt4 key购买 nike

目前,我真的不知道自己做错了什么。我认为这是一个相当简单的查询。

表格:

  operations:
id processedon clientid
1 2018-01-01 9
2 2018-03-16 9
3 2018-04-21 9
4 2018-04-20 9
5 2018-05-09 9


items:
id operation_id quantity unitprice
1 1 10 2
2 1 5 3
3 2 20 4
4 3 10 2
5 4 8 4
6 4 10 4
7 5 2 2

操作/查询的预期结果是:

 month  total_value
1 35
3 80
4 92
5 4

即数量*单价为基础。由于某种原因,它只返回month=4

SELECT 
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM `items`
INNER JOIN `operations` ON (`items`.`operation_id` = `operations`.`id`)
GROUP BY 'month'
ORDER BY 'month'

最佳答案

以下查询将为您提供所需的答案

SELECT 
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`

来自项目INNER JOIN 操作 ON (items.operation_id = 操作.id)按月分组(操作.已处理)按月份排序(操作.已处理)

您需要正确指定月份,因为它不是现有列。你会得到以下结果月total_value

1 35

3 80

4 92

5 4

关于mysql - 按月分组仅返回两个表的四月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49991824/

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