【问题标题】:How to design a pivot table from MySQL如何从 MySQL 设计数据透视表
【发布时间】:2013-09-03 07:37:45
【问题描述】:

我已经尝试了很多来实现这一点,但没有成功,所以我在这里寻求帮助。我会尽量提供完整的细节,这样我就可以帮助自己获得最需要的解决方案。

来自我的 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 的解决方案

【问题讨论】:

    标签: php mysql database-design pivot pivot-table


    【解决方案1】:

    更改 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
    

    然后将其添加到您生成的表格中。

    【讨论】:

    • 让我试试看,我感觉很好,因为我认为这会奏效
    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2020-08-28
    相关资源
    最近更新 更多