MYSQL Group by column with 2 rows for each group
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I need 2 id for each group. SELECT `id` , `category` . `cat_name` FROM `info` LEFT JOIN `category` ON `info` . `cat_id` = `category` . `cat_id` WHERE `category` . `cat_name` IS NOT NULL GROUP BY `category` . `cat_name` ORDER BY `category` . `cat_name` ASC How to do this? Sample Data: id cat_name 1 Cat - 1 2 Cat - 1 3 Cat - 2 4 Cat - 1 5 Cat - 2 6 Cat - 1 7 Cat - 2 Output Will be: id cat_name 6 Cat - 1 4 Cat - 1 7 Cat - 2 5 Cat - 2 回答1: If you need two arbitrary ids, then use min() and max() : SELECT c . `cat_name` , min ( id ), max