使用UNION ALL

时间:2016-11-03 11:29:40

标签: mysql

尝试使用UNION ALL尝试加入两个查询并尝试创建MYSQL数据库视图。这是显示这个的正确方法吗?这两个查询之间的唯一区别是子查询WHERE Con​​dition和Main WHERE条件,其余两个查询都相同。我想知道它是否是正确的方法,或者是否有任何替代方法来实现这一点。我的查询/视图如下。

create or replace view view_bank_guarantee as

SELECT
view_sales_report.tender_id,
view_sales_report.customer_code,
com_payments.bank_ref_no,

(select
com_receipts.rec_date_of_credit
FROM com_receipts
WHERE com_receipts.rec_main_section ='SBG Cancellation' AND view_sales_report.tender_id = com_receipts.tender_id
) AS BGClosedOn

FROM com_payments
LEFT OUTER JOIN view_sales_report ON com_payments.tender_id = view_sales_report.tender_id AND com_payments.main_section ='Bank Guarantee SD'
LEFT OUTER JOIN com_receipts ON com_payments.tender_id = com_receipts.tender_id
GROUP BY view_sales_report.tender_id
HAVING BGClosedOn IS NULL AND (view_sales_report.tender_id !='') 

UNION ALL  

SELECT 
view_sales_report.tender_id,
view_sales_report.customer_code,
com_payments.bank_ref_no,

(select
com_receipts.rec_date_of_credit
FROM com_receipts
WHERE com_receipts.rec_main_section ='PBG Cancellation' AND view_sales_report.tender_id = com_receipts.tender_id
) AS BGClosedOn

FROM com_payments
LEFT OUTER JOIN view_sales_report ON com_payments.tender_id = view_sales_report.tender_id AND com_payments.main_section ='Bank Guarantee PBG'
LEFT OUTER JOIN com_receipts ON com_payments.tender_id = com_receipts.tender_id
GROUP BY view_sales_report.tender_id
HAVING BGClosedOn IS NULL AND (view_sales_report.tender_id !='')

0 个答案:

没有答案