[英]How to phrase MySQL query to get daily totals for a column in groups?
I have the following table structure: 我具有以下表结构:
vote_id date user source
-------- ------ ------ --------
1 2013-07-23 01:37:25 user1 x
2 2013-07-23 13:15:10 user2 y
3 2013-07-23 22:30:53 user3 x
4 2013-07-24 17:38:46 user1 y
What I want to get in a query is the number of votes per day for each source. 我想查询的是每个来源每天的投票数。 The issue is that I record the date to the second, but I want it sorted by day in the query. 问题是我将日期记录到第二个日期,但是我希望它在查询中按天排序。
eg The result would be something like this: 例如,结果将是这样的:
2013-07-23 source: x votes: 10
2013-07-23 source: y votes: 2
2013-07-24 source: x votes: 0
2013-07-24 source: y votes: 6
2013-07-24 source: z votes: 3
So for every day there will be the total number of votes per source (the number of sources isn't specified, it can be anything). 因此,每天会有每个来源的投票总数(未指定来源的数目,可以是任何数目)。
Any ideas? 有任何想法吗? This query is a bit above me at the moment. 此查询此刻高于我。
Try something like this: 尝试这样的事情:
SELECT
DATE(`date`) as day,
COUNT(user) as votes,
source
FROM Tablename
GROUP BY day
Use DATE()
function to extract date out of complete datetime
field. 使用DATE()
函数从完整的datetime
字段中提取日期。
Select DATE(date) Day, Source, Count(*) Votes
From Table
Group By DATE(date) Day, Source
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.