选择最大金额行

时间:2011-12-04 16:45:42

标签: mysql greatest-n-per-group

我有一个包含以下列的表:

Categorie
Rubriek
Adv_nr

对于每个rubriek,有可能有100或更多adv_nr。

我想从每个rubriek中选择最多5行,这可能在一个查询中吗?

2 个答案:

答案 0 :(得分:0)

select * from table as t1
where (select count(*) from table as t2
       where t1.rubriek = t2.rubriek and t2.adv_nr > t1.adv_nr) < 5
order by rubriek,adv_nr desc

答案 1 :(得分:0)

select * from *ad_table*  
    where category_id IN (   
         select *category_table*.id   
            from *category_table*, *group_table*
            where *category_table*.id = *group_table*.category_id)
    LIMIT 5;