简体   繁体   English

Mysql VIEW在FROM条件中删除子查询

[英]MySql VIEW remove subquery in FROM condition

im really newbie with my sql and im trying to create some views buts mysql yell with error 我真的与我的SQL新手,我试图创建一些视图,但MySQL大喊错误

ERROR 1349 (HY000): View's SELECT contains a subquery in the FROM clause 错误1349(HY000):视图的SELECT在FROM子句中包含一个子查询

how can i remove subquery in FROM condition and get the same results in a view? 如何在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

You cannot use dinamica subquery during the creation of a view so you should create a proper view for subquery 您不能在创建视图的过程中使用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

and the call the view in your top view 然后在顶视图中调用该视图

    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;

With this the performance will suffer .. but to overcome the error you must not use subquery dynamics .. if you can is definitely better to rewrite the query . 这样做会降低性能。但是要克服该错误,您一定不能使用子查询动态。

View's select contains a subquery error: You can solve this. 视图的选择包含一个子查询错误:您可以解决此问题。 Use mysql mysqlworkbench to connect mysql DB. 使用mysql mysqlworkbench连接mysql DB。 and run create view script from mysqlworkbench. 并从mysqlworkbench运行create view脚本。 It will work 会工作的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM