0

我基本上有这个从数据库中获取订单的程序。我的问题是我需要以这样一种方式制作报告,即脚本将获取具有相同列值的行,然后对它们进行计数并显示它们。

说表“订单”

销售订单系列产品
1111111 pi_gx af000
1111111 pi_gx af000
1111112 sfng af111
1111113 pi_gx af000

将显示在我的 php 页面中

销售订单系列数量产品
1111111 pi_gx 2 af000
1111112 sfng 1 af111
1111113 pi_gx 1 af000

它计算所述销售订单的行数并显示数量,同时在我的页面中仅显示该销售订单的单个副本。

这是代码:

<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">&nbsp;</td>
    <td class="labels">Time Coverage:  </td>
    <td class="boxed">12:00 to 2:00</td>
    <td class="divider">&nbsp;</td>
    <td class="labels">BirthStamp: </td>
    <td class="boxed">5/21/2012</td>
    <td class="divider">&nbsp;</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>";
}

?>
4

3 回答 3

1
SELECT salesorder, family, product, COUNT() AS qty FROM orders 
       GROUP BY salesorder;

EDIT: OK, try this on for size:

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
于 2012-05-24T07:08:53.907 回答
1

Try this query:

SELECT 
 COUNT() AS qty, sales_order, family, products 
FROM orders 
GROUP BY sales_order
于 2012-05-24T07:09:33.043 回答
0

Finally figured it out. Thank you guys for helping. I changed my sql query to this:

$sql = mysql_query("SELECT salesorder, masterproduct, family, birthstamp, duedate, COUNT( * ) AS total FROM report WHERE family = '$family' AND birthstamp BETWEEN '$startDT' AND '$endDT' GROUP BY salesorder, masterproduct, family, duedate  ");

and it worked fine!

于 2012-05-25T03:29:12.817 回答