关键字“SELECT”附近的语法不正确。创建视图错误

发布于 2024-12-14 04:54:49 字数 625 浏览 2 评论 0原文

我正在为作业创建数据库视图,

但我在代码中不断收到此错误。

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 还很陌生,任何帮助将不胜感激。

提前致谢。

I am creating a view for a database for an assignment,

i keep getting this error in my code though.

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

In the first SELECT statement it is underlined red and i get the error

"Incorrect syntax near the keyword 'SELECT'."

i literally have no idea how to correct this as i am fairly new to SQL, any help would be much appreciated.

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

回忆追雨的时光 2024-12-21 04:54:49

您需要 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

You need the AS keyword:

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

If you want to combine the two SELECT in one, you need to JOIN the tables from both, with something like this:

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
亚希 2024-12-21 04:54:49
CREATE VIEW Rental_View
AS

您缺少“AS”关键字

CREATE VIEW Rental_View
AS

you are missing the "AS" keyword

£烟消云散 2024-12-21 04:54:49

您看起来在这段代码中的 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

You look top have an erroneous single quote after rental_duration in this code -

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

try

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
但可醉心 2024-12-21 04:54:49

您可以尝试将第一个选择从

SELECT Mo.Movie_ID, copy_id, mo.Movie_Name, format_name

更改为

SELECT mo.Movie_ID, copy_id, mo.Movie_Name, format_name

you can try changing the first select from

SELECT Mo.Movie_ID, copy_id, mo.Movie_Name, format_name

to

SELECT mo.Movie_ID, copy_id, mo.Movie_Name, format_name

难忘№最初的完美 2024-12-21 04:54:49

尝试:

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

Try:

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

Normally you don't create a view that will return more than one result set. If this is truly what you desire then a BEGIN/END block should solve the problem (Untested - not infront of any RDMS right now):

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
忘羡 2024-12-21 04:54:49

您发布的代码中有多个错误。

  1. CREATE VIEW 语句在 SELECE 语句之前需要 AS 关键字。你可以这样做:

    创建视图 Rental_View
    作为
    SELECT Mo.Movie_ID、copy_id、mo.Movie_Name、format_name
    FROM 电影 AS mo JOIN 副本 AS co
    ON mo.Movie_ID = co.movi​​e_id  
    JOIN 格式 AS fo
    ON co.Format_id = fo.format_id
    
  2. 我可以看到另一个 SELECT 语句,这在视图中没有任何意义。添加第二个 SELECT 语句将导致错误“关键字‘SELECT’附近的语法不正确。”对于第二个 SELECT 语句。

There are multiple errors in the code that you have posted.

  1. The CREATE VIEW statement needs AS keyword befor the SELECE statement. You might do it like:

    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
    
  2. And I could see another SELECT statement, that is making no meaning in a view. Adding the second SELECT statement will cause an error "Incorrect syntax near the keyword 'SELECT'." for the second SELECT statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文