我正在做我的第一份工作在PHP/MySQL &我需要幫助。我有一個主表:MySQL查詢表
CREATE TABLE `m4l_movies` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(250) NOT NULL,
`Rating` int(11) NOT NULL,
`Genre` varchar(250) NOT NULL,
`Actors` varchar(250) NOT NULL,
`UserID` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=115 DEFAULT CHARSET=utf8;
接收用於具有從這些值查找表形式輸入:
CREATE TABLE `m4l_actors` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Actor` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=232 DEFAULT CHARSET=utf8;
CREATE TABLE `m4l_genre` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Genre` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=202 DEFAULT CHARSET=utf8;
CREATE TABLE `m4l_movierating` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Movie_Rating` varchar(250) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
&我已經創建的視圖:
SELECT m4l_movies.ID AS ID,
m4l_movies.Title AS Title,
m4l_movierating.Movie_Rating AS Rating,
m4l_movies.Actors AS Actors,
m4l_movies.Genre AS Genre
FROM m4l_movies
JOIN m4l_movierating ON m4l_movierating.ID = m4l_movies.Rating
INNER JOIN m4l_genre ON m4l_movies.Genre = m4l_genre.ID
INNER JOIN m4l_actors ON m4l_movies.Actors = m4l_actors.ID
ORDER BY m4l_movies.Title
這裏是我得到的結果:
----------------------------------------------------------------|
ID Title Rating Actor Genre |
10 Summer G (10,15,25) (45,115,123) |
1 About You G-1 (63,163,405) (3,16,51) |
5 Dog Years P (45,65,95) (98,163,357) |
----------------------------------------------------------------|
首先,這個視圖應該返回超過200條記錄。其次,我需要知道如何創建一個查找或其他一些方法來將NAME & GENRE轉換回相應的文本值。一些RATING值如何正確執行,但我無法使NAME或GENRE正確執行。我相信這與我加入表格的方式有關,但我無法弄清楚我要出錯的地方。有人請幫助我。
好的嘗試與菲爾建議做出我已經去除了演員跟着一起,從電影的故事&流派創建movies_genre & movies_actors
DROP TABLE IF EXISTS `m4l_movies`;
CREATE TABLE `m4l_movies` (
`ID` int(11) NOT NULL auto_increment,
`Title` varchar(250) NOT NULL,
`Year` float NOT NULL,
`Review` varchar(250) NOT NULL,
`Rating` int(11) NOT NULL,
`Image` varchar(250) NOT NULL,
`Storyline` longtext NOT NULL,
`Director` varchar(250) NOT NULL,
`UserID` int(11) NOT NULL default '1',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `m4l_movie_actor` (
movie_id INT(11),
actor_id INT(11),
PRIMARY KEY (movie_id, actor_id),
FOREIGN KEY (movie_id) REFERENCES m4l_movies (ID),
FOREIGN KEY (actor_id) REFERENCES m4l_actors (ID)
);
CREATE TABLE `m4l_movie_genre` (
movie_id INT(11),
genre_id INT(11),
PRIMARY KEY (movie_id, genre_id),
FOREIGN KEY (movie_id) REFERENCES m4l_movies (ID),
FOREIGN KEY (genre_id) REFERENCES m4l_genre (ID)
);
DROP TABLE IF EXISTS m4l_genre;
CREATE TABLE m4l_genre (
ID int(11) NOT NULL auto_increment,
Genre varchar(250) NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS m4l_actors;
CREATE TABLE m4l_actors (
ID int(11) NOT NULL auto_increment,
Actor varchar(255) NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
但是當我嘗試創建movie_actor OR MOVIE_GENRE我得到
9時29分45秒
CREATE TABLE`m4l_movie_actor`(movie_id INT(11),actor_id INT(11),
PRIMARY KEY(movie_id ,actor_id),
外鍵(movie_id)參考m4l_movies(ID),
外鍵(actor_id)參考m4l_actors(ID))
錯誤代碼:1215無法添加外鍵約束
0.000秒9點四十分56秒CREATE TABLE
m4l_movie_genre
(movie_id INT(11),genre_id INT(11),
PRIMARY KEY(movie_id,genre_id),
外鍵(movie_id)參考m4l_movies(ID),
外鍵(genre_id )參考文獻m4l_genre(ID))
錯誤代碼:1215無法添加外鍵約束
0.016秒
從我可以收集這些,當你有數據類型不匹配僅發生,但我想我擁有所有INT數據類型,所以爲什麼我收到這個錯誤?
決不(我的意思是**從不**)將逗號分隔的外鍵存儲在單個字段中。改爲使用[聯結表](http://en.wikipedia.org/wiki/Junction_table)。 – Phil
菲爾,如果你能解釋什麼和怎麼做,我就是爲了它......我只是想讓這件事情起作用。 – Les
你有沒有打開鏈接的維基文章? – Phil