我一直在制作一个显示评分阶梯的网站,它处理所有玩家比赛的比赛。附加链接
我现在所拥有的是下表,但是当我想要选择一个玩家的所有匹配时,我必须包含“或”语句,并且对于所有具有玩家名字的列都有索引。所以我设计了一种新方法。
[当前方法]
aoe3_sp_games表
信息:保持打过的所有比赛和与它有关的球员。
Column Type Null Default
game_id char(36) No
map varchar(30) No
date_time int(11) No
length float No
player_count tinyint(4) No
t1_p1 varchar(16) No
t1_p2 varchar(16) No
t1_p3 varchar(16) No
t1_p4 varchar(16) No
t2_p1 varchar(16) No
t2_p2 varchar(16) No
t2_p3 varchar(16) No
t2_p4 varchar(16) No
t1_p1_rating smallint(4) No
t1_p2_rating smallint(4) No
t1_p3_rating smallint(4) No
t1_p4_rating smallint(4) No
t2_p1_rating smallint(4) No
t2_p2_rating smallint(4) No
t2_p3_rating smallint(4) No
t2_p4_rating smallint(4) No
t1_p1_pr float(6,2) No
t1_p2_pr float(6,2) No
t1_p3_pr float(6,2) No
t1_p4_pr float(6,2) No
t2_p1_pr float(6,2) No
t2_p2_pr float(6,2) No
t2_p3_pr float(6,2) No
t2_p4_pr float(6,2) No
t1_p1_civ char(2) No
t1_p2_civ char(2) No
t1_p3_civ char(2) No
t1_p4_civ char(2) No
t2_p1_civ char(2) No
t2_p2_civ char(2) No
t2_p3_civ char(2) No
t2_p4_civ char(2) No
[新方法]
aoe3_sp_matches表
信息:包含所有比赛打过。
Column Type Null Default
match_id int(10) No
match_guid char(36) No
map varchar(30) No
date_time int(11) No
length float No
player_count tinyint(4) No
aoe3_sp_match_players表
信息:容纳了与从#Matches_table比赛的球员。
Column Type Null Default Links to
match_id int(10) No aoe3_sp_matches -> match_id
player_id int(10) No eso_players -> player_id
team tinyint(4) No
player_name varchar(16) No
player_rating smallint(4) No
player_pr float(6,2) Yes NULL
player_civ char(2) No
我需要的是获得玩家使用#Match_Players_table玩过的所有匹配。 然后将比赛ID加入参与比赛的球员。
任何比赛最多只能有8名选手(4vs4)和至少2名(1vs1)选手。
我正在使用Player 1570进行测试,因为他看起来非常适合没有太多比赛的情况。
我读过的地方不要使用不超过2个连接,否则它可能会降低性能。 我的网站处理大约5000〜3天的比赛和17000〜行的玩家,这些数字是基于新方法。
我的mysql版本是:5.1.61。
我的网站:http://exciple.com/forum/player.php?n=yosimasa&s=m
SQL小提琴:http://sqlfiddle.com/#!2/0b2245/3
结果类型一个
基本上将所有日e球员属于那一场比赛的信息,就像方法一一样。我确实试图获得像这样的结果,但是我在加入多于一行的连接时遇到困难。我知道这需要很多连接,所以不是理想的方式。
game_id map date_time length player_count t1_p1 t1_p2 t1_p3 t1_p4 t2_p1 t2_p2 t2_p3 t2_p4 t1_p1_rating t1_p2_rating t1_p3_rating t1_p4_rating t2_p1_rating t2_p2_rating t2_p3_rating t2_p4_rating t1_p1_pr t1_p2_pr t1_p3_pr t1_p4_pr t2_p1_pr t2_p2_pr t2_p3_pr t2_p4_pr t1_p1_civ t1_p2_civ t1_p3_civ t1_p4_civ t2_p1_civ t2_p2_civ t2_p3_civ t2_p4_civ
43f9499f-870d-47c0-8a05-47554e349698 great plains 1413510540 1451 4 greenandugly Jomp17 Armykid913 FUHAHAHA 1616 1616 0 0 1584 1571 0 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 FR OT OT BR
结果两型
所以我一直在使用查询瞎搞,这里是我得到了什么至今。
执行两个查询
[大游戏]
SELECT aoe3_sp_matches.* FROM aoe3_sp_matches
right JOIN `aoe3_sp_match_players`
ON `aoe3_sp_match_players`.`match_id`=`aoe3_sp_matches`.`match_id`
WHERE player_id = 1570
返回播放器1570的所有比赛能正常工作。
[第二个用于比赛的选手]
SELECT aoe3_sp_match_players.* FROM aoe3_sp_match_players
right JOIN `aoe3_sp_matches`
ON `aoe3_sp_match_players`.`match_id`=`aoe3_sp_matches`.`match_id`
WHERE aoe3_sp_match_players.player_id = 1570
从aoe3_sp_match_players返回数据,与where子句满足。不返回所有玩家,但只返回where子句中的玩家。
SELECT aoe3_sp_match_players.* FROM aoe3_sp_match_players
WHERE aoe3_sp_match_players.match_id in
(SELECT aoe3_sp_match_players.match_id
FROM aoe3_sp_match_players
WHERE player_id = 1570)
我设法获取数据我想要的方式,但使用子查询,这是慢于我的数据库,而不是一个surprize那里的联接。
编辑:我找到一种方式来获得使用在外面连接结果两型的第二部分,它是一样快的where子句加盟之前放置。
Select
aoe3_sp_match_players.match_id,
aoe3_sp_match_players.team,
aoe3_sp_match_players.player_name,
aoe3_sp_match_players.player_rating,
aoe3_sp_match_players.player_pr,
aoe3_sp_match_players.player_civ
From
(Select
aoe3_sp_match_players.match_id
From
aoe3_sp_match_players
Where
aoe3_sp_match_players.player_id = 1570
Limit 30) aoe3_sp_matches Left Join
aoe3_sp_match_players On aoe3_sp_match_players.match_id =
aoe3_sp_matches.match_id
Order By
aoe3_sp_match_players.match_id Desc
你认为获得结果的最好方法是什么?
你有没有写过一个工作'JOIN'? – 2014-10-17 02:18:42
是的一个基本的:SELECT'aoe3_sp_civs_overall'.'player_id','aoe3_sp_civs_overall'.'BR_used','aoe3_sp_overall'.'rating' FROM'aoe3_sp_overall' INNER JOIN'aoe3_sp_civs_overall' ON'aoe3_sp_civs_overall'.'player_id' ='aoe3_sp_overall'.'player_id' ORDER BY'aoe3_sp_overall'.'rating' DESC LIMIT 0,30; – Antonio 2014-10-17 02:21:20
显然最好使用规范化表格。你还没有在那里。您需要在**球员**中只有关于球员本身的信息,然后创建一个单独的“*链接*”表** PlayersInMatches **您将与match_id匹配的球员匹配项(无法使用盘)'player_id'。所以三个表格需要两个简单的'INNER JOIN'。 – 2014-10-17 02:30:09