dongma0722 2019-04-19 22:09
浏览 244
已采纳

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

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.

$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

  • 写回答

1条回答 默认 最新

  • douhun8647 2019-04-21 22:02
    关注

    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...)

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统