mysql中的复杂连接查询

时间:2012-12-11 13:54:43

标签: mysql join

每个人,

我真的需要你的帮助。我必须获得所有用户的列表(不同):   - 在比赛中进行现场签到(checkins.ctype ='live')   - 他们最喜欢的球队(见fanusers_teams)   - 赢得了3个更多的进球差距。

最喜欢的团队可能是info_matches.team_id1或info_matches.team_id2,甚至两者都可以。

这是涉及的表格的一个小设计:

design

我尝试过的,80%的工作(所以它没有:(),因为它返回一些用户正确(他们喜欢团队3+目标差异),但也返回没有收藏的用户在这种情况下团队。我认为他们被归还是因为他们已经为一场比赛进行了现场签到,其中一支球队或另一支球队以3+的目标获胜。

这是我的问题:

SELECT DISTINCT 
    f.id 
FROM 
    fanusers f 
LEFT JOIN 
    checkins c ON f.id = c.fanuser_id 
LEFT JOIN 
    info_matches m ON m.id = c.match_id 
WHERE 
    c.ctype = 'live' AND
(
        m.team_id1 IN(
                SELECT DISTINCT
                    m1.team_id1
                FROM
                    info_matches m1
                RIGHT JOIN 
                                        fanusers_teams ft ON m1.team_id1 = ft.team_id
                RIGHT JOIN 
                                        fanusers f ON f.id = ft.fanuser_id
                WHERE
                    m1.pointsteam1 - m1.pointsteam2 >= 3
                  )
        OR 

        m.team_id2 IN(
                SELECT DISTINCT
                    m2.team_id2
                    FROM
                    info_matches m2
                RIGHT JOIN 
                                        fanusers_teams ft ON m2.team_id2 = ft.team_id
                RIGHT JOIN 
                                        fanusers f ON f.id = ft.fanuser_id
                WHERE
                    m2.pointsteam2 - m2.pointsteam1 >= 3
                  )
)

如果有人成功解决了这个问题,我还会很感激我做错了什么。

感谢。

2 个答案:

答案 0 :(得分:2)

这样的事情应该有效:

SELECT  DISTINCT f.id 

FROM    fanusers f 

        JOIN checkins c 
        ON f.id = c.fanuser_id 

        JOIN fanusers_teams ft
        ON f.id = ft.fanuser_id

        JOIN info_matches m 
        ON m.id = c.match_id 
        AND 
        (
            (ft.team_id = m.team_id1 AND pointsteam1 - pointsteam2 >= 3) 
        OR 
            (ft.team_id = m.team_id2 AND pointsteam2 - pointsteam1 >= 3)
        )

WHERE   c.ctype = 'live'

答案 1 :(得分:0)

我发现了一种解决查询的方法,但不像Tom那样好。

我的方式:


SELECT DISTINCT
                            f.id AS user_id
                        FROM
                            fanusers f
                        LEFT JOIN
                            checkins c ON f.id = c.fanuser_id
                        LEFT JOIN
                            info_matches m ON m.id = c.match_id
                        WHERE
                            c.ctype = 'live' 
                            AND
                            m.matchisfinished = 1 
                            AND
                            c.fanuser_id NOT IN ( SELECT DISTINCT
                                                                            f1.id
                                                                        FROM
                                                                            fanusers f1
                                                                        LEFT JOIN
                                                                            fanusers_stickers fs
                                                                        ON f1.id = fs.fanuser_id
                                                                        WHERE
                                                                            fs.sticker_id = 35
                                                                    ) 
                            AND
                            (
                                ( m.team_id1 IN  (  SELECT DISTINCT
                                                                                ft.team_id
                                                                            FROM
                                                                                fanusers_teams ft 
                                                                            INNER JOIN
                                                                                checkins c1
                                                                            ON
                                                                                ft.fanuser_id = c1.fanuser_id
                                                                            WHERE
                                                                                f.id = c1.fanuser_id
                                                                    ) 
                                        AND
                                        m.pointsteam1-m.pointsteam2>=3
                                    )

                                    OR

                                    (
                                         m.team_id2 IN  (   SELECT DISTINCT
                                                                                    ft.team_id
                                                                                FROM
                                                                                    fanusers_teams ft 
                                                                                INNER JOIN
                                                                                    checkins c1
                                                                                ON
                                                                                    ft.fanuser_id = c1.fanuser_id
                                                                                WHERE
                                                                                    f.id = c1.fanuser_id
                                                                        ) 
                                            AND
                                            m.pointsteam2-m.pointsteam1>=3
                                    )
                            )

所以目前,即使我很高兴我自己解决了查询,我也会使用Tom的查询:)。