0

I want to join all data in table. For example: I have a table like this,

ID     Name    ForeingId
----------------------
1       A        1
2       B        1
3       C        2
4       D        1
5       E        1

I want to get as a result the following with query. Such as 'SELECT ... WHERE ForeingId=1' . I dont want to use procedure or function.

result : A,B,D,E

Sinan AKYAZICI
  • 3,942
  • 5
  • 35
  • 60

2 Answers2

0

For ORACLE, try this:

SELECT 
   LISTAGG(name, ',') WITHIN GROUP (ORDER BY name) AS NAME
  FROM  TABLE
WHERE ForeingId= '1'
bonsvr
  • 2,262
  • 5
  • 22
  • 33
0

I am using MSSQL

I thank everyone for the answers. I found answer with link of @mehul9595's comment.

Simulating group_concat MySQL function in Microsoft SQL Server 2005?

This way:

SELECT STUFF(
    (SELECT ',' + t.name FROM table_name t
     where t.foreingId = 1 FOR XML PATH ('')), 1, 1, '')
Community
  • 1
  • 1
Sinan AKYAZICI
  • 3,942
  • 5
  • 35
  • 60