SQL - 每种类型只给我 3 次点击
·
问题:SQL - 每种类型只给我 3 次点击
我有某种不可能的要求:)。
我有一张表,其中一列名为type。我想为该列中的每种类型选择 3 条记录。那可能吗?
另请注意,我使用的是 MySQL 和 Sphinx。
更新:表结构
id title type
1 AAAA string1
2 CCCC string2
3 EEEE string2
4 DDDD string2
5 FFFF string2
6 BBBB string2
6 BBBB string2
我希望我的 MySQL 返回的是(按标题排序的每种类型最多 3 条记录):
id title type
1 AAAA string1
6 BBBB string2
2 CCCC string2
4 DDDD string2
解答
select id, title, type
from (select id, title, type,
@num := if(@group = type, @num + 1, 1) as row_number,
@group := type as dummy
from your_table
order by type, title) as x
where row_number <= 3
(使用[与Martin Wickman 的答案相同的站点上的不同文章](http://www.xaprb.com/blog/2006/12/02/how-to-number-rows-in-mysql/)!)
更多推荐



所有评论(0)