简体   繁体   中英

Concat comma separated firstname and lastname using mysql

I have a users table look like this.

id      name      ma_lastname

1      michael       zohans   
2       todd         butler
3       sam          pile

And another table named config_project

 id      pass
  1       1,3
  2       3,2
  3       2,1

Now i want the output look like this

 id               pass

  1      michael zohans, sam pile
  2       sam pile, todd butler
  3       todd butler, michael zohans

You should really consider fixing your table structure. Normalize it.

For now, this should do (in the order of increasing user id):

select
    c.id,
    group_concat(concat(u.name, ' ', u.ma_lastname) order by u.id separator ', ') pass
from config_project c
left join users u on find_in_set(u.id, c.pass) > 0
group by c.id;

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM