注意:使用DISTINCT
无法解决问题。 (我会解释原因)
这是我的代码
DROP TABLE tmdb_movies; CREATE TABLE tmdb_movies ( tmdb_id INTEGER NOT NULL PRIMARY KEY, movie_title TEXT NOT NULL ); INSERT INTO tmdb_movies (tmdb_id, movie_title) VALUES (1, 'The Dark Knight'); DROP TABLE recommendations; CREATE TABLE recommendations ( recommendations_tmdb_id INTEGER NOT NULL, recommendations_title TEXT NOT NULL, recommendations_vote_average TEXT NOT NULL ); INSERT INTO recommendations (recommendations_tmdb_id, recommendations_title, recommendations_vote_average) VALUES (1, 'The Dark Knight Rises', '7.5'), (1, 'Batman Begins', '7.5'), (1, 'Iron Man', '7.3'), (1, 'The Lord of the Rings: The Return of the King', '8.1'), (1, 'The Lord of the Rings: The The Fellowship of the Ring', '8'), (1, 'The Lord of the Rings: The Two Towers', '7.9'), (1, 'The Matrix', '7.9'), (1, 'Inception', '8'), (1, 'Iron Man 2', '6.6'), (1, 'Captain America: The First Avenger', '6.6'); DROP TABLE cast; CREATE TABLE cast ( cast_tmdb_id INTEGER NOT NULL, cast_name TEXT NOT NULL, cast_character TEXT NOT NULL ); INSERT INTO cast (cast_tmdb_id, cast_name, cast_character) VALUES (1, 'Christian Bale', 'Bruce Wayne / Batman'), (1, 'Michael Caine', 'Alfred Pennyworth'), (1, 'Heath Ledger', 'The Joker'), (1, 'Aaron Eckhart', 'Harvey Dent / Two-Face'), (1, 'Gary Oldman', 'Lt. James Gordon'), (1, 'Maggie Gyllenhaal', 'Rachel Dawes'), (1, 'Morgan Freeman', 'Lucius Fox'), (1, 'Chin Han', 'Lau'), (1, 'Eric Roberts', 'Salvatore Maroni'), (1, 'Monique Gabriela Curnen', 'Ramirez'); SELECT tmdb_movies.movie_title ,GROUP_CONCAT(recommendations.recommendations_title) as recommendations_title ,GROUP_CONCAT(recommendations.recommendations_vote_average) as recommendations_vote_average FROM tmdb_movies LEFT JOIN cast ON cast.cast_tmdb_id=tmdb_movies.tmdb_id LEFT JOIN recommendations ON recommendations.recommendations_tmdb_id=tmdb_movies.tmdb_id Where tmdb_movies.tmdb_id= 1 GROUP BY tmdb_movies.movie_title
如您所见,来自recommendations
表的记录多次显示。但如果我删除此行LEFT JOIN cast ON cast.cast_tmdb_id=tmdb_movies.tmdb_id
然后,它们不会多次显示。所以这一行是主要问题。
我的SQL查询中没有使用强制转换表,只是为了简单起见。但我需要它。
是的,我需要group_concat在我的PHP代码中一起显示记录
为什么DISTINCT
无法解决问题?好吧,有些电影的评分相同,例如:7.5
。因此,并非所有电影的评级都会显示出来。这就是为什么,你可以自己尝试,尝试在小提琴中使用
QL
答案 0 :(得分:1)
你觉得这个结果好吗?
List