在表HTML中转换的SQL表

时间:2016-10-19 13:55:14

标签: php html sql

我有这张桌子:

+----+---------+------+----------+
| ID | ID_USER | NOTE | QUESTION |
+----+---------+------+----------+
|  1 |    12   |   3  |     1    |
|  2 |    31   |   2  |     1    |
|  3 |    12   |   9  |     1    |
|  4 |    31   |   8  |     2    |
|  5 |    12   |  10  |     2    |
|  6 |    31   |   3  |     2    |
|  7 |    14   |   4  |     3    |
+----+---------+------+----------+

如何在sql表中提取值并将它们公开在HTML表格中,如下所示?

+----------+----+----+----+
| QUESTION | 12 | 31 | 14 |
+----------+----+----+----+
|     1    |  6 |  2 |  0 |
|     2    |  2 |5.5 |  0 |
|     3    |  0 |  0 |  3 |
+----------+----+----+----+

值6,5.5,3是平均值。 使用php-mysqli。

我已经启动了代码,但我不知道如何在列中转换行。我的代码生成两列和行等于ID-s的数量。我需要生成两行和一列等于ID-s的数量。



<?php
 include 'bd_cnx.php';
 $sql = "SELECT
           id_user,
           question,
           AVG(note) AS medium_note
         FROM notes
         GROUP BY id_user, question";

 $result = $conn->query($sql);
 if ($result->num_rows > 0) {
   while($row = $result->fetch_assoc()) {
     echo '<tr><th>QUESTION</th>
               <th>'. $row['id_user'] .'</th></tr>';
     echo '<tr><td>'. $row['question'] .'</td><td>' . $row['medium_note'] . '</td></tr>';
    }
}
?>
&#13;
&#13;
&#13;

谢谢!

0 个答案:

没有答案