gpt4 book ai didi

mysql - 如何分别用他们的名字替换逗号分隔的部门 ID?

转载 作者:行者123 更新时间:2023-11-29 06:28:21 25 4
gpt4 key购买 nike

我的表格是这些:

员工表:

+-----------+----------+------------+
| id | name | department |
+-----------+----------+------------+
| 1 | Carrera | 1 |
| 2 | Taylor | 1,2 |
+-----------+----------+------------+

部门表:

+--------+-------+
| id | name |
+--------+-------+
| 1 | CS |
| 2 | IT |
+--------+-------+

员工表和部门表的输出:

+----+------------+-------------+
| id | name | department |
+----+------------+-------------+
| 1 | Carrera | CS |
| 2 | Taylor | CS,IT |
+----+------------+-------------+

最佳答案

您应该避免将数据存储为逗号分隔值,并遵循规范化。

但是在这种情况下你可以做一些事情

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 ;

关于mysql - 如何分别用他们的名字替换逗号分隔的部门 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29484913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com