目前,我真的不知道自己做错了什么。我认为这是一个相当简单的查询。
表格:
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/