Group by day from timestamp

MysqlSql

Mysql Problem Overview


In my posts table, it saves timestamp for each post record.

What query can group posts by day, using this timestamp column?

Mysql Solutions


Solution 1 - Mysql

How about this? Using the DATE function:

 SELECT DATE(FROM_UNIXTIME(MyTimestamp)) AS ForDate,
        COUNT(*) AS NumPosts
 FROM   MyPostsTable
 GROUP BY DATE(FROM_UNIXTIME(MyTimestamp))
 ORDER BY ForDate

This will show you the number of posts on each date that the table has data for.

Solution 2 - Mysql

SELECT DATE(timestamp) AS ForDate,
        COUNT(*) AS NumPosts
 FROM   user_messages
 GROUP BY DATE(timestamp)
 ORDER BY ForDate

This found for me. I have timestamp like "2013-03-27 15:46:08".

Solution 3 - Mysql

SELECT
    *
FROM
(
    SELECT DATE(FROM_UNIXTIME(MyTimestamp)) AS ForDate,
           ROW_NUMBER() OVER (PARTITION BY DATE(FROM_UNIXTIME(MyTimestamp)) ORDER BY MyTimeStamp) AS PostRowID,
           *
    FROM   MyPostsTable
)
    AS sequenced_daily_posts
WHERE
    ForDate = <whatever date(s) you want>
    AND PostRowID <= 2

Solution 4 - Mysql

I did a lot of research about this error and got a solution that if you are select timestamp within DATE then you have to use this symbol (``) before and after of column name.

select DATE(addedon) as addedon, count(*) from data where id= '$bid' GROUP BY DATE(addedon)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDJafariView Question on Stackoverflow
Solution 1 - Mysqlp.campbellView Answer on Stackoverflow
Solution 2 - MysqlGerardo LaggerView Answer on Stackoverflow
Solution 3 - MysqlMatBailieView Answer on Stackoverflow
Solution 4 - MysqlPankaj YadavView Answer on Stackoverflow