Php 如何检测连续几天参加考试的学生?

Php 如何检测连续几天参加考试的学生?,php,mysql,mysqli,Php,Mysql,Mysqli,我有一个包含7列的Examdata表: 班级ID、科目ID、学生ID、科目名称、考试日期、考试时间 我想检测第二天有考试和其他考试的学生id,例如: | student_id | subject_name | exam_days | exam_date | |------------|--------------|-----------|-----------| | 1 | math | Sunday | 2/4/2019 | | 1

我有一个包含7列的Examdata表:

班级ID、科目ID、学生ID、科目名称、考试日期、考试时间

我想检测第二天有考试和其他考试的学生id,例如:

| student_id | subject_name | exam_days | exam_date |
|------------|--------------|-----------|-----------|
| 1          |  math        | Sunday    | 2/4/2019  |
| 1          | physical     | Monday    | 3/4/2019  |
这里,学生id=1连续两天有两次考试,可能学生id比我想检测的要多

注:如果学生在周四和周日有考试,则不应考虑,因为他们之间的周末

这是我的尝试: 消息错误:

查询中出现错误:。您的SQL语法有错误;检查 与右侧的MariaDB服务器版本相对应的手册 使用near“SELECT*from examdata e2其中e1.exam_日期”的语法==[ &&e2.第2行的考试日期='

-
但我不知道如何在查询中传递数组。

我将为您提供一个有效的解决方案,并尝试考虑一个更好/更优化的解决方案

因此,我们需要做的是将表本身连接到student_id上,然后我们需要使用DATEDIFF函数检查exam1是否在exam2之后1天出现

这是SQL查询:

选择examData1.student\u id,examData1.subject\u name作为第一个\u subject\u name,examData2.subject\u name作为下一个\u subject\u name,examData1.exam\u days作为第一个\u考试日期,examData2.exam\u days作为下一个\u考试日期,examData1.exam\u日期,examData2.examData2.exam\u日期 从Examdata到examData1,从Examdata到examData2 其中examData1.student\u id=examData2.student\u id和datedifferencexamdata2.exam\u date,examData1.exam\u date=1 选中此项以测试查询


注意:您应该检查SQL语法,因为您得到的是语法错误。

按日期获取考试日期顺序,然后您可以在PHP中使用类似的内容,或者您的解决方案需要纯SQL

$currentStudent = 0;
$lastCheckedDate = 0;
$listOfStudentsWithConsecutiveDates = [];

while( $row = mysqli_fetch_array( $result )  ) {
    if( $currentStudent == $row['student_id'] ) {
       if( isset($listOfStudentsWithConsecutiveDates[ $row['student_id'] ]) ) continue;
       $datediff = strtotime( $row['exam_date'] ) - $lastCheckedDate;
       if( round($datediff / (60 * 60 * 24)) == 1 ) {
           $listOfStudentsWithConsecutiveDates[ $row['student_id'] ] = $row;
       );
    } else {
       $currentStudent = $row['student_id'];
       $lastCheckedDate = strtotime( $row['exam_date'] );
    }
}
您将获得一个带有

$listOfStudentsWithConsecutiveDates[ student_id ] = first exam which is consecutive to another one for this student

如果没有表结构和示例数据,这个问题就不清楚了……您很可能会在与DATEDIFF的组合中使用像LAG/LEAD这样的使用windows功能,或者使用与DATEDIFF相关的子查询。嘿,您可能是stackoverflow的新手,我建议您共享您的表架构,这将使我们更容易为您提供解决方案:D@dharman你把项目开发和项目管理混淆了deployment@Strawberry你的评论与什么有关?我不理解你。mysqli_错误是一个错误,因为它缺少一个参数。Thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa。。
$listOfStudentsWithConsecutiveDates[ student_id ] = first exam which is consecutive to another one for this student