SQL - 仅为每种类型提供3次点击

xpe*_*int 8 mysql sql sphinx greatest-n-per-group

我有一些不可能的要求:).

我有一个表,其中一个列被命名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
Run Code Online (Sandbox Code Playgroud)

我希望MySQL返回的是(按标题排序的每种类型最多3条记录):

id       title        type
1        AAAA         string1
6        BBBB         string2
2        CCCC         string2
4        DDDD         string2
Run Code Online (Sandbox Code Playgroud)

Mar*_*ith 12

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
Run Code Online (Sandbox Code Playgroud)

(在Martin Wickman回答的同一网站上使用不同的文章!)