gpt4 book ai didi

php - 如何在mysql中将3个表合并为一个表?

转载 作者:行者123 更新时间:2023-11-30 21:34:23 25 4
gpt4 key购买 nike

我有 3 个表:

1.company_master

  id            com_name        unique_id
1 stackoverflow ABCD
2 google EFGH

2.internal_employee_master

  id          employee_name        unique_id
1 Noah ABCD
2 Liam ABCD
3 William ABCD
4 Benjamin ABCD
5 Jacob EFGH

3.external_employee_master

  id    employee_name     unique_id
1 Elijah ABCD
2 Ethan ABCD
3 Alexander EFGH

我有一个包含所有公司和 onchange 事件的动态选择框我希望特定员工的所有内部和外部员工都排成一排。例如

如果用户从选择框中选择 stackoverflow,用户将从 internal_employee_masterexternal_employee_master 获取所有数据:

  id          employee_name        unique_id
1 Noah ABCD
2 Liam ABCD
3 William ABCD
4 Benjamin ABCD
1 Elijah ABCD
2 Ethan ABCD

how to create query to get above data.and then how can insert this data in one table with identification of who is internal_employee or who is external_employee. I have only one column unique_id in all 3 tables.

最佳答案

您可以使用 UNION返回两个表的组合结果...

select id, employee_name, unique_id
from internal_employee_master
where unique_id = 'ABCD'
union
select id, employee_name, unique_id
from external_employee_master
where unique_id = 'ABCD'

尽管您最好将这两个表结合起来,并且只使用一些标志来指示哪些是内部员工,哪些是外部员工。

如果您需要识别不同类型的员工,您可以在行中添加标记...

select id, employee_name, unique_id, "Internal" as typeOfEmployee
from internal_employee_master
where unique_id = 'ABCD'
union
select id, employee_name, unique_id, "External" as typeOfEmployee
from external_employee_master
where unique_id = 'ABCD'

关于php - 如何在mysql中将3个表合并为一个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54721010/

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