我正在为作业的数据库创建一个视图,
我的代码中仍然出现此错误。
CREATE VIEW Rental_View
SELECT Mo.Movie_ID, copy_id, mo.Movie_Name, format_name
FROM Movies AS mo JOIN copies AS co
ON mo.Movie_ID = co.movie_id
JOIN format AS fo
ON co.Format_id = fo.format_id
SELECT c.customer_id, rental_ID, DATEDIFF (day, rental_date, return_date) AS rental_duration' , c.first_name + ' ' + c.last_name AS customer_name
FROM Customers AS c INNER JOIN rentals AS r
ON c.customer_ID = r.customer_ID
在第一个SELECT语句中,它带有下划线红色,我得到错误
“关键字'SELECT'附近的语法不正确。”
我真的不知道如何纠正这个因为我对SQL很新,任何帮助都会非常感激。
提前感谢。
答案 0 :(得分:2)
您需要 AS
关键字:
CREATE VIEW Rental_View AS --- <------ AS needed here
SELECT mo.Movie_ID --- mo, not Mo
, co.copy_id --- play safe and declare which table
, mo.Movie_Name
, fo.format_name --- are these two columns from
FROM Movies AS mo JOIN copies AS co
ON mo.Movie_ID = co.movie_id
JOIN format AS fo
ON co.Format_id = fo.format_id
; --- add a ; if you want to
--- run multiple statements
如果您想将两个SELECT
合并为一个,则需要JOIN
两者中的表格,如下所示:
CREATE VIEW Rental_View AS
SELECT mo.Movie_ID
, co.copy_id
, mo.Movie_Name
, fo.format_name
, c.customer_id
, rental_ID
, DATEDIFF (day, rental_date, return_date) AS rental_duration
, c.first_name + ' ' + c.last_name AS customer_name
FROM Movies AS mo
JOIN copies AS co
ON mo.Movie_ID = co.movie_id
JOIN format AS fo
ON co.Format_id = fo.format_id
JOIN rentals AS r
ON r.movie_id = mo.Movie_id --- just a guess, you have to write this
JOIN Customers AS c
ON c.customer_ID = r.customer_ID
答案 1 :(得分:1)
CREATE VIEW Rental_View
AS
您缺少“AS”关键字
答案 2 :(得分:0)
在此代码rental_duration
之后,您看起来顶部有错误的单引号 -
SELECT c.customer_id, rental_ID, DATEDIFF (day, rental_date, return_date) AS rental_duration' , c.first_name + ' ' + c.last_name AS customer_name
FROM Customers AS c INNER JOIN rentals AS r
ON c.customer_ID = r.customer_ID
尝试
SELECT c.customer_id, rental_ID, DATEDIFF (day, rental_date, return_date) AS rental_duration , c.first_name + ' ' + c.last_name AS customer_name
FROM Customers AS c INNER JOIN rentals AS r
ON c.customer_ID = r.customer_ID
答案 3 :(得分:0)
您可以尝试更改
中的第一个选择SELECT Mo.Movie_ID ,copy_id,mo.Movie_Name,format_name
到
SELECT mo.Movie_ID ,copy_id,mo.Movie_Name,format_name
答案 4 :(得分:0)
您发布的代码中存在多个错误。
CREATE VIEW语句需要ASEC关键字作为SELECE语句。你可以这样做:
CREATE VIEW Rental_View
AS
SELECT Mo.Movie_ID, copy_id, mo.Movie_Name, format_name
FROM Movies AS mo JOIN copies AS co
ON mo.Movie_ID = co.movie_id
JOIN format AS fo
ON co.Format_id = fo.format_id
我可以看到另一个SELECT语句,它在视图中没有任何意义。添加第二个SELECT语句将导致错误“关键字'SELECT'附近的语法不正确。”对于第二个SELECT语句。
答案 5 :(得分:0)
尝试:
CREATE VIEW Rental_View
AS
SELECT Mo.Movie_ID, copy_id, mo.Movie_Name, format_name
FROM Movies AS mo JOIN copies AS co
ON mo.Movie_ID = co.movie_id
JOIN format AS fo
ON co.Format_id = fo.format_id
CREATE VIEW Rental_Duration
AS
SELECT c.customer_id, rental_ID, DATEDIFF (day, rental_date, return_date) AS rental_duration , c.first_name + ' ' + c.last_name AS customer_name
FROM Customers AS c INNER JOIN rentals AS r
ON c.customer_ID = r.customer_ID
通常,您不会创建将返回多个结果集的视图。如果这真的是你想要的,那么BEGIN / END块应该可以解决问题(未经测试 - 现在不在任何RDMS之前):
CREATE VIEW Rental_View
AS
BEGIN
SELECT Mo.Movie_ID, copy_id, mo.Movie_Name, format_name
FROM Movies AS mo JOIN copies AS co
ON mo.Movie_ID = co.movie_id
JOIN format AS fo
ON co.Format_id = fo.format_id
SELECT c.customer_id, rental_ID, DATEDIFF (day, rental_date, return_date) AS rental_duration , c.first_name + ' ' + c.last_name AS customer_name
FROM Customers AS c INNER JOIN rentals AS r
ON c.customer_ID = r.customer_ID
END