Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从MySQL设计数据透视表_Php_Mysql_Database Design_Pivot_Pivot Table - Fatal编程技术网

Php 如何从MySQL设计数据透视表

Php 如何从MySQL设计数据透视表,php,mysql,database-design,pivot,pivot-table,Php,Mysql,Database Design,Pivot,Pivot Table,我做了很多努力,但都没有成功,所以我在这里寻求帮助。我会尽量给出全部细节,这样我就可以帮助自己得到最好的解决方案 从我的MySQL数据库 Table (students) -------------------------------- studentID | class_id | and other info Table (class) ------------------------------- classID | name | other info Table (subjects

我做了很多努力,但都没有成功,所以我在这里寻求帮助。我会尽量给出全部细节,这样我就可以帮助自己得到最好的解决方案

从我的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 
然后你可以这样做(我已经模拟了db的结果)


然后,只需将其添加到您生成的表中即可。

让我试试看,我感觉很好,因为我认为这会起作用
$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