简体   繁体   English

Mysql逐日选择

[英]Mysql select day by day

I have table userMessage with rows id , userID , text and date (timestamp).我有表userMessage与行iduserIDtextdate (时间戳)。 I want to get statistics on the number of messages for each day.我想获得关于每天消息数量的统计信息。 how to write such a sample?这样的样本怎么写?

This SQL will work in MySql此 SQL 将在 MySql 中工作

SELECT 
CAST(`date` AS DATE) AS MessageDate, 
COUNT(*) AS TotalMessages, 
COUNT(DISTINCT userID) AS TotalUniqueUsers
FROM userMessage
GROUP BY MessageDate
ORDER BY MessageDate DESC

You can test it on db<>fiddle here您可以在db<>fiddle here上对其进行测试

(Btw, it might be better to use column names that don't look like reserved words. (顺便说一句,最好使用看起来不像保留字的列名。
Fe instead of date use a name like msgTimestamp ) Fe 而不是date使用类似msgTimestamp的名称)

I don't know what the database is, but in SQLite, I have try to run this, it is OK:我不知道数据库是什么,但是在 SQLite 中,我尝试运行它,可以:

select strftime('%Y-%m-%d',yourDateColumnName) as DATE_BY_DAY,count(*) 
  from userMessage 
 group by DATE_BY_DAY;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM