繁体   English   中英

为什么我的查询不返回任何行?

[英]Why does my query returns no rows?

这是我的查询:

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

这是我使用的表:

Attenance_tbl: http//prntscr.com/j21rib

它存储特定课程,房间,部分每天学生的出勤记录

status_tbl: http ://prntscr.com/j21ro8

状态表在出勤表中包含status_id的图例

student_tbl: http//prntscr.com/j21spa

它包含学生编号,学生姓名等

公告_tblhttp : //prntscr.com/j21s5h

它存储公告,如果描述为假期,则不计入该特定日期学生的出勤率。

我的问题是它不返回任何行。 在我的公告表上(如屏幕截图所示),该日期表示2018年3月24日为假期。该日期的所有出勤都不计入。 因此,必须显示2018年3月25日的2个剩余数据。因为这不是假期。 您能帮我为什么它不返回任何数据吗?

好的,我可能有话要说,因为我说这个查询非常复杂,所以我认为。

无论如何,我把它弄碎了,所以有点可读

    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

我不知道这是什么意思。

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

但是,如果所有这些都为空,则它们只会占用空白空间,不会匹配数据库中的任何内容,我对此查询进行了测试。

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

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

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

你可以在小提琴中看到它

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

当然,这些条件中的任何一个都可能是错误的,并且不会返回结果。 如果没有小提琴和一些样本数据,那是我所能做的最好的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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