2013-03-05 81 views
1

我有以下看法,我需要從Oracle移植到MySQL。這在Oracle中完美工作,但由於subquery cannot exist in a view錯誤,不適用於MySQL。我怎樣才能轉換這使它在MySQL中工作?如果我想就此再提出一個看法,它會對性能產生怎樣的影響?這是可取的嗎?如果是這樣,我該怎麼做?如果第二個視圖不是一個好主意,如何轉換?謝謝你的幫助!MySQL查看子查詢

CREATE OR REPLACE VIEW view_name (var1, var2, var3, var4, var5) AS 
SELECT SUM(A.var1a) var1, 
     SUM(A.var2a) var2, 
     SUM(A.var3a) var3, 
     SUM(A.var4a) var4, 
     SUM(A.var5a) var5 
FROM (SELECT CASE columnx when 'abc' then COUNT(E.ID) end var1a, 
    CASE columnx when 'def' then COUNT(E.ID) end var2a, 
    CASE columnx when 'ghi' then COUNT(E.ID) end var3a, 
    CASE columnx when 'jkl' then COUNT(E.ID) end var4a, 
        COUNT(E.ID) var5a 
      FROM <list of tables> 
      WHERE <set of conditions> 
     GROUP BY columnx) A; 

回答

1

您應該能夠使用這個改寫如下:

CREATE OR REPLACE VIEW view_name (var1, var2, var3, var4, var5) AS 
SELECT 
    sum(CASE columnx when 'abc' then 1 else 0 end) var1a, 
    sum(CASE columnx when 'def' then 1 else 0 end) end var2a, 
    sum(CASE columnx when 'ghi' then 1 else 0 end) end var3a, 
    sum(CASE columnx when 'jkl' then 1 else 0 end)end var4a, 
    COUNT(E.ID) var5a 
FROM <list of tables> 
WHERE <set of conditions> 
+0

謝謝!我會給它一個鏡頭,讓你知道發生了什麼。謝謝! :) – CodingInCircles 2013-03-05 20:06:00

+0

明智的答案!我第一次嘗試就像一個魅力工作!謝謝! :) – CodingInCircles 2013-03-05 20:09:46

+0

@CodingInCircles高興地幫助,我很高興它的工作原理。 :) – Taryn 2013-03-05 20:10:27