Question

I write a sql to select the recored from two table in 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;

and when i want to create the view for this result ,I get this error, and when I search I know the subquery in view is limit.

now I can do is use two view to get this result.

so I want to know how to rewritten this sql without subquery??

Was it helpful?

Solution

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)

OTHER TIPS

A view can be based on a union. So remove the outer select and replace the

where afi=0 and bfi=0;

with the equivalent where clause repeated on both parts of the union:

where IFNULL(a.father_id,0) = 0
and IFNULL(b.father_id, 0) = 0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top