How to transform vertical data into horizontal data with SQL?

前端 未结 3 1095
你的背包
你的背包 2020-12-01 18:10

I have a table \"Item\" with a number of related items, like so:

ID   Rel_ID  Name  RelRank
---  ------  ----  -------
1    1       foo   1
2    1       bar          


        
3条回答
  •  生来不讨喜
    2020-12-01 19:08

    I think you are looking for a mysql specific answer. Keep in mind that the syntax could vary across different data stores.

    MySQL has a feature that makes this easy.

    SELECT Rel_ID, GROUP_CONCAT(Name SEPARATOR ' ') As Names FROM Item GROUP BY Rel_ID;
    

    that should work :-)

提交回复
热议问题