如何从具有相同查询但表中条件不同的数据库中选择数据?

时间:2019-04-19 22:09:26

标签: php mysql

我有两个表:

  1. 学生
  2. 出勤

我想select按年份和exam_type(期中,期末)的条件出勤并在一张表中显示。

$student_attendance1 = mysqli_query($con, "SELECT * 
FROM student_attendance 
INNER JOIN student 
ON student.student_id = student_attendance.student_id 
WHERE attendance_year=$attendance_year 
AND exam_type=1");

$row_studend_attendance1 = mysqli_fetch_assoc($student_attendance1);

$studend_attendance2 = mysqli_query($con, "SELECT * 
FROM student_attendance 
INNER JOIN student 
ON student.student_id = student_attendance.student_id 
WHERE attendance_year=$attendance_year 
AND exam_type=1");

$row_studend_attendance2 = mysqli_fetch_assoc($student_attendance2);

我该怎么做? DATA

1 个答案:

答案 0 :(得分:0)

我假设您每位学生总是有一行,只有一排,exam_type = 1或exam_type = 2(?)(顺便说一句,您写的两个查询绝对相同...)

您应该添加类似“ ORDER BY student.student_id ASC”的子句,以确保您以相同的顺序从两个查询中检索数据。

然后,您要做的就是将数据显示在表格中:

<?php
$student_attendance1 = mysqli_query($con, "SELECT * 
FROM student_attendance 
INNER JOIN student 
ON student.student_id = student_attendance.student_id 
WHERE attendance_year=$attendance_year 
AND exam_type=1 ORDER BY student.student_id ASC");

$studend_attendance2 = mysqli_query($con, "SELECT * 
FROM student_attendance 
INNER JOIN student 
ON student.student_id = student_attendance.student_id 
WHERE attendance_year=$attendance_year 
AND exam_type=2 ORDER BY student.student_id ASC");

while($row_studend_attendance1 = mysqli_fetch_assoc($student_attendance1)) {
    $row_studend_attendance2 = mysqli_fetch_assoc($student_attendance2);
?> // Close your PHP tag.

    <table>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>F/Name</th>
        <th>Exams</th>
        <th>Year days</th>
        <th>Present</th>
        <th>Absent</th>
        <th>Sickness</th>
        <th>Permission</th>
      </tr>
      <tr>
        <td rowspan="3"><?= $row_studend_attendance1['student_id']  ?></td>
        <td rowspan="3"><?= $row_studend_attendance1['surname']  ?></td>
        <td rowspan="3"><?= $row_studend_attendance1['firstname']  ?></td>
        <td>Midterm</td>
        <td><?= $row_studend_attendance1['year_days']  ?></td>
        <td><?= $row_studend_attendance1['present']  ?></td>
        <td><?= $row_studend_attendance1['absent']  ?></td>
        <td><?= $row_studend_attendance1['sickness']  ?></td>
        <td><?= $row_studend_attendance1['permission']  ?></td>
      </tr>
      <tr>
        <td>Final</td>
        <td><?= $row_studend_attendance2['year_days']  ?></td>
        <td><?= $row_studend_attendance2['present']  ?></td>
        <td><?= $row_studend_attendance2['absent']  ?></td>
        <td><?= $row_studend_attendance2['sickness']  ?></td>
        <td><?= $row_studend_attendance2['permission']  ?></td>
      </tr>
      <tr>
        <td>Sum</td>
        <td><?= $row_studend_attendance1['year_days'] + $row_studend_attendance2['year_days']  ?></td>
        <td><?= $row_studend_attendance1['present'] + $row_studend_attendance2['present']  ?></td>
        <td><?= $row_studend_attendance1['absent'] + $row_studend_attendance2['absent']  ?></td>
        <td><?= $row_studend_attendance1['sickness'] + $row_studend_attendance2['sickness']  ?></td>
        <td><?= $row_studend_attendance1['permission'] + $row_studend_attendance2['permission']  ?></td>
      </tr>
    </table>

<?php } ?>

希望我能回答你的问题。如果没有,请随时询问。