メモ。forループ内でクエリ回数激しかったのをどうにかしたくて・・・。
mysql> select * from concattest where id <= 20; +----+---------+-----------+ | id | user_id | option_id | +----+---------+-----------+ | 1 | 0 | 1 | | 2 | 0 | 2 | | 3 | 0 | 3 | | 4 | 0 | 4 | | 5 | 0 | 5 | | 6 | 0 | 6 | | 7 | 0 | 7 | | 8 | 0 | 8 | | 9 | 0 | 9 | | 10 | 1 | 10 | | 11 | 1 | 11 | | 12 | 1 | 12 | | 13 | 1 | 13 | | 14 | 1 | 14 | | 15 | 1 | 15 | | 16 | 1 | 16 | | 17 | 1 | 17 | | 18 | 1 | 18 | | 19 | 1 | 19 | | 20 | 2 | 20 | +----+---------+-----------+ 20 rows in set (0.02 sec) mysql> select user_id, group_concat(option_id) from concattest where id <= 20 group by user_id; +---------+-------------------------------+ | user_id | group_concat(option_id) | +---------+-------------------------------+ | 0 | 1,9,8,7,6,5,4,3,2 | | 1 | 19,18,17,16,15,14,13,12,11,10 | | 2 | 20 | +---------+-------------------------------+ 3 rows in set (0.00 sec)