将2个内部联接查询组合为一个

时间:2011-05-01 05:12:11

标签: mysql inner-join

我试图将3个表的输出合并为一个查询。

以下是表格结构

BL_PLAYERS

player_id int(10)
league_id int(10)
player_name varchar(150)
性别tinyint(3)
initial_hc smallint(6)
total_score int(10)
total_games smallint(6)
current_hc smallint(6)
league_player tinyint(3)

BL_POINTS

series_id int(10) player_id int(10) point smallint(6)

BL_LEAGUES_RANK

series_id int(10) player_id int(10) rankint(6)
last_game smallint(6) true_score smallint(6) 障碍小(6)
total_score smallint(6)

这是我的2个内部连接语句..它们看起来几乎相同......但是我找不到一种方法来组合它,以便第一个sql将返回附加列,即BL_LEAGUES_RANK中的总和(rn.total_score)

SELECT pl.player_id, pl.player_name, pl.gender, pl.league_player, SUM( pt.point ) AS total_points FROM `bl_players` pl  INNER JOIN `bl_points` pt ON pl.player_id = pt.player_id AND series_id =1 GROUP BY player_id ORDER BY total_points DESC 

SELECT pl.player_id, pl.player_name, pl.gender, pl.league_player, SUM(rn.total_score) as total_pinfall FROM `bl_players` pl INNER JOIN `bl_leagues_rank` rn ON pl.player_id = rn.player_id AND series_id =1 GROUP BY player_id ORDER BY total_pinfall DESC

甚至可能吗?提前感谢您对此的任何意见...

1 个答案:

答案 0 :(得分:0)

我认为这应该有用......

SELECT pl.player_id, pl.player_name, pl.gender, 
    pl.league_player, SUM( pt.point ) AS total_points 
FROM bl_players pl, bl_points pt, bl_leagues_rank rn
WHERE pl.player_id = pt.player_id AND series_id =1 
    AND pl.player_id = rn.player_id
GROUP BY player_id ORDER BY total_points DESC