Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.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 显示每个客户的未结订单数量_Php_Mysql - Fatal编程技术网

Php 显示每个客户的未结订单数量

Php 显示每个客户的未结订单数量,php,mysql,Php,Mysql,我被一段代码困住了一段时间,但我找不到解决方案。。尝试了一系列的选择,但似乎都不起作用 我和所有的顾客都有一张桌子。它显示了他们的名字,邮政编码等,但我也想在同一张表中显示未结订单的数量 我得到了以下表格: 表1 表名:客户 列:客户ID、邮政编码、客户自、客户名称 表2 表名:状态 列号:状态号,状态名 表3 表格名称:订单 列:订单ID、客户ID、状态ID 到目前为止,这是我的代码: $sql = mysql_query ("SELECT customer.customer_ID, cust

我被一段代码困住了一段时间,但我找不到解决方案。。尝试了一系列的选择,但似乎都不起作用

我和所有的顾客都有一张桌子。它显示了他们的名字,邮政编码等,但我也想在同一张表中显示未结订单的数量

我得到了以下表格:

表1

表名:客户

列:客户ID、邮政编码、客户自、客户名称

表2

表名:状态

列号:状态号,状态名

表3

表格名称:订单

列:订单ID、客户ID、状态ID

到目前为止,这是我的代码:

$sql = mysql_query ("SELECT customer.customer_ID, customer.postcode, customer.since, customer.name
FROM customer
ORDER BY customer.customer_ID desc  ");

echo '<table border="0" width="515"  >
      <tr>
    <td>
       <table cellspacing="0" cellpadding="0" border="0" width="515" id="table1" >
         <tr>
            <th width="60" align="center"><span class="tabledescription">Number:</span></td> //customernumber
        <th width="155" align="center"><span class="tabledescription">Name:</span></td> //customername
        <th width="100" align="center"><span class="tabledescription">Postcode:</span></td>//customerpostcode
        <th width="100" align="center"><span class="tabledescription">Orders open:</span></td>//amount of open orders
        <th width="100" align="center"><span class="tabledescription">Since:</span></td>//customer since
         </tr>
       </table>
    </td>
  </tr>
  <tr>
    <td>
       <div style="width:565px; height:322px; overflow:auto;">
         <table id="table1" cellspacing="0" cellpadding="0" border="0" width="575" >';

while($row = mysql_fetch_array($sql, MYSQL_ASSOC))
{
$id = $row['customer_ID'];
$name= $row['name'];
$postcode = $row['postcode'];
$status = $row['status'];
$since = $row['customer_since'];
$probleem = $row['probleem'];

$csince = date('d-m-Y', $since);

echo "<tr><td width=64><a style=' color: #009bce; text-decoration: none;' href='detailvieuwcustomer.php?id=".$id."'>".$id."</a></td>
        <td width=160>$name</td>
        <td width=105>$postcode</td>
        <td width=105>amount</td>
        <td width=105>$csince</td></tr>";

        }
echo ' </table>  
       </div>
    </td>
  </tr>
</table>'; 
这张是给我看每一张没有状态标识7的订单

现在我不知道如何计算状态为1-6的订单数量,并将未结订单数量放在正确客户后面的表格中

我还尝试加入表格:

$sql = mysql_query("SELECT status.status_ID, order.status_ID, order.customer_ID, customer.customer_ID, customer.name, customer.postcode, customer.since
        FROM order
        INNER JOIN status on (status.status_ID = order.status_ID)
        INNER JOIN customer on (customer.customer_ID = order.customer_customer_ID)
        ORDER BY customer.customer_ID desc "); 
但当我这么做的时候,它会多次向我展示我所有的客户,因为他从订单中获取客户ID,而我得到了大约30份订单。它给我的结果是:1,1,1,1,2,2,3,4,4,5,5,5,5等等

我似乎无法一次显示所有客户的订单数量


非常感谢您的帮助。

此问题分为两部分:

  • 我如何知道订单何时打开?依赖“状态ID 1-6”通常不是正确的方法。添加其他状态时会发生什么情况?现在“open”表示“status IDs 1-6和8”,您可以看到它很快就失控了。更好的方法是将
    is_open
    标志添加到
    status
    表中(可能也添加到
    order
    表中,但这只是出于历史目的,在应用程序中可能不必要)

  • 如何获得每个客户的未结订单数?这个问题的答案取决于您对上一个问题的解决方案,但这应该可以做到(假设您在状态表中添加了一个is_open列,它是一个TINYINT(1),并在每个订单状态上正确设置了标志):

    挑选 status.status\u ID, order.status\u ID, order.customer\u ID, customer.customer\u ID, customer.name, customer.postcode, 顾客:自从, 作为未结订单计数(如果(status.is_open=1,order.order_ID,NULL),则不同) 来自客户 左联合订单打开(order.customer\u ID=customer.customer\u ID) 左连接状态打开(status.status\u ID=order.status\u ID) 按客户分组。客户识别码


  • 请注意使用
    左连接
    而不是
    内部连接
    ,这样客户将在有或没有活动订单的情况下出现在您的客户列表中。

    有两种方法

    一种是对
    order
    表进行外部联接。这里的诀窍是根据客户ID分组,并在
    status\u ID
    列上进行检查,以返回0或1,然后使用SUM GROUP aggregate函数将0和1相加:

     SELECT c.customer_ID
          , c.postcode
          , c.since
          , c.name
          , SUM(IF(s.status_ID != 7,1,0)) AS open_order_count
      FROM customer c
      LEFT
      JOIN order o
        ON o.customer_ID = c.customer_ID
      LEFT
      JOIN status s
        ON s.status_ID = o.status_ID
     GROUP
        BY c.customer_ID
         , c.postcode
         , c.since
         , c.name
     ORDER
        BY c.customer_ID DESC
    
    注意:我们可以使用
    COUNT
    聚合来代替
    SUM
    ,但我们需要为不想计数的行返回NULL

          , COUNT(IF(s.status_ID != 7,1,NULL)) AS open_order_count
    

    另一种方法(通常在大型集合上性能较差)是在选择列表中使用相关子查询:

     SELECT c.customer_ID
          , c.postcode
          , c.since
          , c.name
          , ( SELECT SUM(IF(s.status_ID != 7,1,0))
                FROM order o
                LEFT
                JOIN status s
                  ON s.status_ID = o.status_ID
               WHERE o.customer_ID = c.customer_ID
            ) AS open_order_count
      FROM customer c
     ORDER BY c.customer_ID DESC
    

    注意:为了提高性能,我可能会避免连接到
    status
    表,而只通过查看
    order
    表来简化对
    status\u ID
    的检查。(这实际上取决于查询中包含
    状态
    表的原因;我只是觉得这里不需要它。)


    另一种方法是使用内联视图获取所有客户的未结订单计数,然后将其连接到客户表

     SELECT c.customer_ID
          , c.postcode
          , c.since
          , c.name
          , IFNULL(r.open_order_count,0) AS open_order_count
      FROM customer c
      LEFT
      JOIN (
             SELECT o.customer_ID
                  , SUM(IF(o.status_ID != 7,1,0)) AS open_order_count
               FROM order o
              GROUP
                 BY o.customer_ID
           ) r
        ON r.customer_ID = o.customer_ID
     ORDER BY c.customer_ID DESC
    

    . 它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO,.
    按客户分组。客户ID
    ?为me Geo工作,尽管它只显示下订单的me客户。不是收到0份订单的ons。谢谢njk,我们会调查的。在您的上一个区块中,您使用的是r.open\u order\u count和r.customer\u ID。。在这种情况下,r的用途是什么?您的第一个解决方案对我很有效,非常感谢。尽管我还有一个问题。它没有显示任何没有订单的客户。我还想补充一下。它就像一个完整的客户名单,上面有很多未结订单。感谢您所做的一切。@Phatorax:
    r
    是分配给内联视图(子查询)的别名,因此外部查询中的
    r.customer\u ID
    引用内联视图返回的列。查询中的左联接运算符应该返回
    客户
    表中的所有行,以及任何相关的
    订单
    (如果有)。(这就是外部连接对我们的作用。)我理解,谢谢你的解释。现在开始工作,使用了错误的连接。非常感谢您的帮助。旗帜是一个列,与其他任何列一样。您只需在状态表上创建一个
    TINYINT(1)
    字段,指示该状态是否为打开状态(如果该字段的值为1,则状态为打开-如果不是,则状态为关闭)。我现在明白了。谢谢,但我不想在数据库中创建不必要的列。我现在在我的php文件中找到了它。我认为你的解决方案也可以,但这不是我想要的解决方案。再次感谢。
     SELECT c.customer_ID
          , c.postcode
          , c.since
          , c.name
          , ( SELECT SUM(IF(o.status_ID != 7,1,0))
                FROM order o
               WHERE o.customer_ID = c.customer_ID
            ) AS open_order_count
      FROM customer c
     ORDER BY c.customer_ID DESC
    
     SELECT c.customer_ID
          , c.postcode
          , c.since
          , c.name
          , IFNULL(r.open_order_count,0) AS open_order_count
      FROM customer c
      LEFT
      JOIN (
             SELECT o.customer_ID
                  , SUM(IF(o.status_ID != 7,1,0)) AS open_order_count
               FROM order o
              GROUP
                 BY o.customer_ID
           ) r
        ON r.customer_ID = o.customer_ID
     ORDER BY c.customer_ID DESC