我在一段无法找到解决方案的代码上停留了一段时间。尝试了一大堆选项,但似乎都不起作用。
我和我所有的顾客都有一张桌子。它显示了他们的姓名、邮政编码等。但我也想在同一张表中显示未结订单的数量。
我得到了这些表mysql表:
表格1
表名:客户
列:customer_ID、postcode、customer_since、customer_name
表 2
表名:状态
列: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 次..
帮助将不胜感激。