带有union / join的MySQL和同一列

时间:2016-03-28 03:02:58

标签: mysql sql

当我想分开group_status 1和2的数量时,我在获取数量值时遇到问题。无论我是使用Alias和Union设置数量,它只显示第一个查询。 Fyi,我确实试过使用双左连接并将表格设置为' a'而且' b',它也不起作用。欢迎任何帮助或解决方案。谢谢!

下面是我需要的表格和查询结果。

$ a //开始日期   $ b //结束日期

查询:

"SELECT items.item_name, requesters.item_version, requesters.quantity AS 'approved'
          FROM requesters
          JOIN items ON items.item_id=requesters.item_id 
          WHERE requesters.requested_date >= '$a' AND requesters.requested_date <='$b'
          AND group_status =1
UNION ALL

SELECT items.item_name, requesters.item_version, requesters.quantity AS notapproved
          FROM requesters
          JOIN items ON items.item_id=requesters.item_id 
          WHERE requesters.requested_date >= '$a' AND requesters.requested_date <='$b'
          AND group_status =2";

请求者:

 ------ --------- ------------- --------------- ------------- -------------
| id   |  item_id| item_version| requested_date|     quantity| group_status|
 ------ --------- ------------- --------------- ------------- -------------
|    1 |   2     |        2013 |    2016-01-01 |         100 |           1 |
|    2 |   2     |        2013 |    2016-01-12 |         200 |           2 |
|    3 |   3     |        2007 |    2016-02-04 |         300 |           2 |
|    4 |   3     |        2010 |    2016-03-25 |         400 |           1 |
 ------  -------- ------------- --------------- ------------- -------------

产品:

 ------ -------- ------------- 
| id   | item_id|    item_name| 
 ------ -------- ------------- 
|    1 |    1   |    Ms Office| 
|    1 |    2   | Ms Visio Pro|      
|    2 |    3   | Ms Visio Std|           
 ------ -------- -------------

状态:

 ------ ---------- ------------- 
| id   | status_id|  status_name| 
 ------ ---------- ------------- 
|    1 |      1   | Approved    |      
|    2 |      2   | Not Approved|           
 ------ ---------- -------------

查询结果示例:

日期: 2016-01-01 to 2016-01-31

 ------------- -------------- ------------- ------------- -------------
|   item_name |  item_version|    Approved | Not Approved|        Total|
 ------------- -------------- ------------- ------------- -------------
| Ms Visio Pro|         2013 |         100 |         200 |         300 |
| Ms Visio Std|         2007 |           0 |         300 |         300 |
| Ms Visio Std|         2010 |         400 |           0 |         400 |
 ------------  -------------- ------------- ------------- -------------

1 个答案:

答案 0 :(得分:1)

使用UNIONUNION ALL集合运算符,结果集的列名称在第一个SELECT中指定。将忽略后续查询中的列名和别名。 (无法为结果集中的一列指定两个不同的别名。每列都会分配一个名称。)

但是对于您的结果,您不一定需要进行UNION ALL操作。

看起来您希望同一行中每个状态的数量为SUM。您可以使用条件聚合。例如:

SELECT i.item_name
     , r.item_version
     , SUM(IF(r.group_status=1,r.quantity,0)) AS `Approved`
     , SUM(IF(r.group_status=2,r.quantity,0)) AS `Not Approved`
     , SUM(r.quantity)                        AS `Total`
  FROM requesters r
  JOIN items i
    ON i.item_id = r.item_id
 WHERE r.group_status IN (1,2)
   AND r.requested_date >= ?
   AND r.requested_date <= ?
 GROUP
    BY i.item_name
     , r.item_version

如果您还希望返回itemsrequesters中没有任何相关行(包含group_status 1或2)的行,则可以使用外部联接,然后移动ON子句中的谓词...例如:

SELECT i.item_name
     , r.item_version
     , IFNULL(SUM(IF(r.group_status=1,r.quantity,0)),0) AS `Approved`
     , IFNULL(SUM(IF(r.group_status=2,r.quantity,0)),0) AS `Not Approved`
     , IFNULL(SUM(r.quantity),0)                        AS `Total`
  FROM items i
  LEFT
  JOIN requesters r
    ON r.item_id = i.item_id
   AND r.group_status IN (1,2)
   AND r.requested_date >= ?
   AND r.requested_date <= ?
 GROUP
    BY i.item_name
     , r.item_version

<强>后续

注意:连接谓词r.item_id = i.item_id遵循与原始OP查询相同的模式。

演示:

create table requesters  (id int, item_id int, item_version int
  , requested_date date, quantity int, group_status int)
;
insert into requesters (id, item_id, item_version
  , requested_date, quantity, group_status) values
 ('1','2','2013','2016-01-01','100','1')
,('2','2','2013','2016-01-12','200','2')
,('3','3','2007','2016-02-04','300','2')
,('4','3','2010','2016-03-25','400','1')
;
create table items (id int, item_id int, item_name varchar(12))
;
insert into items (id, item_id, item_name) values
 ('1','1','Ms Office')
,('1','2','Ms Visio Pro')
,('2','3','Ms Visio Std')
;

第一个查询返回三行:

item_name     item_version  Approved  Not Approved   Total
------------  ------------  --------  ------------  ------
Ms Visio Pro          2013       100           200     300
Ms Visio Std          2007         0           300     300
Ms Visio Std          2010       400             0     400  

第二个查询返回四行(包括&#34;零&#34; Ms Office行):

item_name     item_version  Approved  Not Approved   Total
------------  ------------  --------  ------------  ------
Ms Office           (NULL)         0             0       0
Ms Visio Pro          2013       100           200     300
Ms Visio Std          2007         0           300     300
Ms Visio Std          2010       400             0     400