为什么我在sqlite3中收到以下错误消息
Error: near line 99: near ".": syntax error
SELECT cast.cast_id, cast.cast_name, AVG(movies.score)
FROM movies
INNER JOIN cast ON cast.movie_id = movies.id
WHERE movies.score >= 40
GROUP BY cast.cast_name
HAVING COUNT(movies.id) >= 2
ORDER BY AVG(movies.score) DESC, cast.cast_name DESC
LIMIT 10;
第99行是第一行(SELECT行)
答案 0 :(得分:4)
cast
是SQLite中的保留字。这对于表或列名来说确实是一个糟糕的选择。保留字列表在documentation中。
如果您不能更改表名,请使用显式别名:
SELECT c.cast_id, c.cast_name, AVG(m.score)
FROM movies m INNER JOIN
"cast" c
ON c.movie_id = m.id
WHERE m.score >= 40
GROUP BY c.cast_id, c.cast_name
HAVING COUNT(m.id) >= 2
ORDER BY AVG(m.score) DESC, c.cast_name DESC;
注意:
GROUP BY
在SELECT
中具有所有未聚合的列。