我有以下观点,我需要从Oracle移植到MySQL。这在Oracle中完美有效,但由于subquery cannot exist in a view
错误而无法在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;
答案 0 :(得分: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>