Mysql union,left join,group和count

时间:2015-10-20 09:37:14

标签: mysql union

继承我的查询

 SELECT 
    fsi_courier_assignment_print_master_listing.master_listing_id,
    fsi_master_listing.transmittal_id,
    fsi_transmittals.product_name,
    fsi_transmittals.transmittal_id
 FROM fsi_courier_assignment_print_master_listing
 LEFT JOIN fsi_master_listing ON fsi_courier_assignment_print_master_listing.master_listing_id = fsi_master_listing.master_listing_id
 LEFT JOIN fsi_transmittals ON fsi_master_listing.transmittal_id = fsi_transmittals.transmittal_id
 WHERE dispatch_code_id=".$this->db->escape($dispatch_code_id)."
 UNION ALL
 SELECT 
    fsi_courier_assignment_print_master_listing_undelivered.master_listing_id,
    fsi_master_listing.transmittal_id,
    fsi_transmittals.product_name,
    fsi_transmittals.transmittal_id
 FROM fsi_courier_assignment_print_master_listing_undelivered
 LEFT JOIN fsi_master_listing ON fsi_courier_assignment_print_master_listing_undelivered.master_listing_id = fsi_master_listing.master_listing_id
 LEFT JOIN fsi_transmittals ON fsi_master_listing.transmittal_id = fsi_transmittals.transmittal_id
 WHERE dispatch_code_id=".$this->db->escape($dispatch_code_id)."

fsi_courier_assignment_print_master_listing

  master_listing_id   dispatch_code_id
          2                 2
          5                 2
          36                2
          37                2
          134               2
          135               2
          136               2
          137               2
          138               2
          139               2
          140               2

fsi_courier_assignment_print_master_listing_undelivered

  master_listing_id   dispatch_code_id
          1                 2

fsi_master_listing

  master_listing_id   transmittal_id
          1                 1
          2                 1
          5                 2
          36                2
          37                2
          134               3
          135               3
          136               3
          137               3
          138               3
          139               3
          140               3

fsi_transmittals

  transmittal_id   product_name
        1             Name 1
        2             Name 2
        3             Name 3

我尝试做的是获取fsi_courier_assignment_print_master_listingfsi_courier_assignment_print_master_listing_undelivered的产品组合结果,其中dispatch_code_id ='2'并计算它们

我的愿望输出

  Product Name    Product Count
     Name 1            2
     Name 2            3
     Name 3            7

先谢谢,希望有人可以帮我这个..

1 个答案:

答案 0 :(得分:1)

您的查询很好,您只需要:

  • 使用COUNT添加GROUP BY product_name并将您的查询作为子查询。
  • 在两个联合查询中指定transmittal_id两次,要么删除其中一个,要么给它们不同的名称(它可能在MySQL中正常工作,但建议不要这样做)。

所以你的查询将是这样的:

SELECT
  t.product_name,
  COUNT(*) AS ProductCount
FROM
(
    SELECT 
        ml.master_listing_id,
        m.transmittal_id,
        t.product_name
     FROM fsi_courier_assignment_print_master_listing AS ml
     LEFT JOIN fsi_master_listing AS m 
                                  ON ml.master_listing_id = m.master_listing_id
     LEFT JOIN fsi_transmittals AS t 
                                ON m.transmittal_id = t.transmittal_id
     UNION ALL
     SELECT 
        u.master_listing_id,
        m.transmittal_id,
        t.product_name
     FROM fsi_courier_assignment_print_master_listing_undelivered as u 
     LEFT JOIN fsi_master_listing AS m 
                                  ON u.master_listing_id = m.master_listing_id
     LEFT JOIN fsi_transmittals AS t 
                                ON m.transmittal_id = t.transmittal_id
) AS t
GROUP BY t.product_name;

这会给你:

enter image description here