gpt4 book ai didi

mysql - 在 MySQL 中将 2 行连接成单行

转载 作者:行者123 更新时间:2023-11-30 22:05:54 26 4
gpt4 key购买 nike

我有这张表有 2 行

Id(PI,AI)    NAME     lNAME       country
1 fname1 lname1 USA
2 fname2 lname2 USA

如何将这些行连接成单行
我想在 php 中使用结果

Id        NAME                lNAME             Country
1 @ 2 fname1 @ fname2 lname1 @ lname1 USA @ USA

分隔符 = @
谢谢

最佳答案

group_concat 函数与子查询 结合使用:

SELECT 
group_concat( Id separator ' @ ') as Id,
group_concat( Name separator ' @ ') as Name,
group_concat( lNAME separator ' @ ') as lNAME,
group_concat( country separator ' @ ') as Country

FROM (SELECT * FROM table_name LIMIT 2) as t;

https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat

关于mysql - 在 MySQL 中将 2 行连接成单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41796820/

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