0

这是我尝试运行的查询,它正在运行,但现在由于某种原因,我无法弄清楚它现在已经放弃了。

SELECT films.movie_title, films.rating, films.actor, reviewed.review
      FROM films
      INNER JOIN reviewed
      ON films.movie_id=reviewed.review_id

在浏览表格时,我遇到了这个。认为这可能是问题所在。

ALTER TABLE `reviewed`
  ADD CONSTRAINT `reviewed_ibfk_1` FOREIGN KEY (`movie_id`) REFERENCES `films` (`movie_id`),
  ADD CONSTRAINT `reviewed_ibfk_2` FOREIGN KEY (`movie_id`) REFERENCES `films` (`movie_id`) ON DELETE CASCADE;

--
-- Table structure for table `films`
--

CREATE TABLE IF NOT EXISTS `films` (
  `movie_id` int(4) NOT NULL AUTO_INCREMENT,
  `movie_title` varchar(100) NOT NULL,
  `actor` varchar(100) NOT NULL,
  `rating` varchar(20) NOT NULL,
  PRIMARY KEY (`movie_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;

--
-- Dumping data for table `films`
--

INSERT INTO `films` (`movie_id`, `movie_title`, `actor`, `rating`) VALUES
(22, 'Batman Begins', 'Bale', 'Good');

-- --------------------------------------------------------

--
-- Table structure for table `reviewed`
--

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

--
-- Dumping data for table `reviewed`
--

INSERT INTO `reviewed` (`review_id`, `review`, `movie_id`) VALUES
(1, 'Really good film.', 22);

--
ALTER TABLE `reviewed`
  ADD CONSTRAINT `reviewed_ibfk_1` FOREIGN KEY (`movie_id`) REFERENCES `films` (`movie_id`),
  ADD CONSTRAINT `reviewed_ibfk_2` FOREIGN KEY (`movie_id`) REFERENCES `films` (`movie_id`) ON DELETE CASCADE;
4

2 回答 2

5

看起来您正在加入两个主键 (films.movi​​e_id=reviewed.review_id),而不是一个主键和一个外键 (films.movi​​e_id=reviewed.movi​​e_id)。

尝试以下方法:

SELECT films.movie_title, films.rating, films.actor, reviewed.review
      FROM films
      INNER JOIN reviewed
      ON films.movie_id=reviewed.movie_id
于 2013-09-25T19:50:55.033 回答
0

正如@andrewsi 提到的,您的 ON 子句似乎不正确。您正在尝试将电影 ID 与评论的 ID 相匹配,这绝不应该被认为是真实的。当 INNER JOIN 无法从 ON 子句正确匹配时,它通常不会给出任何结果。

试试这个查询:

SELECT films.movie_title, films.rating, films.actor, reviewed.review
FROM films
INNER JOIN reviewed
    ON films.movie_id=reviewed.movie_id
于 2013-09-25T19:52:00.117 回答