草庐IT

MySQL Group by Sum by Day

coder 2023-10-17 原文

我有一张 table

id int pk auto_inc | created int(11) | amount int | user_id int

我想创建一个按天分组的行列表,总计金额字段。

我试过这个:

SELECT created, sum(amount) as amount, id FROM total_log WHERE user_id = $this->user_id GROUP BY DAY(created)

这不会给出正确的结果。他们被分组到一排。

日期由dd/mm/yyyy格式保存为unix时间戳,如1349046000

最佳答案

SELECT 
    DATE(FROM_UNIXTIME(created)) as d, 
    sum(amount) as amount
FROM total_log 
WHERE user_id = $this->user_id 
GROUP BY d

关于MySQL Group by Sum by Day,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8794708/

有关MySQL Group by Sum by Day的更多相关文章

随机推荐