MySQL find top results for each group

前端 未结 2 1417
無奈伤痛
無奈伤痛 2021-01-28 18:21

I have searched a lot, but I can\'t find an answer or something near.

I have a table like this:

id              int(11) NO  PRI     auto_increment
attri         


        
相关标签:
2条回答
  • 2021-01-28 18:50
         SELECT     attribute_id, result
         FROM   TableName a
          WHERE 
        (
            SELECT  COUNT(*) 
            FROM    TableName b
            WHERE   a.attribute_id = b.attribute_id 
                    AND a.result <= b.result
        ) <= 5 order by result DESC;
    
    0 讨论(0)
  • 2021-01-28 18:51
    SELECT  attribute_id, result
    FROM    TableName a
    WHERE 
            (
                SELECT  COUNT(*) 
                FROM    TableName b
                WHERE   a.attribute_id = b.attribute_id 
                        AND a.result <= b.result
            ) <= 5
    
    • SQLFiddle Demo
    0 讨论(0)
提交回复
热议问题