我的MySQL数据库中有两个表:
customer表有列
*customer_billing*表具有以下列
我在*customer_billing*表上运行一个select查询,然后在within循环中在customer表中执行一个select查询,如下所示:
$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);
}我希望能够从customer表中命令company ASC显示的结果
我已经尝试过order_by customer_seq了,很明显这只是对序列号的排序。
我也尝试过在customer表查询上执行company ASC的订单,但这也没有起作用
我怎么才能绕过这一切?
发布于 2013-10-15 09:44:25
您应该在customer_billing和customer_seq上进行连接,并将公司添加到group by子句中。
这样你就可以按公司订购了。
发布于 2013-10-15 09:45:13
您可以使用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-15 09:51:36
您需要使用加入在单个查询中执行此操作。应该是这样的:
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.companyhttps://stackoverflow.com/questions/19377884
复制相似问题