要按照所示的方式组合查询结果,请在外部查询中使用带有排序的UNION
:
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
子句,因为它们似乎是不必要的。
GROUP BY
?除此之外,你是否实际上会得到多条记录?能否向我们展示源表的完整模式以及使用GROUP BY
返回的数据? - MatBailie