我基本上有一个从数据库获取订单的程序。我的问题是我需要以这样的方式制作报告,即脚本将获取具有相同列值的行,然后对它们进行计数并显示它们。
说表“订单”
salesorder family product 1111111 pi_gx af000 1111111 pi_gx af000 1111112 sfng af111 1111113 pi_gx af000
will display in my php page
sales order family qty product 1111111 pi_gx 2 af000 1111112 sfng 1 af111 1111113 pi_gx 1 af000
It counts the quantity of row of the said sales order and displays the quantity, at the same time displays only a single copy of that sales order in my page.
Here's the code:
<body class="printable"><h1 align="center">New Orders Dropped for Product Integration 1X</h1>
<table align="center" width="100%">
<tr>
<td class="labels">Prepared: </td>
<td class="boxed"><?php date_default_timezone_set("Asia/Singapore");$today = date("d/m/y H:i");echo $today; ?></td>
<td class="divider"> </td>
<td class="labels">Time Coverage: </td>
<td class="boxed">12:00 to 2:00</td>
<td class="divider"> </td>
<td class="labels">BirthStamp: </td>
<td class="boxed">5/21/2012</td>
<td class="divider"> </td>
<td class="labels">Saved: </td>
<td class="boxed"><?php echo $today; ?></td>
</tr>
<tr>
<td class="labels">Prepared by (Production): </td>
<td><input type="text" name="preparer" id="preparer" class="boxedPrepared" /></td>
<td class="divider"></td>
<td class="labels">Recorded by (Store): </td>
<td><input type="text" name="recorder" id="recorded" class="boxedPrepared" /></td>
<td class="divider"></td>
<td class="labels">Recorded: </td>
<td class="boxed" colspan="3"><?php echo $today; ?></td>
</tr>
</table>
<br />
<?php
$conn = mysql_connect("localhost", "root", "123456") or die(mysql_error());
mysql_select_db("store") or die(mysql_error());
$sql = mysql_query("SELECT * FROM report ORDER BY salesorder AND masterproduct ASC") or die(mysql_error());
if(mysql_num_rows($sql) == 0) {
echo "<center><b>No ORDER/S in Queue</b></center>";
} else {
echo "
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" class=\"data\">
<tr>
<td class=\"dataHeader\">Sales Order</td>
<td class=\"dataHeader\">Sales Order Code</td>
<td class=\"dataHeader\">Family</td>
<td class=\"dataHeader\">Product Code</td>
<td class=\"dataHeader\">Quantity</td>
<td class=\"dataHeader\">Birth Stamp</td>
<td class=\"dataHeader\">Due Date</td>
</tr>
";
while($result = mysql_fetch_array($sql)) {
echo "
<tr>
<td class=\"data\">".$result['salesorder']."</td>
<td class=\"data\"><span class=\"title\">*".$result['salesorder']."*</span><br />".$result['salesorder']."</td>
<td class=\"data\">".$result['family']."</td>
<td class=\"data\"><span class=\"title\">*".$result['masterproduct']."*</span><br />".$result['masterproduct']."</td>
<td class=\"data\">";
//need to echo the value here
echo "</td>
<td class=\"data\">".$result['birthstamp']."</td>
<td class=\"data\"><span class=\"title\">*".$result['duedate']."*</span><br />".$result['duedate']."</td>
</tr>
";
}
echo "</table>";
}
?>
最佳答案
SELECT salesorder, family, product, COUNT() AS qty FROM orders
GROUP BY salesorder;
编辑:好的,试穿一下尺寸:
SELECT r.*, t.qty FROM report r LEFT JOIN
(SELECT salesorder, COUNT() AS qty FROM orders
GROUP BY salesorder) t
ON t.salesorder=r.salesorder
ORDER BY r.salesorder AND r.masterproduct ASC
关于php - 通过 PHP 从 mysql 获取数据,然后将具有相似列值和计数的行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10732731/