Mysql:如何查询所有用户每天创建的记录?

时间:2017-12-14 10:02:30

标签: mysql database group-by

我的表格包含以下数据:

SELECT * FROM ibiza_production.line_write_revenues order by created_at desc limit 50;

enter image description here

如您所见,上表中包含user_id和created_at列。我想查询数据,如下所示。

enter image description here

我使用了以下查询,但它只能在任何日期为1位用户提供数据。

SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at);

enter image description here

如您所见,它一次只显示一个用户的数据。 请帮忙。

4 个答案:

答案 0 :(得分:2)

在group by子句中包含user_id以获取每个日期和每个用户的数据

SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',
user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at),user_id;

答案 1 :(得分:2)

SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at), user_id;

答案 2 :(得分:0)

尝试使用concat进行分组

SELECT `date`,`user_id`,COUNT(*) FROM ibiza_production.line_write_revenues GROUP BY CONCAT(`date`,'-',`user_id`) ORDER BY `date`,`user_id` LIMIT 50

答案 3 :(得分:0)

嗨,你可以尝试这个,让我知道它是否对你有所帮助。

SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at),user_id order by DATE(created_at);