0

我写了一个 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;

当我想为这个结果创建视图时,我得到了这个错误,当我搜索时,我知道视图中的子查询是限制的。

现在我可以做的是使用两个视图来获得这个结果。

所以我想知道如何在没有子查询的情况下重写这个sql??

4

2 回答 2

1

您的外部查询是多余的。尝试:

    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);

或者,您可以从内部查询创建视图:

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)
于 2013-07-12T05:14:42.010 回答
0

视图可以基于联合。所以删除外部选择并替换

where afi=0 and bfi=0;

在联合的两个部分都重复了等效的 where 子句:

where IFNULL(a.father_id,0) = 0
and IFNULL(b.father_id, 0) = 0
于 2013-07-12T05:18:08.357 回答