gpt4 book ai didi

php - 在 MySQL 中将一行的列连接为单个输出

转载 作者:行者123 更新时间:2023-11-29 04:23:02 24 4
gpt4 key购买 nike

我有一个成员表,其中包含名字、中间名和姓氏的单独列:

id,firstname,middlename,lastname,role,photo

如何通过一次查询检索到完整的名称?我尝试使用 AS 关键字,但我不知道如何将这 3 列合并为一列:

SELECT id,lastname AS name FROM members ORDER BY role

例如,我想检索全名(即名字、中间名和姓氏)作为 name,当然每个值之间有一个空格。

最佳答案

使用CONCAT_WS( )

SELECT ID, CONCAT_WS(' ', firstname, middlename, lastname) AS Name
FROM members
ORDER BY role

来自文档,

CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.

关于php - 在 MySQL 中将一行的列连接为单个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18236276/

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