我需要从一个表中选择多列信息,并将其显示在另一个只有两列的表中。情况是这样的:第一个表(多列):
MATH111 MATH112 MATH113 MATH114
67 89 54 23
我想在这个(两列表)中显示它:
Course Score
MATH111 67
MATH112 89
MATH113 54
MATH114 23
我对php非常熟悉,可以从多列表中获取和显示信息:
"select * from xx where id=''";
echo "<table border='1'>
<tr>
<th>MATH111</th>
<th>MATH112</th>
<th>MATH113</th>
<th>MATH114</th>
</tr>";
while($row=(mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['MATH111'] . "</td>";
echo "<td>" . $row['MATH112'] . "</td>";
echo "<td>" . $row['MATH113'] . "</td>";
echo "<td>" . $row['MATH114'] . "</td>";
echo "</tr>";
}
echo "</table>";
我想显示它的表格将是:
echo '<table><tr><th>Course</th><th>Score</th></tr>';
我需要的是关于代码的帮助来显示它,正如我上面概述的,它的格式是:
Course Score
MATH111 67
MATH112 89
MATH113 54
MATH114 23
请允许我添加"课程"列的值基于所选的表。所以我不能硬编码。
将SQL查询重写为类似的内容
select 'MATH111' Course, MATH111 Score from xx where id=something
union
select 'MATH112' Course, MATH112 Score from xx where id=something
union
select 'MATH113' Course, MATH113 Score from xx where id=something
union
select 'MATH114' Course, MATH114 Score from xx where id=something
并且应该处理剩下的
为什么不只是:
echo '<tr><td>MATH111</td><td>', $row['MATH111'], '</td></tr>',
'<tr><td>MATH112</td><td>', $row['MATH112'], '</td></tr>' [ ... ];