[英]Left Join - Select ALL from left table but select only the latest from right table
[英]Mysql select - all from left and only matching right table
我有兩個表:
employees
[id,名字,姓氏,狀態]
absence
[id,employee_id,reason_type,date_from,date_to
我可以做類似的事情:
SELECT e.first_name, e.last_name, e.status, a.reason_type FROM employees e JOIN absence a ON e.id=a.employee_id
WHERE curdate() between date_from and date_to
但這只會給我在absence
表中找到的那些雇員。
有沒有辦法讓所有員工的名單和他們的狀態(對於那些在發現absence
表匹配條件currdate() between date_from and date_to
返回“是”和reason_type
)和他人說不和空值reason_type
。
謝謝。
您正在尋找left join
,但必須注意where
子句:
SELECT e.first_name, e.last_name, e.status, a.reason_type
FROM employees e LEFT JOIN
absence a
ON e.id = a.employee_id AND
curdate() between a.date_from and a.date_to ;
您可以使用以下查詢。
SELECT e.first_name, e.last_name, e.status, a.reason_type
FROM employees e
LEFT JOIN absence a ON e.id=a.employee_id
WHERE curdate() between date_from and date_to
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.