从一个表中选择与另一个表重合的数据

时间:2013-09-10 19:35:37

标签: php mysql sql database

如何显示电影的评论?

这是我的查询 -

$query = "SELECT movies.movie_id, movies.movie_title, 
                    FROM movies
                    INNER JOIN reviews
                    ON reviews.review_id=reviews.fk_movie_review";




CREATE TABLE IF NOT EXISTS `movies` (
  `movie_id` int(4) NOT NULL AUTO_INCREMENT,
  `movie_title` varchar(100) NOT NULL,
  `actor` varchar(100) NOT NULL,
  `fk_movie_ratings` int(2) NOT NULL,
  PRIMARY KEY (`movie_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--


INSERT INTO `movies` (`movie_id`, `movie_title`, `actor`, `fk_movie_ratings`) VALUES
(1, 'Batman Begins', 'Christian Bale', 5);



CREATE TABLE IF NOT EXISTS `ratings` (
  `rating_id` int(2) NOT NULL,
  `rating_name` varchar(50) NOT NULL,
  PRIMARY KEY (`rating_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



INSERT INTO `ratings` (`rating_id`, `rating_name`) VALUES
(1, 'Excellent'),
(2, 'Brilliant'),
(3, 'Good'),
(4, 'Ok'),
(5, 'Terrible');



--
-- Table structure for table `reviews`
--

CREATE TABLE IF NOT EXISTS `reviews` (
  `review_id` int(4) NOT NULL AUTO_INCREMENT,
  `movie_review` mediumtext NOT NULL,
  `fk_movie_review` int(4) NOT NULL,
  PRIMARY KEY (`review_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;



INSERT INTO `reviews` (`review_id`, `movie_review`, `fk_movie_review`) VALUES
(1, 'Wicked', 1);

3 个答案:

答案 0 :(得分:1)

$query = "SELECT movies.movie_id, movies.movie_title, reviews.*
                    FROM movies
                    INNER JOIN reviews
                    ON movies.movie_id=reviews.fk_movie_review";

加入条件不参考电影表?

在Thomas Andrews发现

时,你还需要更多专栏

答案 1 :(得分:1)

您的联接存在问题,您在ON子句中指定了review子句中的两列,但您尚未加入电影表

SELECT movies.movie_id, movies.movie_title,reviews.movie_review
FROM movies
INNER JOIN reviews
ON movies.movie_id=reviews.fk_movie_review

答案 2 :(得分:0)

试试这个:

SELECT MOVIES.MOVIE_ID, 
       MOVIES.MOVIE_TITLE, 
       REVIEWS.MOVIE_REVIEW 
FROM   MOVIES 
       LEFT JOIN REVIEWS 
              ON REVIEWS.FK_MOVIE_REVIEW = MOVIES.MOVIE_ID