2 回答

TA貢獻1865條經驗 獲得超7個贊
我會給你一個有效的解決方案,并嘗試考慮一個更好/更優化的解決方案。
所以我們需要做的是在student_id上將Table與自己連接起來,然后我們需要使用DATEDIFF函數檢查exam1是否在exam2之后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
檢查此鏈接以測試查詢。
注意:您應該檢查 SQL 語法導致您收到語法錯誤的錯誤。

TA貢獻1816條經驗 獲得超6個贊
按日期順序獲取考試日期,然后您可以在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
- 2 回答
- 0 關注
- 193 瀏覽
添加回答
舉報