显示mysql查询中当前不存在的人员列表

时间:2016-10-27 05:14:03

标签: mysql phpmyadmin

我正在尝试显示

的人员列表

案例1:

Forgotton办理登机手续并出席

案例2:

今天缺席的人今天无法办理登机手续。

在这两种情况下,签到都将为空,因为两者都没有签到

这是我迄今取得的成就

SELECT
    u.id,
    u.`firstname` AS 'Firstname',
    u.`lastname` AS 'Lastname',
    ulr.leave_status AS 'Leave Status',
    ulr.leave_from AS 'Leave From',
    ulr.leave_to AS 'Leave To',
    ulr.leave_description AS 'Leave Description',
    DATE_FORMAT(
        ulr.leave_from,
        '%Y-%m-%d'
    ) AS 'Today''s Date'
FROM
    users u
INNER JOIN users_leave_request ulr ON u.id = ulr.user_id
INNER JOIN checkin_checkout cc ON u.id = cc.users_id
WHERE
    DATE_FORMAT(
        leave_from,
        '%Y-%m-%d'
    ) = CURDATE()
AND cc.checkin_time IS NULL

users_leave_request表:table1

checkin_checkout表:table2

1 个答案:

答案 0 :(得分:0)

此查询将选择今天未签入的所有用户,如果他们在users_leave_request中有记录,则还将返回该记录的数据。 通过使用LEFT JOIN,我们避免为未在users_leave_request中输入的签到用户返回空结果。如果我们使用JOIN,则只有在usersusers_leave_request表中找到记录时才会返回结果。

SELECT
 u.id,
 u.`firstname` AS 'Firstname',
 u.`lastname` AS 'Lastname',
 ulr.leave_status AS 'Leave Status',
 ulr.leave_from AS 'Leave From',
 ulr.leave_to AS 'Leave To',
 ulr.leave_description AS 'Leave Description',
 DATE_FORMAT(
    ulr.leave_from,
    '%Y-%m-%d'
 ) AS 'Today''s Date'
FROM users AS u
LEFT JOIN users_leave_request ulr ON u.id = ulr.user_id
WHERE u.id NOT IN (
    SELECT u.id
    FROM
        users u
    INNER JOIN checkin_checkout cc ON u.id = cc.users_id
    WHERE
        DATE_FORMAT(cc.checkin_time, '%Y-%m-%d') = CURDATE()    
)