MySQL 选择一组列作为一个别名

标签 mysql select

我有一个与此类似的表格:

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/

相关文章:

php - 尝试让登录识别管理员

mysql - 存储动态内容|元素数量可变

c++ - 使这个 UDP 监听套接字成为非阻塞的,我是否有可能搬起石头砸自己的脚?

php - 选择数组()

php - 有没有更简单的方法来编写此内容,以便显示最新条目而不是以前的条目?

mysql - 如何从 Sonar 中的版本从 MySQL 迁移到 PostgreSQL

MySQL在一个查询中多个表中的多个选择条件?

sql - 从表中选择比较对

php - mysql SELECT 查询以正确的语法返回错误

php - 更新多个字段时更新未推送到数据库