mysql中添加多个列,mysql在一列中加入多个值

I need to make a query that creates 3 columns that come from 2 tables which have the following relations:

TABLE 1 has Column ID that relates to TABLE 2 with column ID2

In TABLE 1 there is a column called user In TABLE 2 there is a column called names

There can be 1 unique user but there can be many names associated to that user.

If i do the following i get all data BUT the user column repeats itself for each name it has associated. What i want is for use to appear unique but the names columns appear with all the names associated to the user column but separated by commas, like the following:

select user,names from TABLE1 left join TABLE2 on TABLE1.id = TABLE2.id

This will show the users repeated everytime a name appears for that user. what i want is to appear like this:

USER - NAMES

cyrex - pedrox, rambo, zelda

homeboy - carmen, carlos, tom, sandra

jerry - seinfeld, christine

ninja - soloboy

etc....

解决方案

What you are looking for is the GROUP_CONCAT operator.

select user, GROUP_CONCAT(names SEPARATOR ',')

from TABLE1 left join TABLE2 on TABLE1.id = TABLE2.id

group by user

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值