gpt4 book ai didi

php - 如何从 MySQL 设计数据透视表

转载 作者:行者123 更新时间:2023-11-30 01:20:38 24 4
gpt4 key购买 nike

我已经尝试了很多方法来实现这一目标,但没有成功,所以我来这里寻求帮助。我将尽力提供完整的详细信息,以便我可以帮助自己获得所需的最佳解决方案。

来 self 的 MySQL 数据库

Table (students)
--------------------------------
studentID | class_id | and other info


Table (class)
-------------------------------
classID | name | other info


Table (subjects)
--------------------------------
subjectID | name | class_id (references Class.classID)


Table (exam_type)
--------------------------------
exam_typeID | name | desc | start_date


Table (result)
---------------------------------------------
student_id | exam_type_id | subject_id | mark

在我的查询中我有这个:

SELECT subjects.name, exam_type.name, result.mark FROM subjects 
LEFT JOIN result ON result.subject_id=subjects.subjectID
JOIN exam_type ON exam_type.exam_typeID=result.exam_type_id
WHERE result.student_id=$x
ORDER BY
subjects.name, exam_type.name

在查询中$x是学生的id以显示他/她的结果

查询返回此

results shown, from dummy data in my database

差不多到了这个水平,我现在还好,我的头痛是我想以这种方式取代结果......已经看到我的设计了。

front end

如果有人指出我正确的方向来替换第一张图片中的信息,这将非常有帮助。

----------------------------------------------
CA Tests | Exams
-----------------------------------------------
subjects | 1 | 2 | 3 | score
-----------------------------------------------
Agricultural science | 10 | 9 | 8 | 56
-----------------------------------------------
English language | 12 | 13 | 12 | 43
-----------------------------------------------
French Language | 11 | | |

已解决:通过 JIML 的解决方案 solved

最佳答案

更改 SQL,这样就不会得到具有相同名称的两列

SELECT subjects.name, exam_type.name, result.mark FROM subjects 
-->
SELECT subjects.name, exam_type.name as test, result.mark FROM subjects

然后你可以做这样的事情(我已经模拟了数据库的结果)

$result = array(
0 => array(
'name' => 'Agricultural Science',
'test' => 'CA 1',
'mark' => 10
),
1 => array(
'name' => 'Agricultural Science',
'test' => 'CA 2',
'mark' => 9
),
2 => array(
'name' => 'Agricultural Science',
'test' => 'CA 3',
'mark' => 8
),
3 => array(
'name' => 'Agricultural Science',
'test' => 'Exam',
'mark' => 56
),
4 => array(
'name' => 'English Language',
'test' => 'CA 1',
'mark' => 12
),
5 => array(
'name' => 'English Language',
'test' => 'CA 2',
'mark' => 13
),
6 => array(
'name' => 'English Language',
'test' => 'CA 3',
'mark' => 12
),
7 => array(
'name' => 'English Language',
'test' => 'Exam',
'mark' => 43
)
);

$data = array();
foreach ($result as $row) {
$data[$row['name']]['marks'][$row['test']] = $row['mark'];
}

?>
CA test | Exams<br />
<?php
foreach ($data as $name => $row) {
echo $name . ' | ' . $row['marks']['CA 1'] . ' | ' . $row['marks']['CA 2'] . ' | ' . $row['marks']['CA 3'] . ' | ' . $row['marks']['Exam'] . '<br />';
}

输出

CA test | Exams
Agricultural Science | 10 | 9 | 8 | 56
English Language | 12 | 13 | 12 | 43

然后只需将其添加到生成表的方式即可。

关于php - 如何从 MySQL 设计数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18586657/

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