4

有人可以帮助我编写确切的查询,这可以为我提供以下所需的结果,

我写,

   SELECT tbl_order_detail.order_id, 
          tbl_order_detail.order_title, 
          tbl_order_detail.dealer_id
     FROM tbl_order_detail
LEFT JOIN tbl_order_lead_send_detail ON tbl_order_detail.order_id=tbl_order_lead_send_detail.order_id
    WHERE tbl_order_detail.order_status = 'Active'

并从一个接受dealer_id并返回dealer_name(使用另一个mysql查询)的php函数中找到经销商名称,并从另一个mysql查询中计数

但它没有给出我想要的输出。它的输出为

但我只希望上面圈出的那些在一行中,因为它们中的一切都是一样的,但它们在输出中显示了很多次(为什么不是一个)。

有人可以帮助我吗?

4

3 回答 3

3

如果你GROUP BY order_id,你应该很高兴

编辑:

要获取经销商名称,只需加入经销商表并选择经销商名称列。

未经测试,可能包含错误

SELECT
    tbl_order_detail.order_id, 
    tbl_order_detail.order_title, 
    dealer.dealer_name,
    COUNT(tbl_order_lead_send_detail.order_id) AS total_customers_count
FROM tbl_order_detail
JOIN dealers ON (tbl_order_detail.dealer_id = dealers.dealer_id)
LEFT JOIN tbl_order_lead_send_detail ON (tbl_order_detail.order_id = tbl_order_lead_send_detail.order_id)
WHERE tbl_order_detail.order_status = 'Active'
GROUP BY tbl_order_detail.order_id, tbl_order_detail.order_title, dealer.dealer_name;
于 2012-05-12T14:51:55.297 回答
1

要获得计数,请使用COUNT(). 要按订单 ID 对结果进行分组,请使用GROUP BY tbl_order_detail.order_id- MySQL(或一般的 SQL)的优点之一是它非常接近简单的英语。

于 2012-05-12T14:52:35.243 回答
0

试试这个:

SELECT OD.order_id,        
       OD.order_title, 
       OD.dealer_id,
       SD.customer_id, 
       COUNT(SD.id) AS NumCustomers
FROM      tbl_order_detail OD
LEFT JOIN tbl_order_lead_send_detail SD
      ON OD.order_id=SD.order_id
WHERE OD.order_status='Active'
GROUP BY OD.order_id,        
         OD.order_title, 
         OD.dealer_id,
         SD.customer_id
于 2012-05-12T14:56:07.827 回答