我需要带有行结果的php结果
我的SQL表就像
ID | date | material | Quantity
01 | 01/01/2018| abc | 50
02 | 01/01/2018| abc | 50
03 | 02/01/2018| def | 50
04 | 03/01/2018| ghi | 50
sql查询:
select sum(Quantity) as 'Total Quantity',* group by date
我需要结果为:
01/01/2018 | 02/01/2018 | 03/01/2018
100 |50 |50
这是我的计划:
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>".$row['Date']."</td></tr>";
echo "<tr><td>".$row1['Total Quantity']."</td></tr>";
}
但我不知道如何在标题
中获取日期值答案 0 :(得分:1)
如果您使用标头值对结果集编制索引,则可以使用array_keys()
轻松访问它们:
$sql = "SELECT SUM(Quantity) as quantity, date GROUP BY date ORDER BY date";
$result = $conn->query($sql);
$rows = [];
while ($row = mysqli_fetch_assoc($result))
{
$rows[$row['date']] = $row['quantity'];
}
$header = array_keys($rows);
echo "<table><tr>";
foreach ($header as $cell)
{
echo "<th>$cell</th>";
}
echo "</tr><tr>";
foreach ($rows as $cell)
{
echo "<td>$cell</td>";
}
echo "</tr></table>";
答案 1 :(得分:0)
你可以这样做
$sql = "SELECT sum(Quantity) as Total_Quantity ,date from [Table] group by date order by date";
$result = $conn->query($sql);
while($row = mysql_fetch_assoc($result))
{
echo $row['date']." ";
echo $row['Total_Quantity']." ";
}
(未经测试,但这是它应该如何工作)