[英]joining two database tables in mysql and laravel
I have these two tables.我有这两张桌子。
as_tbl_company_holidays
id year company_id start_date, end_date, description
1 2020 1 2020-01-01 2020-01-01 New Year Holiday
2 2020 1 2020-02-14 2020-02-15 Valentine Holiday
3 2020 2 2020-01-01 2020-01-01 New Year Holiday
as_tbl_employee_holidays
id employee_id company_id year start_date end_date description
1 ASL100 1 2020-01-31 2020-01-31 Casual Holiday
2 ASL200 2 2020-04-01 2020-04-02 Easter Holiday
How can i join these two table to get a holiday data for a particular employee ie我如何加入这两个表以获取特定员工的假期数据,即
id employee_id company_id year start_date end_date description
1 ASL100 1 2020 2020-01-01 2020-01-01 New Year Holiday
2 ASL100 1 2020 2020-02-14 2020-02-15 Valentine Holiday
3 ASL100 1 2020-01-31 2020-01-31 Casual Holiday
id employee_id company_id year start_date end_date description
1 ASL200 2 2020 2 2020-01-01 2020-01-01 New Year Holiday
2 ASL200 2 2020 2 2020-04-01 2020-04-02 Easter Holiday
Meanwhile, I wrote this SQL query and it's returning duplicate data.同时,我编写了这个 SQL 查询,它返回了重复的数据。 How can I combine the data from both tables into one.如何将两个表中的数据合并为一个。
select
emp.employee_id,
comp.company_id,
comp.year,
comp.start_date,
comp.end_date
from
as_tbl_company_holidays as comp
left join as_tbl_employee_holidays as emp on comp.company_id = emp.company_id
where
emp.employee_id = "ASL100" and
comp.company_id = 1
I guess you need a union
query instead of join我想你需要一个union
查询而不是加入
select company_id,year, start_date, end_date,description
from as_tbl_company_holidays
where company_id = 1
union
select company_id,year, start_date, end_date ,description
from as_tbl_employee_holidays
where company_id = 1
and employee_id = "ASL100"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.