简体   繁体   中英

how to show absent and present using mysql php

how to show absent and present this code is showing only present but i want absent result also with date if i remove WHERE a.date BETWEEN '2015-05-20' AND '2015-05-20'

this then absent and present result is showing perfect but i want with selected date

how to fix this issue please help me to fix this issue

thanks

live example please se this link

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

Mysql code

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'

You need to move the condition from the where to the on clause:

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

The WHERE clause turns the outer join into an inner join, because it filters out the NULL values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM