如何将mysql表行显示为列

时间:2012-04-28 17:49:03

标签: php mysql

我有两个表,resultgp

result表格中,我有类似的内容:

|id||student_id   ||course_code||grade||session  ||level||semester|
|1 ||TR/2213234561||MAT111     ||A    ||2009/2010||100  ||first   |
|2 ||TR/2213234561||MAT112     ||B    ||2009/2010||100  ||first   |
|3 ||TR/2213234561||MAT113     ||C    ||2009/2010||100  ||first   |
|4 ||TR/2213234567||MAT111     ||D    ||2009/2010||200  ||first   |
|5 ||TR/2213234567||MAT112     ||C    ||2009/2010||200  ||first   |
|6 ||TR/2213234567||MAT113     ||C    ||2009/2010||200  ||first   |

然后gp

|id||student_id   ||session  ||level||semester||gp |
|1 ||TR/2213234561||2009/2010||100  ||first   ||4.2|
|2 ||TR/2213234567||2009/2010||100  ||first   ||3.5|
|3 ||TR/2213234561||2010/2011||200  ||first   ||4.2|
|4 ||TR/2213234567||2010/2011||200  ||first   ||3.5|

我想要的是这样的:

|Matriculation||MAT111||MAT112||MAT113||MAT114||GP |
|TR/2213234561||A     ||B     ||D     ||C     ||4.2|
|TR/2213234567||C     ||D     ||E     ||F     ||3.5|

课程代码不是固定的 - 这取决于学生注册的课程

我做到了:

<?php
$rst1 = mysql_query("select distinct course_code from result ", $conn);
echo "<table callspacing='4'>";
echo "<tr>";
echo "<td> Matriculation Number </td>";

$c_code = array();
while ($row = mysql_fetch_array($rst1))
{
    $c_code[] = $row['course_code'];
}

foreach($c_code as $c_code)
{
    echo "<td>" .$c_code. "</td>";
}

$sql ="SELECT result.student_id,
       MAX(CASE WHEN course_code = ' $c_code' THEN grade END)  $c_code,
       gp.CTC 
       FROM result 
       JOIN gp  
       ON gp.student_id = result.student_id
       GROUP
       BY student_id";

echo "<td> GP</td>";                         
$rst = mysql_query("$sql",$conn) or die(mysql_error());

while ($row = mysql_fetch_array($rst))
{
    echo "</tr>";
    echo "<tr>";
    echo "<td>" .$row['student_id']. "</td>";
    echo "<td>"  .$row[$c_code]. "</td>";
}

echo "<td>"  .$row[$c_code]. "</td>";
echo "<td>" .$row['CTC']. "</td>";

echo"</tr>";
echo "</table>";
?>

第一个问题是获取课程代码,因为课程不是常数。

使用该代码,我得到了类似的东西:

|Matriculation||MAT111||MAT112||MAT113||MAT114||GP|
|TR/2213234561|
|TR/2213234567|

但我想要

|Matriculation||MAT111||MAT112||MAT113||MAT114||GP |
|TR/2213234561||A     ||B     ||D     ||C     ||4.2|
|TR/2213234567||C     ||D     ||E     ||F     ||3.5|

任何建议或方向都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

你想做的事情被称为“旋转”你的数据,并且是其他一些RDBMS本身支持的东西,但MySQL没有(按照设计,因为开发人员认为这样的操作属于表示层)。

但是,您有几个选择:

  1. 构建一个相当可怕的MySQL查询来手动执行旋转操作:

    SELECT student_id AS Matriculation, MAT111, MAT112, gp AS GP
      FROM gp
      NATURAL JOIN (
        SELECT student_id, grade AS MAT111
        FROM result
        WHERE course_code = 'MAT111'
      ) AS tMAT111
      NATURAL JOIN (
        SELECT student_id, grade AS MAT112
        FROM result
        WHERE course_code = 'MAT112'
      ) AS tMAT112
      -- etc.
    WHERE level = @level AND semester = @semester
    

    如果您选择沿着这条路走下去,可以使用PHP中的循环结构或MySQL中的预准备语句自动生成此查询,从而使您的生活更轻松。

    以下是您在PHP中可以做到的一种方式:

    1. 获取课程列表:

      $dbh = new PDO('mysql:dbname=testdb;host=127.0.0.1', $user, $password);
      $qry = $dbh->query("SELECT DISTINCT course_code FROM result [WHERE ...]");
      $courses = $qry->fetchAll(PDO::FETCH_COLUMN, 0);
      
    2. 循环结果,构建上述SQL:

      mb_regex_encoding($charset);
      
      $columns = mb_ereg_replace('`', '``', $courses);
      $sql = "
      SELECT student_id AS Matriculation, `".implode("`,`", $columns)."`, gp AS GP
        FROM gp";
      
      foreach ($columns as $column) $sql .= "
        NATURAL JOIN (
          SELECT student_id, grade AS `$column`
          FROM result
          WHERE course_code = ?
        ) AS `t$column`";
      
      $sql .= "
      WHERE level = ? AND semester = ?";
      
    3. 执行SQL,将课程数组作为参数传递:

      $qry = $dbh->prepare($sql);
      
      $params = $courses;
      array_push($params, $level, $semester);
      $qry->execute($params);
      
    4. 输出结果:

      echo "<table>";
      
      echo "<tr>";
      for ($i = 0; $i < $qry->columnCount(); $i++) {
        $meta = $qry->getcolumnMeta($i);
        echo "<th scope='col'>" . htmlentities($meta['name']) . "</th>";
      }
      echo "</tr>";
      
      while ($row = $qry->fetch(PDO::FETCH_NUM)) {
        echo "<tr>";
        foreach ($row as $field) echo "<td>" . htmlentities($field) . "</td>"
        echo "</tr>";
      }
      
      echo "</table>";
      
  2. 执行上述操作作为一次性操作,以便更改MySQL数据库的结构以更接近地反映这种所需的布局(一旦转换表格很容易,但可能会影响数据库的其他用途): / p>

    CREATE TABLE StudentGrades (PRIMARY KEY('Matriculation'))
    SELECT student_id AS Matriculation, MAT111, MAT112, gp AS GP
      -- etc. as above
    

    或者,你可以创建一个VIEW,这是一种基于底层表以这种方式构建的“虚拟表”。

  3. 使用PHP手动旋转数据(相对单调乏味)。