如何使用mysql php显示缺席和现在

时间:2015-05-20 11:18:07

标签: php mysql

如何显示缺席和现在 此代码仅显示存在,但我希望缺少日期结果 如果我删除WHERE a.date BETWEEN '2015-05-20' AND '2015-05-20'

然后缺席和现在的结果显示完美,但我希望选择日期

如何解决此问题请帮我解决此问题

感谢

直播示例请点此链接

http://sqlfiddle.com/#!9/9604f/2

Mysql代码

SELECT s.user_name, s.user_pass,a.date
    ,  CASE WHEN a.user_name IS NOT NULL THEN 'Present' ELSE 'Absent' END as attendance_status
    FROM users s 
    LEFT JOIN attend a on s.user_name=a.user_name WHERE a.date BETWEEN '2015-05-20' AND '2015-05-20'

1 个答案:

答案 0 :(得分:1)

您需要将条件从where移动到on子句:

SELECT s.user_name, s.user_pass, a.date,
       (CASE WHEN a.user_name IS NOT NULL THEN 'Present' ELSE 'Absent'
        END) as attendance_status
FROM users s LEFT JOIN
     attend a
     ON s.user_name = a.user_name AND a.date BETWEEN '2015-05-20' AND '2015-05-20';

WHERE子句将外连接转换为内连接,因为它会过滤掉NULL值。