gpt4 book ai didi

mysql左外连接

转载 作者:IT王子 更新时间:2023-10-29 00:31:31 25 4
gpt4 key购买 nike

我有两个表:

  1. employee 字段为 employee_id、firstname、middlename、lastname
  2. timecard 包含字段 employee_id、time-in、time-out、tc_date_transaction

我想选择具有相同 employee_id 和考勤卡且日期与当前日期相同的所有员工记录。如果没有与当前日期相同的记录,则即使没有时间、超时和 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""移动到连接子句中,它将起作用。

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);

关于mysql左外连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3058834/

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