3

我有一张主表和两张表,一张是向内表,另一张是 issue_return_broken 表。所有 3 个表都使用 ITEM_CODE(主键)关联。如果我运行以下 3 个查询,

主查询:

 select item_code, item_name , item_spec, item_quantity,item_unitprice,item_value from      
 ven_inv_partmaster
 where item_code ='NAVTES13'

查询一:

select entry_date, quantity_in from ven_inv_inwardmaster    
 where item_code ='NAVTES13'
 group by entry_date,quantity_in

查询2:

 select issue_date, issue_qnty,rtn_qnty,brkn_qnty from ven_inv_ibrmaster_log ibrlog  
   where ibrlog.item_code ='NAVTES13' and issue_dateid !=0
   group by issue_date,issue_qnty,rtn_qnty,brkn_qnty 

查询 3:

  select rtn_date, rtn_qnty,brkn_qnty from ven_inv_ibrmaster_log ibrlog   
   where ibrlog.item_code ='NAVTES13' and issue_dateid =0
   group by rtn_date,rtn_qnty,brkn_qnty 

我得到如下输出,

  item_code item_name   item_spec  item_quantity    item_unitprice  item_value
  NAVTES13    NAVIN         TEST13       175               15.00    2175.00

输出1:

   entry_date               quantity_in
   2012-04-01 00:00:00.000      50
   2012-04-05 00:00:00.000        50

输出 2:

   issue_date             issue_qnty    rtn_qnty    brkn_qnty
   2012-04-02 00:00:00.000    25             0           0
   2012-04-10 00:00:00.000    10             0           0

输出 3:

   rtn_date             rtn_qnty    brkn_qnty
  2012-04-05 00:00:00.000     10            0   
  2012-04-10 00:00:00.000     9         6

我需要将所有这些查询组合成一个查询,并且需要这样的结果集..

 Date        Quantity_Inward   Quantity_Issued   Return_Quantity   Broken_Quantity
  1/4/2012          50                  0                0               0
  2/4/2012           0                 25                0               0
  5/4/2012           0                  0               10               0 
  5/4/2012          50                  0                0               0
  10/4/2012          0                  0                9               6
  10/4/2012          0                 10                0               0

请帮我解决这个问题..

向内和 ibr 主表:

在此处输入图像描述

4

1 回答 1

2

要以所示方式组合查询结果,请在UNION外部查询中使用 with ordering:

SELECT
  DATE_FORMAT(logdate, '%e/%c/%Y') AS `Date`,
  quantity_in AS Quantity_Inward,
  issue_qnty  AS Quantity_Issued,
  rtn_qnty    AS Return_Quantity,
  brkn_qnty   AS Broken_Quantity
FROM (
  select date(entry_date) as logdate, quantity_in,
    0 as issue_qnty, 0 as rtn_qnty, 0 as brkn_qnty
  from ven_inv_inwardmaster
  where item_code ='NAVTES13'
UNION ALL
  select date(issue_date), 0, issue_qnty, rtn_qnty, brkn_qnty
  from ven_inv_ibrmaster_log
  where item_code ='NAVTES13' and issue_dateid != 0
UNION ALL
  select date(rtn_date), 0, 0, rtn_qnty, brkn_qnty
  from ven_inv_ibrmaster_log
  where item_code ='NAVTES13' and issue_dateid  = 0
) AS t
ORDER BY logdate ASC

如果需要,您甚至可以在外部查询中进行聚合(您的示例输出不这样做):

SELECT
  DATE_FORMAT(logdate, '%e/%c/%Y') AS `Date`,
  SUM(quantity_in) AS Quantity_Inward,
  SUM(issue_qnty)  AS Quantity_Issued,
  SUM(rtn_qnty)    AS Return_Quantity,
  SUM(brkn_qnty)   AS Broken_Quantity
FROM (
  ...
) AS t
GROUP BY logdate
ORDER BY logdate ASC

您可以通过组合查询 2 和 3 来稍微提高性能,如下所示:

  select
    date(if(issue_dateid = 0, rtn_date, issue_date)),
    if(issue_dateid = 0, 0, issue_qnty),
    rtn_qnty,
    brkn_qnty
  from ven_inv_ibrmaster_log
  where item_code = 'NAVTES13'

注意我已经GROUP BY从您的查​​询中删除了这些条款,因为您上面的评论表明它们不是必需的。

于 2012-04-30T10:16:27.797 回答