Your outer query is redundant. Try:
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)
where
(a.father_id is null or a.father_id=0) and
(b.father_id is null or b.father_id=0);
Alternatively you can create view from inner query:
create view V1 as (
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 ))
)
and after that
another one, selecting from it:
create view V2 as (select * from V1 where afi=0 and bfi=0)