0

我的 MySQL 数据库中有 2 个表:

  • 顾客
  • 客户计费

客户表有列

  • 序列
  • 公司

*customer_billing* 表有列

  • 序列
  • 客户序列

我在 *customer_billing* 表上运行一个选择查询,然后在一段时间内循环客户表中的一个选择查询,如下所示:

$sql="SELECT *, SUM(quantity*unitprice) as customertotal from customer_billing where resellerid = '' and salesmanid = '' and producttype = '".$_GET["producttype"]."' group by customer_seq ";
    $rs=mysql_query($sql,$conn) or die(mysql_error());
    while($result=mysql_fetch_array($rs))
    {
        $sql2="SELECT * from customer where sequence = '".$result["customer_seq"]."' and company_status = '' ";
        $rs2=mysql_query($sql2,$conn) or die(mysql_error());
        $result2=mysql_fetch_array($rs2);
}

我希望能够company ASC客户表中订购显示的结果

我已经尝试了 order_by customer_seq 显然这只订购了序列号

我也尝试过company ASC客户表查询进行排序,但这也不起作用

我怎样才能解决这个问题?

4

3 回答 3

0

可以用Joins也不需要用second query试试这个,

$sql="SELECT c.*,ct.*, SUM(ct.quantity*ct.unitprice) as customertotal FROM
    customer_billing ct , custom c WHERE ct.resellerid = '' AND ct.salesmanid = '' 
    AND c.company_status = '' AND c.sequence=ct.customer_seq AND 
    ct.producttype = '".$_GET["producttype"]."'
    GROUP BY ct.customer_seq ORDER BY c.company ASC ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
    // your code
}
于 2013-10-15T09:45:13.180 回答
0

您需要使用join在单个查询中执行此操作。应该是这样的:

SELECT cb.customer_seq, c.sequence, c.company, SUM(quantity*unitprice) as customertotal
FROM customer_billing AS cb JOIN customer AS c
     ON cb.sequence = c.sequence
WHERE cb.resellerid = ''
  AND cb.salesmanid = ''
  AND cb.producttype = '$_GET["producttype"]'
  AND c.company_status = ''
GROUP BY cb.customer_seq, c.company
ORDER BY c.company
于 2013-10-15T09:51:36.570 回答
0

您应该在 customer_seq 上连接 customer 和 customer_billing 并将公司添加到 group by 子句。

这样,您应该可以通过公司订购。

于 2013-10-15T09:44:25.267 回答