在我的posts
表格中,它会为每个帖子记录保存时间戳。
使用此时间戳列,哪些查询可以按天分组?
答案 0 :(得分:57)
这个怎么样?使用DATE
函数:
SELECT DATE(FROM_UNIXTIME(MyTimestamp)) AS ForDate,
COUNT(*) AS NumPosts
FROM MyPostsTable
GROUP BY DATE(FROM_UNIXTIME(MyTimestamp))
ORDER BY ForDate
这将显示表格包含数据的每个日期的帖子数量。
答案 1 :(得分:31)
SELECT DATE(timestamp) AS ForDate,
COUNT(*) AS NumPosts
FROM user_messages
GROUP BY DATE(timestamp)
ORDER BY ForDate
这为我找到了。我有时间戳,如“2013-03-27 15:46:08”。
答案 2 :(得分:0)
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