美文网首页mysql
mysql group查询合并结果

mysql group查询合并结果

作者: 顶尖少爷 | 来源:发表于2022-03-01 15:12 被阅读0次

数据表: 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查询合并结果

    本文链接:https://www.haomeiwen.com/subject/vxndrrtx.html