我有一个与此类似的表格:
ID | Player1 | Player2 | Player3 | Player4 | Player5 | Player6 | Place
==============================================================================
1 | Greg | NULL | Mike | NULL | NULL | NULL | Rockford
2 | NULL | Lisa | NULL | JEFF | NULL | NULL | Peoria
我想要的输出是:
Place | Players
=====================
Rockford | Greg, Mike
Peoria | Lisa, Jeff
我尝试过的:
我尝试在 SELECT 语句中使用 CONCAT,但看起来这可能是一种草率的做法。
是否有更好的方法来选择 Player1、Player2 等 IS NOT NULL 的玩家?
最佳答案
您正在寻找concat_ws
:
select place, concat_ws(',', player1, player2, player3, player4, player5, player6) as players
from your_table;
关于MySQL 选择一组列作为一个别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43729285/