我寫一個SQL選擇從兩個表中的MySQL條記錄視圖不允許FROM子句中的子查詢,如何重寫我的SQL沒有子查詢?
select * from(
select
IFNULL(a.father_id,0) as afi,
IFNULL(b.father_id, 0) as bfi,
IFNULL(a.id,0) AS aid,
IFNULL(b.id,0) AS bid,
a.competitor AS competitor,
IFNULL(a.merchant_order_no,b.merchant_order_no) AS order_no,
IFNULL(a.income,0) AS yingshou,IFNULL(b.income ,0) AS shishou,IFNULL(b.expenditure,0) AS shouxufei,a.apply_time AS apply_time,a.pay_time AS pay_time,b.trading_date AS trading_date,
IFNULL(a.trading_channel,b.trading_channel) as channel,
a.race_id,
a.group_id
from (azim_raceapplicant_export_xls a left join azim_finance_export_xls b on(a.merchant_order_no = b.merchant_order_no ))
union
select
IFNULL(a.father_id,0) as afi,
IFNULL(b.father_id, 0) as bfi,
IFNULL(a.id,0) AS aid,
IFNULL(b.id,0) AS bid,
a.competitor AS competitor,
IFNULL(a.merchant_order_no,b.merchant_order_no) AS order_no,
IFNULL(a.income,0) AS yingshou,IFNULL(b.income ,0) AS shishou,IFNULL(b.expenditure,0) AS shouxufei,a.apply_time AS apply_time,a.pay_time AS pay_time,b.trading_date AS trading_date,
IFNULL(a.trading_channel,b.trading_channel) as channel,
a.race_id,
a.group_id
from (azim_raceapplicant_export_xls a right join azim_finance_export_xls b on(a.merchant_order_no = b.merchant_order_no))
)
as t
where afi=0 and bfi=0;
,當我想爲這個結果來看,我得到這個錯誤,當我搜索我知道在視圖中的子查詢是極限。
現在我可以做的就是使用兩個視圖來獲得這個結果。
所以我想知道如何重寫這個SQL沒有子查詢?
感謝,我執行此查詢,但該查詢,AFI = 0!並顯示bfi!= 0。結果跟我的查詢不一樣.. – chanjianyi
@chanjianyi奇怪。嘗試更新版本。如果沒有幫助,請從內部查詢創建視圖,然後使第二個視圖從您創建的視圖中進行選擇。 –
同樣的結果..是的,我現在創建兩個視圖.. – chanjianyi