一个查询上的mysql多个连接

时间:2015-04-02 16:16:46

标签: php mysql

我在数据库中有4个表,我需要连接四个表但我不能再连接两个表

$query = '
SELECT
   movie_title,genre_label,movie_year,director_name,movie_genre,movie_director,movie_actor
FROM
   general LEFT JOIN genre ON movie_genre = genre_id AND
   general LEFT JOIN movie_directors ON movie_director = director_id
WHERE
   general.movie_genre = genre.genre_id AND
   general.movie_director = movie_directors.director_id
ORDER BY
  movie_year';

我的浏览器显示此错误:

Unknown column 'general' in 'on clause'

请帮助我,Thankes

3 个答案:

答案 0 :(得分:0)

您不必在联接之间插入AND并重复您要查询的表。 您的查询应该是这样的:

$query = '
SELECT
movie_title,genre_label,movie_year,director_name,movie_genre,movie_director,movie_actor
FROM
  general 
  LEFT JOIN genre ON movie_genre = genre_id 
  LEFT JOIN movie_directors ON movie_director = director_id
ORDER BY
  movie_year';

答案 1 :(得分:0)

它不起作用?

SELECT movie_title,genre_label,movie_year,director_name,movie_genre,movie_director,movie_actor
FROM
   general 
   LEFT JOIN genre ON general.movie_genre = genre.genre_id
   LEFT JOIN movie_directors ON general.movie_director = movie_directors.director_id
ORDER BY
  movie_year';

答案 2 :(得分:0)

试试这个:

SELECT
movie_title,genre_label,movie_year,
director_name,movie_genre,movie_director,movie_actor
FROM general 
LEFT JOIN genre ON general.movie_genre = genre.genre_id 
LEFT JOIN movie_directors ON general.movie_director = movie_directors.director_id
ORDER BY movie_year

您的JOIN / WHERE语法不正确。在您的情况下不需要WHERE子句。此外,最好在SELECT和ORDER BY语句中使用表别名指定列名。

注意:以上查询语句基于问题中提供的信息。如果OP在问题中添加表结构信息,则查询可以更改,因为不清楚第二次JOIN应该在哪些表上发生。