我写了一条sql来从Mysql中的两个表中选择记录:
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;
当我想为此结果创建 View 时,我收到此错误,并且当我搜索时我知道 View 中的子查询是有限的。
现在我能做的就是使用两个 View 来获得这个结果。
所以我想知道如何在没有子查询的情况下重写这个sql??
最佳答案
您的外部查询是多余的。尝试:
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);
或者,您可以从内部查询创建 View :
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 ))
)
然后
另一个,从中选择:
create view V2 as (select * from V1 where afi=0 and bfi=0)
关于mysql - View 不允许在 FROM 子句中使用子查询,如何在没有子查询的情况下重写我的 SQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17607187/