我真的是新手用我的sql和我试图创建一些视图但是mysql大喊大叫错误
错误1349(HY000):视图的SELECT包含FROM中的子查询 条款
如何在FROM条件中删除子查询并在视图中获得相同的结果?
SELECT actual,
curr,
CASE WHEN actual > anterior THEN 'raise'
WHEN actual < anterior THEN 'drop' ELSE 'nothing'
END as 'status'
FROM (
SELECT o.i_price as actual, o.i_currency as curr,
(
SELECT i.i_price
FROM Info i
WHERE i.i_article_id = 1
AND i.i_insert < o.i_insert
ORDER BY i.i_insert DESC LIMIT 1
) AS anterior
FROM Info o
WHERE o.i_article_id = 1
ORDER BY o.i_insert
DESC LIMIT 1 ) as q
答案 0 :(得分:0)
在创建视图时不能使用dinamica子查询,因此您应该为子查询创建正确的视图
create view my_q as
SELECT o.i_price as actual, o.i_currency as curr,
(
SELECT i.i_price
FROM Info i
WHERE i.i_article_id = 1
AND i.i_insert < o.i_insert
ORDER BY i.i_insert DESC LIMIT 1
) AS anterior
FROM Info o
WHERE o.i_article_id = 1
ORDER BY o.i_insert
DESC LIMIT 1
并在顶视图中调用视图
create view my_top_view as
SELECT actual,
curr,
CASE WHEN actual > anterior THEN 'raise'
WHEN actual < anterior THEN 'drop' ELSE 'nothing'
END as 'status'
FROM my_q;
有了这个性能将受到影响..但要克服错误,你不能使用子查询动态..如果你可以肯定更好地重写查询。
答案 1 :(得分:0)
View的select包含一个子查询错误:你可以解决这个问题。使用mysql mysqlworkbench连接mysql DB。并从mysqlworkbench运行create view脚本。它会起作用