How to replace comma separated department ids with their name respectively?

前端 未结 2 2004
长发绾君心
长发绾君心 2021-01-16 19:22

My tables are these :

Employee Table:

+-----------+----------+------------+
| id       | name     | department  |
+-----------+----------+-----------         


        
2条回答
  •  甜味超标
    2021-01-16 20:27

    You should avoid storing data as comma-separated values, and follow normalization.

    However in this case you can do something as

    select 
    e.id , 
    e.name , 
    group_concat(d.name) from employee e 
    left join department d on find_in_set(d.id,e.department) 
    group by e.id ;
    

提交回复
热议问题