mysql group查询合并结果

数据表: tab1

+--------------+-------------+---------+-----------+
| lan_paper_id | question_id | user_id | option_id |
+--------------+-------------+---------+-----------+
| 30 | 70 | 53212 | 266 |
| 30 | 70 | 53276 | 261 |
| 30 | 70 | 53212 | 262 |
| 30 | 70 | 53212 | 261 |
| 30 | 70 | 53276 | 266 |
| 30 | 70 | 53276 | 267 |
| 30 | 70 | 53276 | 262 |
| 30 | 70 | 53291 | 267 |
| 30 | 70 | 53291 | 266 |
| 30 | 70 | 53291 | 265 |
+--------------+-------------+---------+-----------+

分组后的结果

+--------------+-------------+----------+---------+-------------+
| lan_paper_id | question_id | order_id | user_id | option_ids |
+--------------+-------------+----------+---------+-------------+
| 30 | 70 | 44045 | 53212 | 266 |
| 30 | 70 | 44049 | 53212 | 262,261 |
| 30 | 70 | 44051 | 53276 | 261 |
| 30 | 70 | 44053 | 53276 | 266 |
| 30 | 70 | 44054 | 53276 | 267,261 |
| 30 | 70 | 44055 | 53276 | 262,267 |
| 30 | 70 | 44056 | 53276 | 267 |
| 30 | 70 | 44058 | 53291 | 267 |
| 30 | 70 | 44060 | 53291 | 266,265,267 |
| 30 | 70 | 44061 | 53291 | 267 |
+--------------+-------------+----------+---------+-------------+

 select ct.lan_paper_id,ct.question_id,ct.order_id, ct.user_id,group_concat(ct.option_id) as option_ids from  tab1 as ct group by ct.user_id,ct.order_id

你可能感兴趣的:(mysql group查询合并结果)