我的MySQL数据库中有两个表:
customer 表的列为
列* customer_billing *表包含列
我正在* customer_billing *表上运行select查询,然后在while循环中在 customer 表中选择查询,如下所示:
$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
进行排序,但这不起作用
我怎么能绕过这个?
答案 0 :(得分:0)
您应该在customer_seq上建立客户和customer_billing之间的联接,并将公司添加到group by子句中。
这样你就可以按公司订购。
答案 1 :(得分: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
}
答案 2 :(得分: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