简体   繁体   中英

Why does my query returns no rows?

Here is my query:

SELECT s.student_id as 'Student_Number', CONCAT(IFNULL(s.student_fname,''),' ', 
          IFNULL(s.student_mname,''),' ', IFNULL(s.student_lname,'')) 'Student_Name',s.student_program as 'Program', 
          (SUM(IF(stat.status_description='Late',1,0))) 'Total_Lates',
          (SUM(IF(stat.status_description='Absent',1,0))) 'Total_Absences', 
          Floor((SUM(IF(stat.status_description='Late',1,0))) / 3 + (SUM(IF(stat.status_description='Absent',1,0)))) 'Total_Absence_with_Lates' 
FROM attendance_tbl a 
LEFT JOIN student_tbl s ON s.student_id=a.entity_id 
LEFT JOIN status_tbl stat ON stat.status_id=a.status_id 
left join announcement_tbl ann on ann.announcement_date=a.date 
where a.course_id='SS019' and a.entity_type='Student' 
   and CONCAT(IFNULL(s.student_fname,''),' ', IFNULL(s.student_mname,''),' ', IFNULL(s.student_lname,'')) 
   and a.date!=ann.announcement_date and ann.announcement_description!='holiday' 
GROUP BY a.entity_id, CONCAT(IFNULL(s.student_fname,''),' ', IFNULL(s.student_mname,''),' ', IFNULL(s.student_lname,'')) 
order by Student_Name ASC

Here are the tables i used:

attendance_tbl: http://prntscr.com/j21rib

it stores the attendance entry of students every day for a certain course, room, section

status_tbl: http://prntscr.com/j21ro8

the status table contains the legend of status_id in attendance table

student_tbl: http://prntscr.com/j21spa

it contains the student number, name etc of the student

announcement_tbl: http://prntscr.com/j21s5h

it stores the announcements, if the description is holiday it will not count the attendance of the student on that specific date.

My problem is that it returns no rows. On my announcement table, as shown in the screen cap, the date indicates a holiday on March 24, 2018. all attendance on that date must not be counted. Therefore, the 2 remaining data for March 25, 2018. must be shown since it's not a holiday. Can u help me why it's not returning any data?

Ok I may have something, as I said this query is very complicated, overly so I think.

Anyway I broke it all up so it's somewhat readable

    SELECT
        s.student_id as 'Student_Number',
        CONCAT(
            IFNULL(s.student_fname,''),
            ' ',
            IFNULL(s.student_mname,''),
            ' ',
            FNULL(s.student_lname,'')
        ) 'Student_Name',
        s.student_program as 'Program',
        (SUM(IF(stat.status_description='Late',1,0))) 'Total_Lates',
        (SUM(IF(stat.status_description='Absent',1,0))) 'Total_Absences',
        Floor((SUM(IF(stat.status_description='Late',1,0))) / 3 + (SUM(IF(stat.status_description='Absent',1,0)))) 'Total_Absence_with_Lates'  
    FROM
        attendance_tbl a
    LEFT JOIN
        student_tbl s ON s.student_id=a.entity_id
    LEFT JOIN
        status_tbl stat ON stat.status_id=a.status_id
    left join
        announcement_tbl ann on ann.announcement_date=a.date
    where
        a.course_id='SS019'
    and
        a.entity_type='Student'
    and
        CONCAT(IFNULL(s.student_fname,''),' ', IFNULL(s.student_mname,''),' ', IFNULL(s.student_lname,''))
    and
        a.date!=ann.announcement_date
    and
        ann.announcement_description!='holiday'
    GROUP BY
        a.entity_id, CONCAT(IFNULL(s.student_fname,''),' ', IFNULL(s.student_mname,''),' ', IFNULL(s.student_lname,''))
    order by
        Student_Name A

I don't know what the intent of this is.

and
  CONCAT(IFNULL(s.student_fname,''),' ', IFNULL(s.student_mname,''),' ', IFNULL(s.student_lname,''))
and

But if all of these are null, they concat only empty space, which wont match anything in the DB, which I did test with this query.

CREATE TABLE test(
  foo VARCHAR(1),
  bar VARCHAR(1)
);

INSERT INTO test (foo)VALUES('a');

SELECT * FROM test WHERE CONCAT(IFNULL(bar,''),' ');

You can see it in this Fiddle

https://www.db-fiddle.com/f/9512ewbrSQjLC41tY3chrx/0

Of course any one of these and conditions could be wrong and it would not return results. Without a fiddle and some sample data, that's the best I can do.

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