gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:51:02 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com