Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何从学校日期列表中查找累计缺勤日期_Mysql_Sql - Fatal编程技术网

Mysql 如何从学校日期列表中查找累计缺勤日期

Mysql 如何从学校日期列表中查找累计缺勤日期,mysql,sql,Mysql,Sql,我有代码从学校日表中提取“出勤”日期(排除节假日、周末、工作日等) 我有一个代码,可以构建一个临时表,给我一个学生ID列表和他们缺席的日期 studentID | currDate 89 | 9/18/2017 89 | 10/5/2017 89 | 10/16/2017 537 | 9/6/2017 541 | 9/11/2017 541 | 9/27/2017 549 | 9/18/2017 549

我有代码从学校日表中提取“出勤”日期(排除节假日、周末、工作日等)

我有一个代码,可以构建一个临时表,给我一个学生ID列表和他们缺席的日期

studentID | currDate
89        | 9/18/2017
89        | 10/5/2017
89        | 10/16/2017
537       | 9/6/2017
541       | 9/11/2017
541       | 9/27/2017
549       | 9/18/2017
549       | 9/20/2017
549       | 9/28/2017
549       | 9/29/2017
549       | 10/2/2017
549       | 10/3/2017
549       | 10/4/2017
549       | 10/5/2017
549       | 10/10/2017
550       | 10/19/2017
845       | 9/26/2017
845       | 10/2/2017
897       | 10/5/2017
897       | 10/20/2017
990       | 9/22/2017
990       | 9/26/2017
990       | 9/27/2017
990       | 9/28/2017
990       | 10/3/2017
我想为每个学生做的是将他们的缺勤日期与学校日表的出勤日期进行比较,找出累计缺勤情况。我一直在绞尽脑汁想看看如何做到这一点(临时表、游标等),但一直没有弄明白


我通过AquaData Studio使用MySQL 2012。有什么想法吗?最后,我想要的是一份报告,其中列出了累计缺勤超过5次的学生名单。谢谢大家!

这可能就是你想要的:

MySQL 5.6架构设置

SELECT studentID
FROM absent
WHERE currDate IN (
    SELECT AttendanceDate 
    FROM school_days
  )
GROUP BY studentID
HAVING count(*) >= 5
| studentID |
|-----------|
|       549 |
|       990 |
查询1

SELECT studentID
FROM absent
WHERE currDate IN (
    SELECT AttendanceDate 
    FROM school_days
  )
GROUP BY studentID
HAVING count(*) >= 5
| studentID |
|-----------|
|       549 |
|       990 |

SELECT studentID
FROM absent
WHERE currDate IN (
    SELECT AttendanceDate 
    FROM school_days
  )
GROUP BY studentID
HAVING count(*) >= 5
| studentID |
|-----------|
|       549 |
|       990 |

这可能就是你想要的:

MySQL 5.6架构设置

SELECT studentID
FROM absent
WHERE currDate IN (
    SELECT AttendanceDate 
    FROM school_days
  )
GROUP BY studentID
HAVING count(*) >= 5
| studentID |
|-----------|
|       549 |
|       990 |
查询1

SELECT studentID
FROM absent
WHERE currDate IN (
    SELECT AttendanceDate 
    FROM school_days
  )
GROUP BY studentID
HAVING count(*) >= 5
| studentID |
|-----------|
|       549 |
|       990 |

SELECT studentID
FROM absent
WHERE currDate IN (
    SELECT AttendanceDate 
    FROM school_days
  )
GROUP BY studentID
HAVING count(*) >= 5
| studentID |
|-----------|
|       549 |
|       990 |

如果您想要五次缺席:

select s.studentId, count(*)
from tempstudents s
group by s.studentId
having count(*) >= 5;
但是,我怀疑您打算在
AttendanceDates
中连续5天。首先,添加列以枚举出勤日期:

alter table AttendanceDates add id int;

set @id = 0;
update AttendanceDates
    set id = (@id := @id + 1)
    order by AttendanceDate;
然后,您希望在一行中找到五个ID。这里有一种方法:

select studentid, count(*), min(currdate), max(currdate)
from (select ads.*,
             (@rn := if(@s = studentid, @rn + 1, if(@s := studentid, 1, 1))) as rn
      from (select s.*, ad.id
            from AttendanceDates ad join
                 tempstudents s
                 on s.currdate = ad.AttendanceDate
            order by s.studentid, ad.id
           ) ads cross join
           (select @s := 0, @rn := 0) params
     ) ads
group by s.studentid, (id - @rn)
having count(*) >= 5;

这使用变量来枚举当前日期。然后,它从标识缺勤序列的
id
中提取差异。外部聚合仅计算这些值,得到5。

如果您想要5次缺席:

select s.studentId, count(*)
from tempstudents s
group by s.studentId
having count(*) >= 5;
但是,我怀疑您打算在
AttendanceDates
中连续5天。首先,添加列以枚举出勤日期:

alter table AttendanceDates add id int;

set @id = 0;
update AttendanceDates
    set id = (@id := @id + 1)
    order by AttendanceDate;
然后,您希望在一行中找到五个ID。这里有一种方法:

select studentid, count(*), min(currdate), max(currdate)
from (select ads.*,
             (@rn := if(@s = studentid, @rn + 1, if(@s := studentid, 1, 1))) as rn
      from (select s.*, ad.id
            from AttendanceDates ad join
                 tempstudents s
                 on s.currdate = ad.AttendanceDate
            order by s.studentid, ad.id
           ) ads cross join
           (select @s := 0, @rn := 0) params
     ) ads
group by s.studentid, (id - @rn)
having count(*) >= 5;


这使用变量来枚举当前日期。然后,它从标识缺勤序列的
id
中提取差异。外部聚合仅对这些数据进行计数,得到5。

您称之为
累计缺勤的情况是什么
?向我们展示数据库模式、样本数据、当前和预期输出。请阅读,这里是学习如何提高问题质量和获得更好答案的好地方。尝试创建一个示例,您称之为“累计缺勤”?显示数据库模式、示例数据、当前和预期输出。请阅读,这里是学习如何提高问题质量和获得更好答案的好地方。尝试在“为什么需要与AttendanceDate核对”中创建一个示例?“缺勤还不够吗?”@JuanCarlosOropeza如果OP提供了一个出席人数,我希望你们可以在不需要出席的一天缺勤(我们可以只检查一门课程,或者我不知道)。但是是的,如果我们只想要缺勤超过5次的学生,我们可以删除
中的
如果不要求出勤,您不能缺勤。;)@JuanCarlosOropeza我至少可以想到3种方法:你提供一个从
/
的日期跨度,它会记录每天的情况,无论你是否需要出席;您使用完整的缺勤表,但只需要特定课程的出勤率;你有选修课,即使该课程不是强制性的(针对法律问题),也会将你标记为“缺席”我很抱歉在这里用错了词——我要的是连续缺席。州政府要求报告连续5天缺课的学生。你为什么需要与AttendanceDate联系?“缺勤还不够吗?”@JuanCarlosOropeza如果OP提供了一个出席人数,我希望你们可以在不需要出席的一天缺勤(我们可以只检查一门课程,或者我不知道)。但是是的,如果我们只想要缺勤超过5次的学生,我们可以删除
中的
如果不要求出勤,您不能缺勤。;)@JuanCarlosOropeza我至少可以想到3种方法:你提供一个从
/
的日期跨度,它会记录每天的情况,无论你是否需要出席;您使用完整的缺勤表,但只需要特定课程的出勤率;你有选修课,即使该课程不是强制性的(针对法律问题),也会将你标记为“缺席”我很抱歉在这里用错了词——我要的是连续缺席。这是一个州的要求,报告学生连续5天缺席。天哪,我很抱歉。我有两种缺勤;(1) 累积和(2)连续。我在这里用错了词。如上所述,累积是很容易的,但与出席日期相对的连续性是很难的,我也很难做到这一点。我将仔细阅读上面的代码,看看它是否对我有帮助,我非常感谢您的反馈。@Karen。只能回答你提出的问题。我建议您提出另一个问题,提供更合适的样本数据和期望的结果。编辑这个问题会使答案无效。我理解,谢谢戈登。我会做到这一点,并在提问方面做得更好。:)我选择此回答作为答案,因为您确实回答了我问得不正确的问题。我将发布另一个问题,以针对连续缺课提供更多信息。谢谢。戈登,如果你有机会,如果你看到这一点,你能告诉我冒号在@rn:=如果。。。。。。当我尝试使用它时,它会抛出一个错误。谢谢你,我很抱歉。我有两种缺勤;(1) 累积和(2)连续。我在这里用错了词。如上所述,累积是很容易的,但与出席日期相对的连续性是很难的,我也很难做到这一点。我将仔细阅读上面的代码,看看它是否对我有帮助,我非常感谢您的反馈。@Karen。只能回答你提出的问题。我建议你用更合适的样本数据问另一个问题