作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经尝试了很多方法来实现这一目标,但没有成功,所以我来这里寻求帮助。我将尽力提供完整的详细信息,以便我可以帮助自己获得所需的最佳解决方案。
来 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以显示他/她的结果
查询返回此
差不多到了这个水平,我现在还好,我的头痛是我想以这种方式取代结果......已经看到我的设计了。
如果有人指出我正确的方向来替换第一张图片中的信息,这将非常有帮助。
----------------------------------------------
CA Tests | Exams
-----------------------------------------------
subjects | 1 | 2 | 3 | score
-----------------------------------------------
Agricultural science | 10 | 9 | 8 | 56
-----------------------------------------------
English language | 12 | 13 | 12 | 43
-----------------------------------------------
French Language | 11 | | |
已解决:通过 JIML 的解决方案
最佳答案
更改 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/
我是一名优秀的程序员,十分优秀!