温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySql将记录中的某个字段进行合并

发布时间:2020-06-03 14:10:33 来源:网络 阅读:7837 作者:W_Yinbin 栏目:MySQL数据库

一 将单条记录中的某个字段合并: concat()
假如对于user表,如下:

id class name age
1 1001 zh 18
2 1001 en 19
3 1002 cs 18
4 1002 jp 19

如果想将name 和age 作为一个字段显示, 有:

select id, class, concat(name, ": ", age) as name_age from user;

结果:

id class name_age
1 1001 zh:18
2 1001 en:19
3 1002 cs: 18
4 1002 jp: 19

二 将多条记录中的某些字段合并:group_coacat()
依然对上面user表, 若根据年级分组, 并将name和age全部合并在一列中显示, 有:

select class, group_concat(name, ":", age) as name_age from user group by class;

结果为:

class name_age
1001 zh:18,en:19
1002 cs:18,jp:19

使用group_coacat() 方法默认是以“,”进行分割, 如果希望以其他字符进行分割可使用“separator”, 如:

select class, group_concat(name, ":", age separator ";") as name_age from user group by class;

结果为:

class name_age
1001 zh:18;en:19
1002 cs:18;jp:19
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI