SQL sqlite3:靠近“。” : 语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21356420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sqlite3: near "." : syntax error
提问by Billy Wang
I have two tables already created:
我已经创建了两个表:
create table movies(id integer, name text, score integer);
create table cast(movie_id integer, cast_id integer, cast_name text);
I need the first 10 (distinct, alphabetically by cast_name) cast members and their average movie scores, so I tried:
我需要前 10 个(不同的,按 cast_name 的字母顺序排列)演员和他们的平均电影分数,所以我尝试了:
select movie_id,cast_id,cast_name,id,score from cast,movies
where movies.id=cast.movie_id and cast_name in
(select distinct cast_name from cast order by cast_name limit 10);
But then I got an error message: near "." : syntax error
但是后来我收到一条错误消息:在“附近”。: 语法错误
After that, I tried to make it simpler:
在那之后,我试图让它更简单:
select cast_name, score from cast,movies where movies.id=cast.movie_id;
I still got the same error.
我仍然遇到同样的错误。
I guess this might be because '.' is a special command in sqlite3, but cannot figure out how to solve this problem.
我想这可能是因为 '.' 是sqlite3中的一个特殊命令,但不知道如何解决这个问题。
Any help will be appreciated.
任何帮助将不胜感激。
回答by Gordon Linoff
cast
is a reserved word. The list of reserved words is here.
cast
是保留字。保留字列表在这里。
select cast_name, score
from `cast` c join
movies m
on m.id = c.movie_id;
You can escape it using backticks or double quotes. This query uses table aliases to simplify the query and more modern syntax for the join.
您可以使用反引号或双引号将其转义。此查询使用表别名来简化查询和联接的更现代语法。
回答by Billy Wang
You cannot specify a column using . notation just call the columns by their name not the tablename.columnname
您不能使用 指定列。表示法只是按名称调用列而不是 tablename.columnname