2013-09-25 54 views
0

下面是查詢我試圖運行它正在工作,但現在有一個原因,我不知道它現在放棄了。Mysql查詢沒有收到任何數據

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; 
+2

難道你想上'films.movi​​e_id = reviewed.movi​​e_id'加盟? – andrewsi

回答

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 
+1

不管怎樣,我都要打敗它,但+1提到了關鍵。 – Demonslay335

0

由於@andrewsi提到,看來你對條款不正確。您正嘗試將電影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 
相關問題