2013-07-12 73 views
0

我寫一個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沒有子查詢?

回答

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) 
+0

感謝,我執行此查詢,但該查詢,AFI = 0!並顯示bfi!= 0。結果跟我的查詢不一樣.. – chanjianyi

+0

@chanjianyi奇怪。嘗試更新版本。如果沒有幫助,請從內部查詢創建視圖,然後使第二個視圖從您創建的視圖中進行選擇。 –

+0

同樣的結果..是的,我現在創建兩個視圖.. – chanjianyi

0

視圖可以基於工會。因此,除去外部選擇和更換

where afi=0 and bfi=0; 

與where子句重複工會的兩個部分相當於:

where IFNULL(a.father_id,0) = 0 
and IFNULL(b.father_id, 0) = 0 
+0

謝謝,像你說的那樣,整個查詢sql就像@David Jashi?但結果與我的查詢不同 – chanjianyi