提问者:小点点

sqlite中的表别名而不是mysql中的表别名


我已经将完全相同的数据库表从MYSQL移到SQLite。以下查询在MYSQL中工作

select castkey, frname, lname, rolename from patrons, cast 
WHERE (patrons.personkey=cast.personkey) and cast.showkey=8 
order by cast.roletype, sex desc

但在SQLITE中,我得到一个“Error near'”。语法错误。使用cast到c的表别名,查询工作良好:

select castkey, frname, lname, rolename from patrons, cast as c
WHERE (patrons.personkey=c.personkey) and c.showkey=8 
order by c.roletype, sex desc

Sqlite不喜欢查询的第一个版本的原因是什么?


共2个答案

匿名用户

您使用的是旧的逗号连接语法。(1992年被取代)。

试试这个

select castkey, frname, lname, rolename 
  FROM patrons
  JOIN cast ON patrons.personkey=cast.personkey
 WHERE cast.showkey=8 
 ORDER BY cast.roletype, sex desc

你的别名情况应该会好起来的。

匿名用户

您可以在SQLite关键字列表中找到cast
虽然SQLite允许将大多数关键字用作标识符,但您应该避免使用这些关键字,或者如果使用这些关键字,则将它们括在后勾、方括号或双引号中。
这样做可以:

select castkey, frname, lname, rolename 
from patrons, cast 
where (patrons.personkey=`cast`.personkey) and `cast`.showkey=8 
order by `cast`.roletype, sex desc

但还应使用适当的联接语法:

select castkey, frname, lname, rolename 
from patrons join cast 
on (patrons.personkey=`cast`.personkey) 
where `cast`.showkey=8 
order by `cast`.roletype, sex desc