MySQL - 加入两个查询(UNION?)

时间:2014-01-23 09:57:00

标签: mysql sql select count group-by

我有三张桌子: -   - 订单(给我订单日期)   - 订单行项目(使我能够将活动结果链接到订单日期)   - 活动项目(给我活动结果)

我正在尝试在图表上绘制结果,并且可以使用以下查询成功为所有结果创建一个数组< = 50 as Passed: -

SELECT
  YEAR( od_date ) AS Year,
  MONTH( od_date ) AS Month,
  test_name,
  pd_name,
  COUNT( test_result ) AS Passed
FROM tbl_lab_item
  INNER JOIN tbl_order_item
    ON tbl_order_item.od_item_id = tbl_lab_item.od_item_id
  INNER JOIN tbl_order
    ON tbl_order.od_id = tbl_order_item.od_id
WHERE tbl_order.od_date >= DATE_SUB(now(), INTERVAL 12 MONTH)
    AND test_name = 'Test'
    AND pd_name = 'Product'
    AND test_result <= 50
    AND od_customer_id = '4'
GROUP BY test_name, YEAR(od_date), MONTH(od_date)ASC

我可以使用相同的查询对失败的结果执行类似操作,但选择中的Count(test_result)AS失败,并将WHERE条件更改为&gt;。

如果可能的话,我想在一个查询中完成另外两件事。

首先,我希望能够将通过和失败结合到同一个查询中,这样我就可以获得月份,通过次数,失败次数。

其次,为了在图表上绘制所有这些,我可能需要显示所有月份,即使没有通过,失败或两者都没有结果。根据我对UNION的理解,查询需要具有相同的形状,因此我可以将Null as Failed添加到Pass查询中,反之亦然,但是如何将其合并为一种获取所有月份显示的方法? / p>

我在这里看How to get a count even if there are no results corresponding mysql?以显示所有月份,但是我没有将此信息整合到我的查询中的知识。

希望这一切都有意义吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT YEAR(od_date) AS `Year`, MONTH(od_date) AS `Month`, test_name, pd_name, 
       SUM(test_result <= 50) AS Passed, SUM(test_result > 50) AS Failes
FROM tbl_lab_item li
INNER JOIN tbl_order_item oi ON oi.od_item_id = li.od_item_id
INNER JOIN tbl_order o ON o.od_id = oi.od_id
WHERE o.od_date >= DATE_SUB(NOW(), INTERVAL 12 MONTH) AND test_name = 'Test' AND 
      pd_name = 'Product' AND od_customer_id = '4'
GROUP BY test_name, YEAR(od_date), MONTH(od_date) ASC

答案 1 :(得分:0)

对于传球和失败,你可以做到

SELECT
  YEAR( od_date ) AS Year,
  MONTH( od_date ) AS Month,
  test_name,
  pd_name,
  SUM( IF(test_result <= 50 , 1 , 0) ) AS Passed,
  SUM( IF(test_result > 50 , 1 , 0) ) AS Failed
FROM tbl_lab_item
  INNER JOIN tbl_order_item
    ON tbl_order_item.od_item_id = tbl_lab_item.od_item_id
  INNER JOIN tbl_order
    ON tbl_order.od_id = tbl_order_item.od_id
WHERE tbl_order.od_date >= DATE_SUB(now(), INTERVAL 12 MONTH)
    AND test_name = 'Test'
    AND pd_name = 'Product'
    AND od_customer_id = '4'
GROUP BY test_name, YEAR(od_date), MONTH(od_date)ASC

从where子句中删除条件并将其放入选择

对于第二部分,创建一个表月

    id  |   month
-------------------
    1   |   1
    1   |   2
    1   |   3       
    1   |   4       
    1   |   5       
    1   |   6       
    1   |   7       
    1   |   8       
    1   |   9       
    1   |   10      
    1   |   11      
    1   |   12      

现在加入

SELECT
  YEAR( od_date ) AS Year,
  MONTH( od_date ) AS Month,
  test_name,
  pd_name,
  SUM( IF(test_result <= 50 , 1 , 0) ) AS Passed,
  SUM( IF(test_result > 50 , 1 , 0) ) AS Failed
FROM tbl_lab_item
  INNER JOIN tbl_order_item
    ON tbl_order_item.od_item_id = tbl_lab_item.od_item_id
  INNER JOIN tbl_order
    ON tbl_order.od_id = tbl_order_item.od_id
  LEFT JOIN month ON months.month = MONTH(tbl_lab_item.od_date)
WHERE tbl_order.od_date >= DATE_SUB(now(), INTERVAL 12 MONTH)
    AND test_name = 'Test'
    AND pd_name = 'Product'
    AND od_customer_id = '4'
GROUP BY test_name, YEAR(od_date), MONTH(od_date)ASC