简体   繁体   English

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

[英]How to select data from database with same query but different condition in a table?

I have two tables: 我有两张桌子:

  1. student 学生
  2. attendance

I want to select the attendance of students by conditions of year and exam_type (midterm, final) and show in one table. 我想根据年份和exam_type(期中,期末)的条件select学生的出勤率,并在一个表格中显示。

$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);

How can I do this? 我怎样才能做到这一点? DATA 数据

I will suppose you always have one, and only one, row per student for exam_type = 1 or exam_type = 2(?) (By the way, your two queries, as you wrote them, are absolutely identical...) 对于exam_type = 1或者exam_type = 2(?),我认为你每个学生总有一行,而且只有一行(顺便说一下,你写的两个查询完全相同......)

You should add a clause like "ORDER BY student.student_id ASC" to be sure you retrieve the datas from your two queries in the same order. 您应该添加类似“ORDER BY student.student_id ASC”的子句,以确保以相同的顺序从两个查询中检索数据。

Then, all you have to do is display your datas into a table : 然后,您所要做的就是将数据显示在表格中:

<?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 } ?>

I hope I answer your question. 我希望我回答你的问题。 If not, feel free to ask again. 如果没有,请随时再问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM