MySQL: Limiting number of results received based on a column value | Combining queries

我们两清 提交于 2019-12-01 03:57:27
Nathan Lippi

Found the answer when looking at the first answer in the following post:

How do I limit the number of rows per field value in SQL?

I've modified it to fit my specific needs:

SELECT * FROM
(
    SELECT *, @num := if(@some_id = some_id, @num := @num + 1, 1) as row_num,
           @some_id := some_id as some_id
    FROM example
    ORDER BY last_modified DESC
) as e
WHERE row_num <= 5

Hi Please use Group by and having clause for check limit for every id..

SELECT * FROM `example`
GROUP BY some_id
HAVING count( * ) <= 5
ORDER BY last_modified DESC

It will check for every some_id and check if number of rows for some_id is <=5 then it will be display result.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!