MYSQL LEFT JOIN多个表两次

时间:2011-08-14 17:53:10

标签: php mysql left-join

我有以下MySQL代码:

SELECT * 
FROM b_movies, b_mov_rol_celeb, b_starcast 
LEFT JOIN bb_celebs ON b_mov_rol_celeb.celeb_id = bb_celebs.celeb_id 
  OR b_starcast.celeb_id = bb_celebs.celeb_id 
LEFT JOIN bb_roles ON b_mov_rol_celeb.role_id = bb_roles.role_id 
WHERE b_movies.id = '14';

给出错误:

  

'on clause'中的未知列'b_mov_rol_celeb.celeb_id'

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT m.title_slug, m.title, m.release_date,
       c.celeb_slug, c.celeb_name, 
       r.role_name,
       s.char_name
FROM b_movies m
INNER JOIN b_starcast s ON m.id = s.movie_id
INNER JOIN b_mov_rol_celeb mrc ON m.id = mrc.movie_id
LEFT JOIN bb_celebs c ON mrc.celeb_id = c.celeb_id OR s.celeb_id = c.celeb_id 
LEFT JOIN bb_roles r ON mrc.role_id = r.role_id 
WHERE m.id = '14';

表的顺序在左或右连接中很重要。