Sql Merging multiple records into one record

后端 未结 3 1398
一个人的身影
一个人的身影 2021-01-28 12:33

I have table with two columns user_id and tags.

  user_id    tags
    1    
    1    
    1    

        
相关标签:
3条回答
  • 2021-01-28 13:19

    duplicate of https://stackoverflow.com/questions/16218616/sql-marching-values-in-column-a-with-more-than-1-value-in-column-b/16218678#16218678

    select user_id, group_concat(tags separator ',')
    from t
    group by user_id
    
    0 讨论(0)
  • 2021-01-28 13:20

    Try this one -:

    SELECT t2.userid,
    stuff((
    SELECT ', ' + t1.tags
    FROM table_name as t1
    where t1.userid = t2.userid
    FOR XML PATH('')
    ), 1, 2, '')
    FROM table_name as t2
    GROUP BY t2.userid

    I hope this helps you out.

    Check the example -: http://www.sqlfiddle.com/#!3/85c89/6

    0 讨论(0)
  • 2021-01-28 13:25

    You should look into the GROUP_CONCAT function in mysql.A good example is here

    In your case it would be something like:

    SELECT user_id, GROUP_CONCAT(tags) FROM tablename GROUP BY user_id
    
    0 讨论(0)
提交回复
热议问题