Mysql查询完全加入

时间:2012-11-10 03:07:41

标签: mysql

我的mysql db中有三个表

dc_match_entries with player_id, goals, yellow cards, own goals etc for each match
dc_player player_id, team_id etc etc
dc_team team_is, some stuff etc etc

我也有这个问题

SELECT
    dc_player. *,
    dc_team.name AS team_name,
    dc_team.abbr_name AS team_abbr_name,
    COUNT( dc_match_entry.player_id ) AS played,
    AVG( dc_match_entry.vote ) AS vote_average,
    SUM( dc_match_entry.goal ) AS goal_sum,
    SUM( dc_match_entry.own_goal) AS own_goal_sum,
    SUM( dc_match_entry.vote ) *10
        + SUM( dc_match_entry.goal ) *10
        + SUM( dc_match_entry.r_card ) *-10
        + SUM( dc_match_entry.y_card ) *-5
        + SUM( dc_match_entry.own_goal ) *-10 AS score,
    SUM( dc_match_entry.y_card ) AS y_card_sum,
    SUM( dc_match_entry.r_card ) AS r_card_sum
FROM
    dc_player,
    dc_match_entry,
    dc_team
WHERE 
    dc_player.id = dc_match_entry.player_id AND
    dc_player.team = dc_team.id
GROUP BY
    dc_player.id

可悲的是,这个查询并没有显示在比赛中没有进行任何比赛的球员(因为他们没有dc_match_entry参赛作品)。我找不到一种简单(也可能是清晰的)方式来展示那些玩家(完全加入?)

PS: 这个查询嵌套在一个更大的查询中:

SELECT
    L.id,
    L.role,
    L.first_name,
    L.last_name,
    L.birth_date,
    L.team_name,
    L.team_abbr_name,
    L.team AS team_id,
    L.photo,
    L.email,
    L.played,
    L.vote_average,
    L.goal_sum AS goal,
    L.own_goal_sum AS own_goal,
    L.score +
        IFNULL(L.c_mvp, 0)*15
        + IFNULL(unbeaten, 0)*50*IF(L.role='P', 1, 0) AS score,
    L.y_card_sum AS y_card,
    L.r_card_sum AS r_card,
    IFNULL(L.c_mvp, 0) AS mvp,
    IFNULL(unbeaten, 0) AS unbeaten
FROM (
    SELECT * FROM (
        SELECT
            dc_player. *,
            dc_team.name AS team_name,
            dc_team.abbr_name AS team_abbr_name,
            COUNT( dc_match_entry.player_id ) AS played,
            AVG( dc_match_entry.vote ) AS vote_average,
            SUM( dc_match_entry.goal ) AS goal_sum,
            SUM( dc_match_entry.own_goal) AS own_goal_sum,
            SUM( dc_match_entry.vote ) *10
                + SUM( dc_match_entry.goal ) *10
                + SUM( dc_match_entry.r_card ) *-10
                + SUM( dc_match_entry.y_card ) *-5
                + SUM( dc_match_entry.own_goal ) *-10 AS score,
            SUM( dc_match_entry.y_card ) AS y_card_sum,
            SUM( dc_match_entry.r_card ) AS r_card_sum
        FROM
            dc_player,
            dc_match_entry,
            dc_team
        WHERE 
            dc_player.id = dc_match_entry.player_id AND
            dc_player.team = dc_team.id
        GROUP BY
            dc_player.id
    ) LOL
    LEFT JOIN (
        SELECT
            COUNT(dc_match.mvp) c_mvp,
            mvp AS player_id
        FROM dc_match
        WHERE
            dc_match.mvp IS NOT NULL
        GROUP BY dc_match.mvp
    ) ROFL
    ON ROFL.player_id = LOL.id
    ORDER BY 
        LOL.score DESC
) L
LEFT OUTER JOIN(
    SELECT
        dc_player.id,
        COUNT(dc_match.id) AS unbeaten
    FROM
        dc_match,
        dc_match_entry,
        dc_player
    WHERE
        (
            (dc_player.team=dc_match.host AND
            dc_match.guest_score=0
            ) OR
            (
                dc_player.team=dc_match.guest AND
                dc_match.host_score=0
            )
        ) AND
        dc_player.id = dc_match_entry.player_id AND dc_match.id = dc_match_entry.match_id
    GROUP BY
        dc_player.id
) I
ON
    L.id = I.id
WHERE
    L.team = 2
GROUP BY
    id
ORDER BY
    role DESC, score DESC

1 个答案:

答案 0 :(得分:3)

你需要像这样使用LEFT JOIN

SELECT ...
FROM dc_player
JOIN dc_team ON (dc_player.team = dc_team.id)
LEFT JOIN dc_match_entry ON (dc_player.id = dc_match_entry.player_id)
GROUP BY dc_player.id

这将为每个玩家返回一行,无论他们是否有任何匹配条目。