mysql查询连接语句

时间:2013-04-15 19:14:37

标签: mysql

我在查询中遇到问题。 我需要制作一个页面,用户可以看到他用电影名称,电影院名称,他保留的座位代码所做的所有预订

我达到了这个水平

SELECT member.member_username, show_datetime, movie_name
FROM `member`
JOIN `reservation` ON `reservation`.`member_id` = `member`.`member_id`
JOIN `show` ON `show`.`show_id`= `reservation`.`show_id`
JOIN `movie` ON `movie`.`movie_id` = `show`.`movie_id`
WHERE `reservation`.`member_id`=1

我需要建立一个连接,并从电影桌和座位表和剧院名称的seat_code获取cinema_name 事实上,我需要一个查询来向我提供我整个数据库中几乎所有数据

这是DB的架构 http://imageshack.us/photo/my-images/824/58054944.jpg/

2 个答案:

答案 0 :(得分:1)

JOIN这两个表也是:

SELECT 
  m.member_username, 
  sh.show_datetime, 
  v.movie_name,
  c.cinema_name,
  t.theater_name
FROM member            AS m
INNER JOIN reservation AS r  ON r.member_id  = m.member_id
INNEr JOIN show        AS sh ON sh.show_id   = r.show_id
INNER JOIN movie       AS v  ON v.movie_id   = s.movie_id
INNER JOIN theater     AS t  ON t.theater_id = sh.theater_id
INNER JOIN cinema      AS c  ON c.theater_id = sh.theater_id 
WHERE r.member_id = 1

答案 1 :(得分:1)

继续加入你的表格

SELECT member.member_username, show_datetime, movie_name, c.cinema_name, t.theater_name
FROM `member`
    JOIN `reservation` ON `reservation`.`member_id` = `member`.`member_id`
    JOIN `show` ON `show`.`show_id`= `reservation`.`show_id`
    JOIN `movie` ON `movie`.`movie_id` = `show`.`movie_id`
    JOIN `theater` ON `show`.`theater_id` = `theater`.`theater_id`
    JOIN `cinema` ON `theater`.`cinema_id` = `cinema`.`cinema_id`
    JOIN `seat` ON `show`.`theater_id` = `seat`.`theater_id`
WHERE `reservation`.`member_id`=1