0

Hello i have one table has 2 columns like

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

I want to get list of product id's records in the same group id

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
mrking
  • 1
  • 1

1 Answers1

1

You haven't specified your desired output. But if you're wanting a comma separated list by group you can do something like this answer from the below StackOverflow threads:

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 Sever

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

This will return a result set like:

group_id    product_id
2           65, 50, 30, 60, 42
5           40, 42
6           30, 65, 60
7           90, 40
gbeaven
  • 1,522
  • 2
  • 20
  • 40