1
SELECT * FROM movie_time AS a,movie_db AS b, movie_actor AS c
WHERE a.Movie_ID = b.Movie_ID AND b.Movie_ID=c.Movie_ID 
      AND c.Movie_ID = a.Movie_ID AND Movie_ID =17

嗨,我正在尝试使用 join 函数来检索正在工作的行,除非当我输入AND Movie_ID =17子句时,我无法检索对应于的行Movie_ID =17

我正在使用 mySQL 工作台。我也可以使用类似的语句来删除检索到的特定行吗?

错误: Error code 1052 Column 'Movie_ID' in where clause is ambiguos

4

4 回答 4

3

您需要指定别名a.Movie_ID =17

SELECT * FROM movie_time AS a,movie_db AS b, movie_actor AS c
WHERE a.Movie_ID = b.Movie_ID AND b.Movie_ID=c.Movie_ID 
      AND c.Movie_ID = a.Movie_ID AND a.Movie_ID =17

对于删除(假设您要从movie_time表中删除):

DELETE
    a
FROM 
    movie_time AS a 
INNER JOIN movie_db AS b
    ON a.Movie_ID = b.Movie_ID
INNER JOIN movie_actor AS c
    ON c.Movie_ID = a.Movie_ID
WHERE 
    a.Movie_ID =17
于 2013-05-16T15:12:50.123 回答
1
SELECT * FROM movie_time AS a,movie_db AS b, movie_actor AS c
WHERE a.Movie_ID = b.Movie_ID AND b.Movie_ID=c.Movie_ID 
      AND c.Movie_ID = a.Movie_ID AND a.Movie_ID =17

您没有为 Movie_ID =17 指定别名,导致歧义。由于所有提到的表都有列 Movie_id ,当使用语句 Movie_id=17 时,SQL 无法确定您所引用的表 Movie_id 列。a.Movie_ID =17如果需要,请Movie_id=17从表 movie_time更改为。

作为旁注,您应该使用 INNER JOIN 来执行此操作。

于 2013-05-16T15:12:44.740 回答
1

关于别名的答案是正确的。除了修复之外,我还想鼓励您使用 ANSI 连接,这将清理您的WHERE子句并让您将过滤逻辑 ( Movie_ID = 17) 与连接逻辑(子句中的所有其他噪音)分开WHERE

这将与上面的查询相同:

SELECT * FROM movie_time AS a
  INNER JOIN movie_db AS b ON a.Movie_ID = b.Movie_ID
  INNER JOIN movie_actor AS c ON b.Movie_ID=c.Movie_ID
  WHERE a.Movie_ID =17

您不需要 的AND c.Movie_ID = a.Movie_ID部分,WHERE因为您已经加入acthrough b

于 2013-05-16T15:17:56.113 回答
0

这会返回比您需要的更多的列,但请考虑这些行的语法......

SELECT * 
  FROM movie_time mt
  JOIN movie_db m
    ON m.Movie_ID = mt.Movie_ID 
  JOIN movie_actor ma
    ON ma.Movie_ID = m.Movie_ID
 WHERE m.Movie_ID = 17;

请注意,任何“无演员”电影将不予退还!

于 2013-05-16T15:23:21.590 回答