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

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

My tables are these :

Employee Table:

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


        
相关标签:
2条回答
  • 2021-01-16 20:08
    SELECT replace(replace(department,'1','CS'),'2','IT')  as dept  from Employee
    
    0 讨论(0)
  • 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 ;
    
    0 讨论(0)
提交回复
热议问题