我在一段我无法找到解决方案的代码上停留了一段时间。尝试了一大堆选项,但似乎都没有。
我和所有客户都有一张桌子。它显示了他们的名字,邮政编码等。但我也想在同一张表中显示未结订单的数量。
我得到了这些表mysql表:
表1
表名:客户
列:customer_ID,邮政编码,customer_since,customer_name
表2
表名:状态
Columsn:status_ID,status_name
表3
表名:订单
列:order_ID,customer_ID,status_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>';
到目前为止,这项工作正在向我的8位客户展示。每个订单有7种不同的状态类型。最后一个是它的交付,以便一个不开放。我做了这个代码:
$statusnumber = 7;
$sql1 = mysql_query("SELECT * FROM order WHERE customer_ID = '". $id . " ' AND status_ID != '". $statusnumber . "' ");
while($prow = mysql_fetch_array($sql1, MYSQL_ASSOC))
{
$openstatus = $prow['storing_ID'];
echo $openstatus;
这个显示了每个没有status_ID 7的订单。
现在我不知道如何计算获得status_ID 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 ");
但是当我这样做时,它会多次向我展示我的所有客户,因为他从订单中获得了customer_ID,我得到了大约30个订单。它给我一个结果,如:1,1,1,1,2,2,2,3,4,4,5,5,5,5等。
我似乎无法用适当数量的订单显示所有客户1次...
帮助将不胜感激。
答案 0 :(得分:1)
有几种方法。
一种是对order
表进行OUTER JOIN。这里的技巧是customer_ID上的GROUP BY,并检查status_ID
列以返回0或1,然后使用SUM组聚合函数将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列表中使用相关子查询:
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
表,并通过查看status_ID
表来快速检查order
。 (这实际上取决于查询中包含status
表的原因;我在这里看不到需要它。)例如。
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
答案 1 :(得分:0)
这个问题分为两部分:
我如何知道订单何时开放?依赖“状态ID 1-6”通常不是正确的方法。添加其他状态会发生什么?现在“打开”是指“状态ID 1-6和8?”你可以看到这很快失控。更好的方法是在is_open
表中添加status
标志(也可能添加到order
表,但这只是出于历史目的,在您的应用程序中可能没有必要。)< / p>
如何计算每个客户的未结订单数量?这个问题的答案取决于您之前的解决方案,但这应该可以解决问题(假设您已经有了)在状态表中添加了一个is_open列,它是一个TINYINT(1),并在每个订单状态上正确设置了标志):
选择 status.status_ID, order.status_ID, order.customer_ID, customer.customer_ID, 顾客姓名, customer.postcode, customer.since, COUNT(DISTINCT IF(status.is_open = 1,order.order_ID,NULL))AS open_orders 来自客户 LEFT JOIN命令ON(order.customer_ID = customer.customer_ID) LEFT JOIN状态为ON(status.status_ID = order.status_ID) GROUP BY customer.customer_ID
请注意使用LEFT JOIN
代替INNER JOIN
,以便客户无论是否有有效订单都会显示在您的客户列表中。