简体   繁体   中英

Mysql select day by day

I have table userMessage with rows id , userID , text and date (timestamp). 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

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

(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 )

I don't know what the database is, but in SQLite, I have try to run this, it is OK:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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