你好,我有一张桌子有2列
group_id product_id
2 65
2 50
2 30
2 60
2 42
5 40
5 42
6 30
6 65
6 60
7 90
7 40
我想获取同一组ID中的产品ID记录列表
答案 0 :(得分:1)
您尚未指定所需的输出。但是,如果您希望按组将逗号分隔,则可以从以下StackOverflow线程中执行类似以下答案的操作:
MySQL
SELECT t.group_id,
GROUP_CONCAT(t.product_id) AS product_id_group
FROM [YOURTABLE] t
GROUP BY t.id, t.product_id;
MySQL Results as comma separated list
SQL服务器
SELECT group_id, product_id =
STUFF((SELECT ', ' + product_id
FROM [YOURTABLE] t1
WHERE t1.group_id = t2.group_id
FOR XML PATH('')), 1, 2, '')
FROM [YOURTABLE] t2
GROUP BY group_id
SQL Server : GROUP BY clause to get comma-separated values
这将返回如下结果集:
group_id product_id
2 65, 50, 30, 60, 42
5 40, 42
6 30, 65, 60
7 90, 40