我正在创建一个电影数据库,我想我已经完成了它的设计,但我的结果并没有像我希望的那样回归。我现在只有2个电影细节。我试图让它停止显示重复的信息。
你能看看并提供反馈吗?
感谢您的帮助。
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50525
Source Host : localhost
Source Database : MovieDB
Target Server Version : 50525
File Encoding : utf-8
Date: 09/12/2013 22:06:21 PM
*/
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `FORMAT`
-- ----------------------------
DROP TABLE IF EXISTS `FORMAT`;
CREATE TABLE `FORMAT` (
`ID` int(10) NOT NULL AUTO_INCREMENT,
`Formats` varchar(10) NOT NULL DEFAULT '',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `FORMAT_ID`
-- ----------------------------
DROP TABLE IF EXISTS `FORMAT_ID`;
CREATE TABLE `FORMAT_ID` (
`MovieID` int(11) NOT NULL DEFAULT '0',
`FormatID` int(11) NOT NULL DEFAULT '0',
`Num_Discs` int(128) DEFAULT NULL,
`Locations` varchar(128) DEFAULT NULL,
PRIMARY KEY (`MovieID`,`FormatID`),
KEY `FormatID` (`FormatID`),
CONSTRAINT `FORMAT_ID_ibfk_1` FOREIGN KEY (`MovieID`) REFERENCES `MOVIE` (`ID`),
CONSTRAINT `FORMAT_ID_ibfk_2` FOREIGN KEY (`FormatID`) REFERENCES `FORMAT` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `GENRE`
-- ----------------------------
DROP TABLE IF EXISTS `GENRE`;
CREATE TABLE `GENRE` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Genres` varchar(50) NOT NULL DEFAULT '',
`Descriptions` varchar(128) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `GENRE_ID`
-- ----------------------------
DROP TABLE IF EXISTS `GENRE_ID`;
CREATE TABLE `GENRE_ID` (
`MovieID` int(11) NOT NULL DEFAULT '0',
`GenreID` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`MovieID`,`GenreID`),
KEY `GenreID` (`GenreID`),
CONSTRAINT `GENRE_ID_ibfk_2` FOREIGN KEY (`GenreID`) REFERENCES `GENRE` (`ID`),
CONSTRAINT `GENRE_ID_ibfk_1` FOREIGN KEY (`MovieID`) REFERENCES `MOVIE` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `MOVIE`
-- ----------------------------
DROP TABLE IF EXISTS `MOVIE`;
CREATE TABLE `MOVIE` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Titles` varchar(128) NOT NULL DEFAULT '',
`Alt_Titles` varchar(128) DEFAULT NULL,
`Types` varchar(10) NOT NULL DEFAULT '',
`Synopsis` varchar(128) DEFAULT NULL,
`Images` varchar(128) DEFAULT NULL,
`Num_Eps` int(10) DEFAULT '0',
`Catagories` varchar(50) NOT NULL DEFAULT '',
`Duration` time DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `RENT`
-- ----------------------------
DROP TABLE IF EXISTS `RENT`;
CREATE TABLE `RENT` (
`MovieID` int(11) NOT NULL DEFAULT '0',
`Rents` varchar(3) NOT NULL DEFAULT '',
`Who` varchar(128) NOT NULL DEFAULT '',
`Note` varchar(128) DEFAULT NULL,
PRIMARY KEY (`MovieID`,`Rents`,`Who`),
CONSTRAINT `RENT_ibfk_1` FOREIGN KEY (`MovieID`) REFERENCES `MOVIE` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS = 1;
SQL:
select distinct Titles, Types, Num_Eps, Catagories, Duration, Formats, Num_Discs, Genres
from MOVIE, FORMAT, FORMAT_ID, GENRE, GENRE_ID
where MOVIE.ID=GENRE_ID.MovieID
AND GENRE_ID.GenreID=GENRE.ID
and MOVIE.ID=FORMAT_ID.MovieID
AND FORMAT_ID.FormatID=FORMAT.ID
order by Titles;
这是我的结果:
Titles,Types,Num_Eps,Catagories,Duration,Formats,Num_Discs,Genres
Ghost in the Shell,Movie,1,Anime,01:23:00,DVD,1,Mecha
Ghost in the Shell,Movie,1,Anime,01:23:00,DVD,1,Police
Ghost in the Shell,Movie,1,Anime,01:23:00,DVD,1,Psychological
Ghost in the Shell,Movie,1,Anime,01:23:00,DVD,1,Sci-Fi
Ghost in the Shell,Movie,1,Anime,01:23:00,DVD,1,Cyberpunk
Ghost in the Shell,Movie,1,Anime,01:23:00,Bluray,1,Mecha
Ghost in the Shell,Movie,1,Anime,01:23:00,Bluray,1,Police
Ghost in the Shell,Movie,1,Anime,01:23:00,Bluray,1,Psychological
Ghost in the Shell,Movie,1,Anime,01:23:00,Bluray,1,Sci-Fi
Ghost in the Shell,Movie,1,Anime,01:23:00,Bluray,1,Cyberpunk
Summer Wars,Movie,1,Anime,01:54:00,DVD,1,Comedy
Summer Wars,Movie,1,Anime,01:54:00,DVD,1,Sci-Fi
Summer Wars,Movie,1,Anime,01:54:00,HD,,Comedy
Summer Wars,Movie,1,Anime,01:54:00,HD,,Sci-Fi
答案 0 :(得分:1)
几点。我会使用完整的连接语法,
FROM MOVIE JOIN GENRE ON MOVIE.ID=GENRE_ID.MovieID
而不是你现在的where子句。
然后你可能想尝试GROUP_CONCAT(expr)将你的类型连接成一行,例如。机甲,警察,心理等...
答案 1 :(得分:0)
更新:感谢Vulcronos我有类型和格式列出正确的,如果我使用
分开类型:
select distinct Titles, Types, Num_Eps, Catagories, Duration, Genres
FROM MOVIE JOIN GENRE_ID ON MOVIE.ID=GENRE_ID.MovieID, GENRE
where GENRE_ID.GENREID=GENRE.ID
order by Titles;
格式:
select distinct Titles, Types, Num_Eps, Catagories, Duration, Formats, Num_Discs, Locations
FROM MOVIE JOIN FORMAT_ID ON MOVIE.ID=FORMAT_ID.MovieID, FORMAT
where FORMAT_ID.FORMATID=FORMAT.ID
order by Titles;
有没有办法将两者合并?
所以我会得到这样的东西:
Ghost in the Shell,Movie,1,Anime,01:23:00,DVD,1,Mecha
Ghost in the Shell,Movie,1,Anime,01:23:00,DVD,1,Police
Ghost in the Shell,Movie,1,Anime,01:23:00,DVD,1,Psychological
Ghost in the Shell,Movie,1,Anime,01:23:00,DVD,1,Sci-Fi
Ghost in the Shell,Movie,1,Anime,01:23:00,Bluray,1,Cyberpunk
Summer Wars,Movie,1,Anime,01:54:00,DVD,1,Comedy
Summer Wars,Movie,1,Anime,01:54:00,HD,,Sci-Fi
它会列出一次类型和格式一次。当然有多种类型的格式将被重复,除非有办法解决这个问题,但它至少会显示所有格式一次以及类型一次。
感谢您的帮助。
答案 2 :(得分:0)
没有人可能会再看这个帖子了,但是我想让未来的观点和帮助我的人找到我在问题中找到的东西。
select DISTINCT m.Title,
group_concat(DISTINCT g.Genre) as Genres,
group_concat(DISTINCT f.Format) as Formats
from MOVIES m
left join MOVIEGENRES mg on m.Code=mg.MovieCode
left join GENRES g on mg.GenCode=g.Code
left join MOVIEFORMATS mf on m.Code=mf.MovieCode
left join FORMATS f on mf.FormCode=f.Code
group by m.Code;