将表行根据其值划分为不同的列

时间:2019-04-20 18:03:35

标签: php mysql

我有一个mysql表ID,gra,grb整数和NOT NULLS,并且包含1到10之间的不同数字。 我想根据其值将数据从gra和grb划分到不同的列。

示例:mysql表:

+------------+------------+------------+
| id         | gra        | grb        |
+------------+------------+------------+ 
| 1          | 2          | 6          |
+------------+------------+------------+ 
| 2          | 10         | 8          |
+------------+------------+------------+ 
| 3          | 9          | 5          |
+------------+------------+------------+ 

etc

预期的表输出如下:

+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| gr1        | gr2        | gr3        | gr4        | gr5        |gr6         | gr7        |gr8         |gr9         |gr10        |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
|            | 2          |            |            |            | 6          |            |            |            |            |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
|            |            |            |            |            |            |            | 8          |            | 10         |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
|            |            |            |            | 5          |            |            |            | 9          |            |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+

有没有简单的方法可以做到这一点?我以为我可以使用带有CASE函数的mysql来做到这一点,但这太长了。我认为应该有一个更简单的方法。请问我可以帮忙吗?谢谢。

1 个答案:

答案 0 :(得分:2)

您可以使用这种方法获得所需的结果

// Create connection
$conn = new mysqli('hostname', 'username', 'password', 'dbname');
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
} 
$sql    = "SELECT gra ,grb FROM `tablename`";
$result = $conn->query($sql);
$rows   = array();
$i = 0;
if ($result->num_rows > 0) {

 while($row = $result->fetch_assoc()) {
    foreach(range(1,10) as $key => $value){
        $_key = 'gr'.$value;
        $rows[$i][$_key] = ($row['gra'] == $value) ? $row['gra'] : ( ($row['grb'] == $value) ? $row['grb'] : null);
    }
    $i++;
  }
}
echo '<pre>';
print_r($rows);

您可以在这样的表格中打印

$sql    = "SELECT gra ,grb FROM `grade`";
$result = $conn->query($sql);
$rows   = array();
$i = 0;
$html = '<table border="1"><tr>';
foreach(range(1,10) as $v){
  $html.= '<td>gr'.$v.'</td>';
}
$html .= '</tr>';
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
  $html .= '<tr>';
  foreach(range(1,10) as $key => $value){
    $_key = 'gr'.$value;
     $rows[$i][$_key] = ($row['gra'] == $value) ? $row['gra'] : ( ($row['grb'] == $value) ? $row['grb'] : null);
     $html .= '<td>&nbsp;'.$rows[$i][$_key].'&nbsp;</td>';
   }
   $html .= '</tr>';
   print_r($rows[$i]);echo '<hr>';
   $i++;
 }
}
$html .= '</table>';
echo $html;