gpt4 book ai didi

php - 查询后如何通过 PHP 对 SQL 行进行分组或标记

转载 作者:行者123 更新时间:2023-11-29 10:41:45 24 4
gpt4 key购买 nike

我有一个如下所示的 MySQL 表

id  |  roll   | exam_id  |  course_id  |  marks  |  status
----------------------------------------------------------
1 | 01001 | 1 | 1 | 56 | 1
2 | 01002 | 1 | 1 | 68 | 1
3 | 01003 | 1 | 1 | 55 | 1
4 | 01004 | 1 | 1 | 67 | 1
5 | 01001 | 1 | 2 | 54 | 1
6 | 01002 | 1 | 2 | 59 | 1
7 | 01003 | 1 | 2 | 62 | 1
8 | 01004 | 1 | 2 | 63 | 1
9 | 01001 | 2 | 3 | 61 | 1
10 | 01002 | 2 | 3 | 48 | 1
11 | 01003 | 2 | 3 | 22 | 1
12 | 01004 | 2 | 3 | 39 | 1

现在我想拥有 exam_id = 1 的所有行

SELECT * FROM result WHERE exam_id=1 ORDER BY course_id

之后,我需要在按 roll 分组后在 HTML 中显示此表,这意味着根据结果表的类(class)编号,每个卷具有行跨度

Roll   |  course_id |  marks
-----------------------------
01001 | 1 | 56
| 2 | 68
-----------------------------
01002 | 1 | 55
| 2 | 67
-----------------------------
01003 | 1 | 55
| 2 | 62
-----------------------------
01004 | 1 | 67
| 2 | 63

我正在使用 Codeigniter 框架来完成这个项目。关于如何做到这一点有什么建议吗?

提前谢谢您。

[编辑]我当前用来执行此操作的 SQL:

SELECT * FROM `exam_result` JOIN `course` ON `course`.`course_tab_id`=`exam_result`.`result_course` WHERE `exam_id` = '1' AND `result_status` = 1 GROUP BY `exam_result`.`exam_roll`, `course`.`course_tab_id` ORDER BY `exam_result`.`exam_roll` ASC, `course`.`course_id` ASC

最佳答案

尝试一下,但我的第一个查询中该表的不同之处在于,类(class)标题将包含在单个表中,我已将标记设置为 AVG,因为如果您有重复的数据,您将看到它们的平均结果用于学校的类(class)和名册或任何您使用此结构的内容

SELECT roll, exam_id, a.course_id, marks, status, course_title, course_credit
FROM
(SELECT roll, exam_id, course_id, AVE(marks) as marks, status FROM result) as a
LEFT JOIN
(SELECT course_id, course_title, course_credit FROM course) as b
ON
a.course_id = b.course_id
WHERE exam_id = '1' -- you can remove this if you wanted all exam appear on your list
GROUP BY roll, exam_id, a.course_id, marks, status, course_title, course_credit
ORDER BY roll, course_id, marks

关于php - 查询后如何通过 PHP 对 SQL 行进行分组或标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45342989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com