[英]mysql left outer join
我有兩張桌子:
employee
包含字段employee_id,名字,中間名,姓氏 timecard
with fields employee_id,time-in,time-out,tc_date_transaction 我想選擇具有相同employee_id的所有員工記錄,其中timecard和date與當前日期相同。 如果沒有與當前日期相等的記錄,那么即使沒有time-in,timeout和tc_date_transaction,也會返回員工的記錄。
我有這樣的查詢
SELECT *
FROM employee LEFT OUTER JOIN timecard
ON employee.employee_id = timecard.employee_id
WHERE tc_date_transaction = "17/06/2010";
結果應該是這樣的:
employee_id | firstname | middlename | lastname | time-in | time-out | tc_date_transaction ------------------------------------------------------------------------------------------ 1 | john | t | cruz | 08:00 | 05:00 | 17/06/2010 2 | mary | j | von | null | null | null
您正在過濾tc_date_transaction,它會過濾此字段中的所有空值,即使是由外連接生成的那些值,也會因此失敗。 將過濾器“tc_date_transaction =”17/06/2010“”移動到join子句中,它將起作用。
SELECT *
FROM employee LEFT OUTER JOIN timecard
ON employee.employee_id = timecard.employee_id and tc_date_transaction = "17/06/2010";
或寫
SELECT *
FROM employee LEFT OUTER JOIN timecard
ON employee.employee_id = timecard.employee_id
where (tc_date_transaction = "17/06/2010" or tc_date_transaction is null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.