MYSQL - Group by limit

前端 未结 3 1832
太阳男子
太阳男子 2020-12-06 16:48

Is there a simple way to LIMIT the GROUP BY results to the top 2. The following query returns all the results. Using \'LIMIT 2\' reduces the overall list to the top 2 entr

3条回答
  •  情书的邮戳
    2020-12-06 17:28

    SUBSTRING_INDEX(
        GROUP_CONCAT(expr1 ORDER BY expr2 SEPARATOR ";"),
        ";",
        2  /* the GROUP_LIMIT */
    )
    

    expr1 can be like CONCAT(...). Involve REPLACE to hide any ";".

提交回复
热议问题