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.

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

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢