首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP选择一个mysql表并按另一个表进行排序

PHP选择一个mysql表并按另一个表进行排序
EN

Stack Overflow用户
提问于 2013-10-15 09:38:44
回答 3查看 133关注 0票数 0

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

  • 客户
  • customer_billing

customer表有列

  • 序列
  • 公司

*customer_billing*表具有以下列

  • 序列
  • customer_seq

我在*customer_billing*表上运行一个select查询,然后在within循环中在customer表中执行一个select查询,如下所示:

代码语言:javascript
复制
$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的订单,但这也没有起作用

我怎么才能绕过这一切?

EN

回答 3

Stack Overflow用户

发布于 2013-10-15 09:44:25

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

这样你就可以按公司订购了。

票数 0
EN

Stack Overflow用户

发布于 2013-10-15 09:45:13

您可以使用Joins而不需要使用second query尝试一下,

代码语言:javascript
复制
$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
}
票数 0
EN

Stack Overflow用户

发布于 2013-10-15 09:51:36

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19377884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档