sql query statement for xml path syntax

The function of for xml path: Combine the query results of multiple lines into one line according to certain conditions.

For example: now there is a table

Execute the following statement

  select [Department],
        (SELECT [Employee]+',' FROM dbo.[People] b WHERE b.[Department]=a.[Department] For XML Path('')) AS Student 
  from [dbo].[People] as a
  group by [Department]

get the following result

Yes, it is found that the for xml path will combine IT and HR personnel and separate them with commas.

If you want to get the people of the entire company, separated by commas, execute the following statement

select STUFF((select ','+[Employee] 
FROM (
    select [Employee]
    from dbo.[People] as a
) as c FOR XML PATH('')),1,1,'')

get the following result

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325897754&siteId=291194637