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

时间:2019-06-29 14:20:59

标签: php mysql mysqli

我有一个(Examdata)表,其中包含7列:

(班级ID,科目ID,学生ID,科目名称,考试天数,考试日期,考试时间)

我想检测第二天进行考试和另一项考试的“ student_id”,例如:

| student_id | subject_name | exam_days | exam_date |
|------------|--------------|-----------|-----------|
| 1          |  math        | Sunday    | 2/4/2019  |
| 1          | physical     | Monday    | 3/4/2019  |

在这里,student_id =“ 1”在连续的几天里要进行两次考试,也许Student_id比我想要检测的还要多。

注意:如果学生在星期四m和星期日进行考试,则不应考虑,因为他们之间是周末。

这是我的尝试:

<?php

$con = mysqli_connect("localhost", "root", "", "Exams");

$array1 = array();

$n = "SELECT DISTINCT exam_dates FROM Examdata ORDER BY exam_dates";

$queryarray1 = mysqli_query($con, $n) or die("Error in query: $queryarray1. ".mysqli_error());

while ($row = mysqli_fetch_assoc($queryarray1)) {
    $array1[] = $row;
}

// print_r($array1);

/* the output for this :

Array ( [0] => Array ( [exam_dates] => 1440-04-02 ) [1] => Array ( [exam_dates] => 1440-04-03 ) [2] => Array ( [exam_dates] => 1440-04-04 ) [3] => Array ( [exam_dates] => 1440-04-05 ) [4] => Array ( [exam_dates] => 1440-04-06 ) [5] => Array ( [exam_dates] => 1440-04-08 ) [6] => Array ( [exam_dates] => 1440-04-09 ) [7] => Array ( [exam_dates] => 1440-04-10 ) [8] => Array ( [exam_dates] => 1440-04-11 ) [9] => Array ( [exam_dates] => 1440-04-12 ) [10] => Array ( [exam_dates] => 1440-04-13 ) [11] => Array ( [exam_dates] => 1440-04-15 ) [12] => Array ( [exam_dates] => 1440-04-16 ) [13] => Array ( [exam_dates] => 1440-04-17 ) [14] => Array ( [exam_dates] => 1440-04-18 ) [15] => Array ( [exam_dates] => 1440-04-20 ) )
*/

$b = json_encode($array1);

$z = sizeof($array1);

for ($i = 0; $i < $z; $i++) {
    $search = "SELECT *  
            FROM Examdata e1
            ( 
               SELECT * 
               FROM examdata e2 where e1.exam_dates == $b[$i] && e2.exam_dates==$b($i++)  && e1.Student_ID==e2.Student_ID && e1.exam_days!='Thursday' && e2.exam_days!='sunday'
               ORDER BY `exam_dates`,`Student_ID `
            )";

    $querysearch = mysqli_query($con, $search) or die("Error in query: $querysearch. ".mysqli_error($con));

    while ($result = mysqli_fetch_array($querysearch)) {
        echo '<tr><td>' . $result['Class_ID'] .
         '</td><td>' . $result['Subject_ID'] .
         '</td><td>' . $result['Student_ID'] .
         '</td><td>' . $result['Subject_name'].
         '</td><td>' . $result['exam_days'] .
         '</td><td>' . $result['exam_dates'] .
         '</td><td>' . $result['exam_times'] .
         '</td></tr>';
    }
}

消息错误:

  

查询错误:。您的SQL语法有误;检查   对应于您的MariaDB服务器版本的手册   在'(SELECT * from exadatadata e2其中e1.exam_dates == [   && e2.exam_dates ='在第2行

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

2 个答案:

答案 0 :(得分:1)

我将为您提供一种可行的解决方案,并尝试考虑一种更好/更优化的解决方案。

因此,我们需要做的就是将表本身与student_id连接起来,然后我们需要使用DATEDIFF函数检查是否在体检2之后1天检查体检1。

这是SQL查询:

SELECT examData1.student_id, examData1.subject_name as first_subject_name, examData2.subject_name as next_subject_name , examData1.exam_days as first_exam_day, examData2.exam_days as next_exam_day, examData1.exam_date, examData2.exam_date
FROM Examdata as examData1, Examdata as examData2
WHERE examData1.student_id = examData2.student_id AND DATEDIFF(examData2.exam_date,examData1.exam_date) = 1

选中此link来测试查询。

注意:您应该检查SQL语法导致的错误,并得到语法错误。

答案 1 :(得分:0)

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

only
 - dev
 - staging
 - master

您将获得一个数组

$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'] );
    }
}