简体   繁体   English

在同一列上具有联合/联接和两个别名的MySQL

[英]MySQL with union/join and two alias on same column

I'm having problem on getting the quantity value when I want to separate the quantity of group_status 1 and 2. Whether I set the quantity by using the Alias and Union,its only display the first query. 当我想将group_status 1和2的数量分开时,我在获取数量值时遇到问题。是否使用Alias和Union设置数量,它仅显示第一个查询。 Fyi, I did tried using the double left join and set the table to 'a' and 'b', its also not working. Fyi,我确实尝试过使用双左联接并将表设置为'a'和'b',但它也无法正常工作。 Any helps or solution are welcome. 欢迎任何帮助或解决方案。 Thanks! 谢谢!

Below are the table and the query result that I need. 下面是表格和我需要的查询结果。

$a //start date $b //end date $ a //开始日期$ b //结束日期

Query : 查询:

"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";

Requesters: 请求者:

 ------ --------- ------------- --------------- ------------- -------------
| 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 |
 ------  -------- ------------- --------------- ------------- -------------

Items: 项目:

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

Status: 状态:

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

Example of query result: 查询结果示例:

Date: 2016-01-01 to 2016-01-31 日期: 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 |
 ------------  -------------- ------------- ------------- -------------

With a UNION and UNION ALL set operators, the column names for the resultset are specified in the first SELECT . 使用UNIONUNION ALL集合运算符,结果集的列名称在第一个SELECT中指定。 The column names and aliases in subsquent queries are ignored. 后续查询中的列名和别名将被忽略。 (It's not possible to specify two different aliases for one column in the resultset. Each column gets assigned a single name.) (不可能为结果集中的一列指定两个不同的别名。每一列都分配有一个单独的名称。)

But for your result, you don't necessarily need a UNION ALL operation. 但是对于您的结果,您不一定需要UNION ALL操作。

It looks like you want the SUM of the quantity for each status, on the same line. 似乎您希望在同一行上每种状态的数量SUM You can use conditional aggregation. 您可以使用条件聚合。 For example: 例如:

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

If you also want to return rows for items that don't have any related rows in requesters (with group_status 1 or 2), you could use an outer join, and move the predicates in the ON clause... for example: 如果您还想为requesters没有任何相关行的items返回行(使用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

FOLLOWUP 跟进

NOTE: The join predicate r.item_id = i.item_id follows the same pattern as the original OP query. 注意:连接谓词r.item_id = i.item_id遵循与原始OP查询相同的模式。

Demonstration: 示范:

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')
;

First query returns three rows: 第一个查询返回三行:

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  

Second query returns four rows (includes "zeros" row for Ms Office): 第二个查询返回四行(包括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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM