需要有关创建视图中语法错误的帮助吗

ecbunoof  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(319)

我之前创建了两个视图,如下所示:

CREATE VIEW T22 
AS
    (SELECT anumber, AVG(slevel) 
     FROM SPOSSESSED
     GROUP BY anumber);

CREATE VIEW T23 (anumber, slevel) 
AS
    (SELECT anumber, 0 
     FROM APPLICANT 
     WHERE anumber NOT IN (SELECT anumber FROM SPOSSESSED));

现在,我需要创建一个视图,它是上述两个视图的并集。我试过这样的说法,效果非常好,并且显示出我想要的结果:

SELECT * 
FROM T22 
UNION 
SELECT * 
FROM T23;

但当我尝试为此创建视图时,它有一个语法错误,我似乎无法识别:

CREATE VIEW T24 
AS 
   (SELECT * 
    FROM T22 
    UNION 
    SELECT * 
    FROM T23);
w8f9ii69

w8f9ii691#

列的命名架构存在冲突,无法使用 SELECT * 在这种情况下。但是,您可以更改 anumber 把它化名成另一个名字。来解决问题。
比如:

CREATE VIEW T24
AS
(
        SELECT anumber AS anumber1,
            AVG(slevel)
        FROM T22

        UNION

        SELECT anumber,
            slevel
        FROM T23
        );
piv4azn7

piv4azn72#

表t22和表t23中的数据类型匹配吗?列数匹配吗?请把你得到的错误贴出来。

0dxa2lsx

0dxa2lsx3#

我自己解决了这个问题!
解决方法是去掉括号,语法错误就消失了:

CREATE VIEW T24 AS SELECT* FROM T22 UNION SELECT * FROM T23;

相关问题