Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP从mysql获取数据,然后将具有类似列值和计数的行分组_Php_Mysql_Row_Fetch - Fatal编程技术网

通过PHP从mysql获取数据,然后将具有类似列值和计数的行分组

通过PHP从mysql获取数据,然后将具有类似列值和计数的行分组,php,mysql,row,fetch,Php,Mysql,Row,Fetch,我基本上有一个从数据库获取订单的程序。我的问题是,我需要以这样一种方式生成报告:脚本将获得具有相同列值的行,然后对它们进行计数并显示它们 说“订单” salesorder family product 1111111 pi_gx af000 1111111 pi_gx af000 1111112 sfng af111 1111113 pi_gx af000 salesorder系列产品 1111111皮尤gx af000 1111111皮尤gx af000 11

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

说“订单”

salesorder family product 1111111 pi_gx af000 1111111 pi_gx af000 1111112 sfng af111 1111113 pi_gx af000 salesorder系列产品 1111111皮尤gx af000 1111111皮尤gx af000 1111112 sfng af111 1111113皮尤gx af000 将显示在我的php页面中

sales order family qty product 1111111 pi_gx 2 af000 1111112 sfng 1 af111 1111113 pi_gx 1 af000 销售订单系列产品数量 1111111皮尤gx 2 af000 111111 2 sfng 1 af111 111111 3皮尤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>";
}

?>
因产品集成1X而放弃的新订单
准备:
时间范围:
12:00至2:00
生日邮票:
5/21/2012
保存的:
编制人(生产):
记录人(商店):
记录:

编辑:好的,试试这个尺寸:

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
请尝试以下查询:

SELECT 
 COUNT() AS qty, sales_order, family, products 
FROM orders 
GROUP BY sales_order

我终于明白了。谢谢你们的帮助。我将sql查询更改为:

$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  ");

而且效果很好

嘿,我对自己说,“我会在那个补丁中编辑,然后查看那个评论是什么”。很好的捕获,即使我在阅读之前已经修复了它:)感谢您的快速回复,但是,问题是我需要将它插入另一个查询中。请看我的编辑上面。你可以完全子查询它这样,没有问题。(我还没有看到编辑)嗨@zebediah49,很抱歉我的编辑迟到了。我基本上需要用上面的代码来做这件事,我在应该制作数量的地方放了一条注释。我也不确定我的第一个问题是否适合这种情况,我明白了。关系数据库的另一个奇妙特性是:
JOIN
:D。如果您不熟悉,它将从报告中选择所有内容,然后使用salesorder标识将从子查询中提取的数量粘合在一起。关系数据库就是为此创建的。我建议大家阅读一下mysql支持的聚合函数:对不起,我不太明白,因为我还是个noob。我该怎么做呢?在这种情况下,我建议阅读mySQL如何处理聚合函数、连接和子查询。我相信你自己可以找到一些好东西,但你应该先开始。对不起,还是拿不到。你能告诉我我的代码需要做什么吗?我将非常感谢你的帮助(
$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  ");