SQL:此Create View脚本中的语法错误是什么? (初学者)

时间:2017-12-04 02:22:35

标签: mysql sql select syntax workbench

我正在尝试使用两个不同的查询创建一个新视图。我是SQL的新手,我想知道导致以下代码语法错误的原因是什么:

CREATE VIEW `License_Expiration` AS

/* These queries show the license expiration date for retailers and 
processing facilities, sorted by soonest expiration date */

SELECT `Retailers`.`Retailer_Name`, `Retailers`.`Retail_License_Number`, 
`Retailers`.`Retal_License_Expiration` FROM `mydb2`.`Retailers`
ORDER BY `Retailers`.`Retal_License_Expiration`;

SELECT `Processing Facility`.`Facility_Name`, `Processing 
Facility`.`Facility_License_Num`, `Processing 
Facility`.`Facility_License_Exp` FROM `mydb2`.`Processing Facility`
ORDER BY `Processing Facility`.`Facility_License_Exp`;

语法错误发生在第二个SELECT语句中:“SELECT在此位置不是有效输入”。我正在使用SQL Workbench。任何帮助表示赞赏!

编辑: 你的解决方案已经解决了,谢谢你的帮助!!

3 个答案:

答案 0 :(得分:1)

我认为您无法在查询中获得多个选择来创建视图。如果您仍然想要两个选择的所有列,请加入这两个表并创建包含必要列的视图,或者您可以根据需要建立联合。

答案 1 :(得分:0)

您可以创建两个 Views,如:

CREATE VIEW `License_Expiration_Retailer` AS

/* These queries show the license expiration date for retailers and 
   processing facilities, sorted by soonest expiration date */

SELECT `Retailers`.`Retailer_Name`, 
`Retailers`.`Retail_License_Number`, 
`Retailers`.`Retal_License_Expiration` FROM `mydb2`.`Retailers`
ORDER BY `Retailers`.`Retal_License_Expiration`;


CREATE VIEW `License_Expiration_Processing_Facilities` AS

SELECT `Processing Facility`.`Facility_Name`, `Processing 
Facility`.`Facility_License_Num`, `Processing 
Facility`.`Facility_License_Exp` FROM `mydb2`.`Processing Facility`
ORDER BY `Processing Facility`.`Facility_License_Exp`;

一个 View合并两者:

CREATE VIEW `License_Expiration` AS

/* These queries show the license expiration date for retailers and 
   processing facilities, sorted by soonest expiration date */

SELECT "Retailer" as License_Type, `Retailers`.`Retailer_Name` as Owner_Name, 
`Retailers`.`Retail_License_Number` as License_Number, 
`Retailers`.`Retal_License_Expiration` as Lincense_Expiration_Date FROM `mydb2`.`Retailers`
ORDER BY `Retailers`.`Retal_License_Expiration`;

UNION ALL

SELECT "Processing Facility" as License_Type, 
`Processing Facility`.`Facility_Name` as OwnerName, `Processing 
Facility`.`Facility_License_Num` as License_Number, `Processing 
Facility`.`Facility_License_Exp` as Lincense_Expiration_Date 
FROM `mydb2`.`Processing Facility`
ORDER BY `Processing Facility`.`Facility_License_Exp`;

答案 2 :(得分:0)

您无法像创建一样使用两个独立的SELECT语句创建视图。根据MySQL 5.7文档,视图的语法如下所示。

CREATE
    [OR REPLACE]
    [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
    [DEFINER = { user | CURRENT_USER }]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW view_name [(column_list)]
    AS select_statement
    [WITH [CASCADED | LOCAL] CHECK OPTION]

由于看起来您希望从两种类型(即零售商和设施)获得许可证到期,或许您可以UNION这两个SELECT语句并且有一个CHAR列,指示该记录是针对零售商还是设施。 UNION假定所有列都属于同一类型。

CREATE VIEW `License_Expiration` AS

    /* These queries show the license expiration date for retailers and 
    processing facilities, sorted by soonest expiration date */

    (SELECT `Retailers`.`Retailer_Name`, AS Name,
    `Retailers`.`Retail_License_Number` AS License_Number,
    `Retailers`.`Retal_License_Expiration` AS License_Expiration, 'R'   
    AS Source_Type FROM `mydb2`.`Retailers`)

    UNION

    (SELECT `Processing Facility`.`Facility_Name`, AS Name, 
    `Processing Facility`.`Facility_License_Num` AS License_Number,     
    `Processing Facility`.`Facility_License_Exp` AS License_Expiration,  
    'F' AS Source_Type FROM `mydb2`.`Processing Facility`)

    ORDER BY Name, License Expiration;

希望这会有所帮助。试一试。