SELECT SUBSTRING_INDEX(subject_set, ',', 1) AS subject_id1, COUNT(SUBSTRING_INDEX(subject_set, ',', 1)) AS cnt FROM comment_detail WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(subject_set, ',', 2), ',', -1) = '1002' GROUP BY subject_id1 ORDER BY subject_id1;