这是电影在线商店mi数据库的结构:
这些是用于创建的sql语句
影
CREATE TABLE `movies` (
`id_movie` varchar(4) NOT NULL default '',
`nom_movie` varchar(30) default NULL,
`detall_movie` text,
`precio_movie` varchar(6) default NULL,
`img_rut_movie` varchar(50) default NULL,
PRIMARY KEY (`id_movie`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
generos
CREATE TABLE `genres` (
`id_genre` varchar(3) collate utf8_bin NOT NULL default '',
`nom_genre` varchar(20) collate utf8_bin default NULL,
PRIMARY KEY (`id_genero`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
movie_genero
CREATE TABLE `movie_genre` (
`id_movie` varchar(4) collate utf8_bin default NULL,
`id_genre` varchar(3) collate utf8_bin default NULL,
FOREIGN KEY(id_movie) REFERENCES movies(id_movie),
FOREIGN KEY(id_genero) REFERENCES generos(id_genero)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
例如,我想展示属于“动作”类型的电影:
SELECT nom_movie FROM movies, genres, movie_genre WHERE genres.nom_genre = 'action';
但它显示超过1200个结果!!,即重复36次电影表中托管的36部电影。
我希望你能帮忙!!
答案 0 :(得分:1)
即使是Distinct,这也是一个效率低下的查询。我建议一个适当的内部联接。这应该这样做:
select
m.nom_movie
FROM
movies
inner join
movie_genre mg
on
m.id_movie=mg.id_movie
inner join
genres g
on
mg.id_genre=g.id_genre
where
g.nom_genre = 'action';
答案 1 :(得分:1)
SELECT movies.nom_movie
FROM movies, genres, movie_genre
WHERE
movies.id_movie = movie_genre.id_movie
and movie_genre.id_genre = genre.id_genre
and genres.nom_genre = 'action';
答案 2 :(得分:1)
结构很完美,只需使用此查询。
SELECT nom_movie FROM movies, genres, movie_genre WHERE genres.nom_genre = 'action' AND genres.id_genre= movie_genre.id_genre AND movie_genre.id_movie=movies.id_movie ;