If you need two arbitrary identifiers, use min() and max() :
SELECT c.`cat_name` , min(id), max(id) FROM `info` i INNER JOIN `category` c ON i.`cat_id` = c.`cat_id` WHERE c.`cat_name` IS NOT NULL GROUP BY c`.`cat_name` ORDER BY c.`cat_name` ASC ;
Note. You use LEFT JOIN and then aggregate the column in the second table. This is usually not a good idea, because non-matches are all placed in a NULL group. Also, your WHERE turns LEFT JOIN into INNER JOIN , so I fixed it. The WHERE may or may not be necessary, depending on whether cat_name NULL NULL .
If you want the two largest or smallest - and you can have them in one column:
SELECT c.`cat_name`, substring_index(group_concat id order by id), ',', 2) as ids_2 FROM `info` i INNER JOIN `category` c ON i.`cat_id` = c.`cat_id` WHERE c.`cat_name` IS NOT NULL GROUP BY c`.`cat_name` ORDER BY c.`cat_name` ASC ;
Gordon linoff
source share