我正在尝试用未规范化的“摘要”表中的数据填充“testMatch”表(如下所示):
测试匹配表
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| match_id | int(11) | NO | PRI | NULL | |
| match_date | date | YES | | NULL | |
| ground | varchar(50) | YES | MUL | NULL | |
| homeTeam | varchar(100) | YES | MUL | NULL | |
| awayTeam | varchar(100) | YES | MUL | NULL | |
| matchResult | varchar(100) | YES | MUL | NULL | |
| manOfMatch | varchar(30) | YES | | NULL | |
| homeTeam_captain | int(10) | YES | MUL | NULL | |
| homeTeam_keeper | int(10) | YES | MUL | NULL | |
| awayTeam_captain | int(10) | YES | MUL | NULL | |
| awayTeam_keeper | int(10) | YES | MUL | NULL | |
+------------------+--------------+------+-----+---------+-------+
填充match_id-->manOfMatch没有问题-我遇到问题的是“homeTeam_captain”、“homeTeam_keeper”、“awayTeam_captain”和“awayTeam_keeper”。
汇总表
mysql> DESCRIBE SUMMARY;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| matchID | int(11) | NO | PRI | NULL | |
| Test | int(11) | YES | | NULL | |
| matchDate | date | YES | | NULL | |
| Ground | varchar(50) | YES | | NULL | |
| HomeTeam | varchar(100) | YES | | NULL | |
| AwayTeam | varchar(100) | YES | | NULL | |
| matchResult | varchar(50) | YES | | NULL | |
| MarginRuns | int(11) | YES | | NULL | |
| MarginWickets | int(11) | YES | | NULL | |
| ManOfMatch | varchar(40) | YES | | NULL | |
| HomeTeamCaptain | varchar(30) | YES | | NULL | |
| HomeTeamKeeper | varchar(30) | YES | | NULL | |
| AwayTeamCaptain | varchar(30) | YES | | NULL | |
| AwayTeamKeeper | varchar(30) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
我需要从summary中选择数据,获取相应的player_id并将player_id输入到我的“testMatch”中。玩家表如下:
球员桌
mysql> describe players;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| player_id | int(11) | NO | PRI | NULL | auto_increment |
| player_surname | varchar(30) | YES | | NULL | |
| team | varchar(100) | YES | MUL | NULL | |
+----------------+--------------+------+-----+---------+----------------+
所以为了澄清,例如,我想从汇总表中选择主队队长数据,而不是名字,我想要相应的球员id。
我想我需要使用某种连接/子查询来完成这个任务。。。我试过选择:
select matchID, player_id, player_surname, team from players p, summary
s
where
s.hometeamcaptain = p.player_surname ORDER BY matchID;
但这将带回73行,应该只有65(65场比赛)。
我希望这是有意义的,谢谢你的帮助!!
特雷莎
最佳答案
有重叠的名字吗?如果是,还应确保团队对应(将s.HomeTeam = p.team
添加到where块)。如果一个团队中有同名玩家,则必须手动解决这些冲突。
要同时选择所有的守门员/队长,您需要left outer joins。我想每个玩家只能加入一个,所以你必须加入同一个表4次。
一旦选择了正确的数据,就可以使用INSERT ... SELECT将其插入testMatch
表中。