我有一张这样的表
+----+------------+------------+---------+-----+------+------+
| id | is_deleted | sort_order | version | cid | pid | qid |
+----+------------+------------+---------+-----+------+------+
| 1 | | 1 | 0 | 1 | 1 | 1 |
| 2 | | 2 | 0 | 1 | 1 | 2 |
| 3 | | 3 | 0 | 1 | 1 | 3 |
| 4 | | 1 | 0 | 1 | 2 | 7 |
| 5 | | 2 | 0 | 1 | 2 | 1 |
| 6 | ☺ | 1 | 1 | 1 | 6 | 14 |
| 7 | ☺ | 1 | 1 | 1 | 5 | 13 |
| 8 | | 1 | 0 | 1 | 4 | 12 |
| 9 | | 3 | 0 | 1 | 2 | 2 |
| 10 | | 4 | 0 | 1 | 1 | 4 |
| 11 | | 5 | 0 | 1 | 1 | 5 |
+----+------------+------------+---------+-----+------+------+
你可以看到pid重复了。 是否有可能得到以下格式
pid qid
1 1,2,3,4,5
2 7,1,2
6 14
5 13
4 12
我试过这样,但我得到的输出是
SELECT pid,GROUP_CONCAT(qid) FROM client_parent_question
------+--------------------------+
pid | GROUP_CONCAT(qid) |
------+--------------------------+
1 | 1,2,3,7,1,14,13,12,2,4,5 |
------+--------------------------+
答案 0 :(得分:3)
使用GROUP BY
SELECT pid, GROUP_CONCAT(qid)
FROM client_parent_question
GROUP BY pid
答案 1 :(得分:2)
你缺少分组
SELECT pid,GROUP_CONCAT(qid) FROM client_parent_question group by pid
答案 2 :(得分:1)
SELECT pid,GROUP_CONCAT(qid) FROM client_parent_question
Group by PID
会做到这一点。输出如下:
pid | qid
1 | 1,2,3,4,5
2 | 7,1,2
6 | 14
5 | 13
4 | 12