Limit query results to two rows per group

后端 未结 2 472
醉话见心
醉话见心 2021-01-27 22:49

I have a query to list all posts:

SELECT *, DATE(FROM_UNIXTIME(`timestamp`)) `date` 
FROM `posts`
ORDER BY `date` DESC

The query list all rows,

2条回答
  •  时光取名叫无心
    2021-01-27 23:19

    This might work, though I couldn't say much about it's performance...

    SELECT DATE(FROM_UNIXTIME(MyTimestamp)) AS ForDate, *
    FROM   MyPostsTable
    WHERE  2 >=
    (
        SELECT COUNT(*)
        FROM   MyPostsTable AS lookup
        WHERE  DATE(FROM_UNIXTIME(lookup.MyTimestamp)) = DATE(FROM_UNIXTIME(MyPostsTable.MyTimestamp))
        AND    lookup.MyTimeStamp >= MyPostsTable.MyTimestamp
    )
    

提交回复
热议问题