How do i merge two or more rows based on their foreign key?

前端 未结 3 697
北恋
北恋 2020-12-12 04:02

I have three tables which are:

A(a1, a2, a3)

//This tbl (Table) can have multiple instances of a1, but cause of its dependence on b1, 
//we have a unique re         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 05:01

    Here is another solution using SQLXML:

    SELECT  
        STUFF  
        (  
            (  
            SELECT ', ' + cast(id AS NVARCHAR(max)) + ': ' + video  
            FROM Video
            ORDER BY Id  
            FOR XML PATH ('')  
            ),1,1,''  
        )
    
    CREATE TABLE [dbo].[Video](
        [ID] [int] NOT NULL,
        [Video] [varchar](50) NOT NULL,
        [Views] [int] NOT NULL
    )
    

    Result: 1: Hulk, 2: Jack, 3: The King

    Update: I also just found this at Data Management and Exchange.

提交回复
热议问题