[英]joining two database tables in mysql and laravel
我有這兩張桌子。
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
我如何加入這兩個表以獲取特定員工的假期數據,即
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
同時,我編寫了這個 SQL 查詢,它返回了重復的數據。 如何將兩個表中的數據合並為一個。
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
我想你需要一個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.