繁体   English   中英

使用PHP显示的MySQL查询组

[英]Mysql Query Group By Display Using PHP

我想显示一个使用PHP的Mysql查询中的表格:

Vendor Name

Item Not to Drawing     Item Defective    Incorrect Item Received   Other

         9                    2                    3                  5


Vendor Name

Item Not to Drawing     Item Defective    Incorrect Item Received   Other

         2                    4                    5                  7

等等..

这是我的代码。

<?php
$con = mysql_connect("localhost","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("qa", $con);

$result = mysql_query("SELECT Vendor, Sum(draw), Sum(defective), Sun(received), Sum(other) FROM qa_reports Group by Vendor Order by Vendor ASC");
echo "<table>
<tr>
<th>Item not to Drawing</th>
<th>Item Defective</th>
<th>Incorrect Item Received</th>
<th>Other</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Sum(draw)'] . "</td>";
  echo "<td>" . $row['Sum(defective)'] . "</td>";
  echo "<td>" . $row['Sum(received'] . "</td>";
  echo "<td'>" . $row['Sum(other)'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysql_close($con);
?>

不知道如何获取上述格式的结果。

我已经使用CFML做到了这一点,但对PHP来说是新手,无法掌握如何在按字段分组的表中列出结果。

您需要为列加别名,以便在$row数组中引用它们。

SELECT vendor, 
       Sum(draw)      AS draw, 
       Sum(defective) AS defective, 
       Sun(received)  AS received, 
       Sum(other)     AS other 
FROM   qa_reports ...

然后,您可以像这样引用它们:

$row['draw'];
...

SELECT Vendor, Sum(draw) AS sumDraw, Sum(defective) AS sumDefective ...

$row['sumDraw']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM