MYSQL查询表连接提供重复的行

时间:2016-03-11 12:29:27

标签: mysql sql

我有表movie,movie_client,language_movie,language,subtitle_movie和subtitle

我想选择所有带有字幕和语言的电影,其中client_id(在movie_client中)为1

        SELECT * FROM movie 
        LEFT OUTER JOIN movie_client 
        ON movie.movie_id = movie_client.movie_client_id 
        LEFT OUTER JOIN client
        ON movie_client.client_movie_id = client.client_id

        LEFT OUTER JOIN language_movie
        ON movie.movie_id = language_movie.movie_id
        LEFT OUTER JOIN language
        ON language_movie.language_id = language.language_id

        LEFT OUTER JOIN subtitle_movie
        ON movie.movie_id = subtitle_movie.movie_id
        LEFT OUTER JOIN subtitle
        ON subtitle_movie.subtitle_id = subtitle.subtitle_id

        WHERE client.client_id=1

这确实是工作原因我得到了重复的行我尝试了内部连接作为膨胀,但它只是不会工作。任何人都可以帮我解决正确的问题吗?

2 个答案:

答案 0 :(得分:1)

试一试:

    SELECT distinct m.movie_id, lm.language_id, sm.subtitle_id FROM movie m
    INNER JOIN movie_client mc
    ON m.movie_id = mc.movie_client_id 
    INNER JOIN client
    ON mc.client_movie_id = c.client_id

    INNER JOIN lm
    ON m.movie_id = lm.movie_id
    INNER JOIN language l
    ON lm.language_id = l.language_id

    INNER JOIN subtitle_movie sm
    ON m.movie_id = sm.movie_id
    INNER JOIN JOIN subtitle s
    ON sm.subtitle_id = s.subtitle_id

    WHERE client.client_id=1

答案 1 :(得分:0)

这个

    FROM movie 
    LEFT OUTER JOIN movie_client 
    ON movie.movie_id = movie_client.movie_client_id 
    LEFT OUTER JOIN client
    ON movie_client.client_movie_id = client.client_id

应该是INNER JOIN,你不需要所有的电影

    SELECT * FROM movie 
    INNER JOIN movie_client 
    ON movie.movie_id = movie_client.movie_client_id 
    INNER JOIN client
    ON movie_client.client_movie_id = client.client_id

    LEFT OUTER JOIN language_movie
    ON movie.movie_id = language_movie.movie_id
    LEFT OUTER JOIN language
    ON language_movie.language_id = language.language_id

    LEFT OUTER JOIN subtitle_movie
    ON movie.movie_id = subtitle_movie.movie_id
    LEFT OUTER JOIN subtitle
    ON subtitle_movie.subtitle_id = subtitle.subtitle_id

    WHERE client.client_id=1

检查this to understand difference INNER and OUTER JOIN