SQL: 此创建视图脚本中的语法错误是什么? (初学者)
SQL: What is the syntax error in this Create View script? (Beginner)
我正在尝试创建一个包含两个不同查询的新视图。我是 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 is not a valid input at this position"。我正在使用 SQL Workbench。感谢您的帮助!
编辑:
您的方案已经解决了,感谢您的帮助!!
您可以创建 两个 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`;
您不能像您所做的那样使用两个独立的 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;
希望这对您有所帮助。试一试。
据我所知,您不能在查询中获得多个选择来创建视图。如果您仍然想要两个选择的所有列,请加入这两个表并创建包含必要列的视图,或者您可以根据需要进行联合。
我正在尝试创建一个包含两个不同查询的新视图。我是 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 is not a valid input at this position"。我正在使用 SQL Workbench。感谢您的帮助!
编辑: 您的方案已经解决了,感谢您的帮助!!
您可以创建 两个 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`;
您不能像您所做的那样使用两个独立的 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;
希望这对您有所帮助。试一试。
据我所知,您不能在查询中获得多个选择来创建视图。如果您仍然想要两个选择的所有列,请加入这两个表并创建包含必要列的视图,或者您可以根据需要进行联合。