我现在正试图用我的'summary'表中的数据填充我的'testMatch'表(下面):
TESTMATCH TABLE
+------------------+--------------+------+-----+---------+-------+
| 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 | |
+-----------------+--------------+------+-----+---------+-------+
我需要以某种方式从摘要中选择数据,获取相应的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 | |
+----------------+--------------+------+-----+---------+----------------+
所以要澄清一下,例如。我想选择homeTeam_captain数据FROM摘要表但不是名字,我想要相应的player_id。
我假设我需要使用某种连接/子查询来完成这个...我已经尝试首先找到正确的选择查询以确保我拔出正确的数据,并且我一直在使用下面的测试代码(感谢用户Larry_Croft帮助我):
select matchID, player_id, player_surname, team from players p, summary s
where
s.hometeamcaptain = p.player_surname AND s.HomeTeam = p.team ORDER BY matchID;
但是这正确地带回了65行(65场比赛)但是我接着尝试了:
select matchID, player_id, player_surname, team from players p, summary s
where
s.hometeamKEEPER = p.player_surname AND s.HomeTeam = p.team ORDER BY matchID;
但是这只能带回61行(应该是65行)所以我必须在逻辑中出错。
一旦我选择了这个工作,我就需要以某种方式将它包含在我的'INSERT INTO ..... SELECT语句中,将所有数据放入'testMatch'表。
我希望这是有道理的,谢谢你的帮助!
答案 0 :(得分:0)
对我来说,看起来有一个家庭计划,它有一个空值或者hometeamKEEPER的值不在玩家表中。
使用以下查询,您应该能够找到不在播放器表中的hometownKEEPER:
SELECT matchID, player_id, player_surname, team
FROM players p
RIGHT JOIN summary s ON p.hometeamKEEPER = p.player_surname AND
s.HomeTeam = p.team
ORDER BY matchID;