[英]mysql problem: populate table from another table but referencing ID instead of name
I'm now trying to populate my 'testMatch' table (below) with data from my 'summary' table: 我现在正尝试使用“摘要”表中的数据填充“ 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 | |
+------------------+--------------+------+-----+---------+-------+
There is no problem populating match_id -----> manOfMatch - it is 'homeTeam_captain', 'homeTeam_keeper', 'awayTeam_captain' and 'awayTeam_keeper' that i'm having problems bringing in. 填充match_id -----> manOfMatch没问题-我遇到的问题是“ homeTeam_captain”,“ homeTeam_keeper”,“ awayTeam_captain”和“ awayTeam_keeper”。
SUMMARY TABLE 汇总表
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 | |
+-----------------+--------------+------+-----+---------+-------+
I need to somehow select the data from summary, get the corresponding player_id and input the player_id into my 'testMatch'.
我需要以某种方式从摘要中选择数据,获取相应的player_id并将其输入到我的“ testMatch”中。
Player table below:
球员表如下:
PLAYERS TABLE 玩家桌
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 | | +----------------+--------------+------+-----+---------+----------------+
So to clarify, eg. 因此要澄清,例如。 I want to select homeTeam_captain data FROM summary table BUT not the name, I want the corresponding player_id instead. 我想从汇总表中选择homeTeam_captain数据,但不是名称,而是要使用相应的player_id。
I assume I need to use some sort of join/subqueries to get this done... i've tried finding the correct select query first to make sure i'm pulling out the right data, and I have been using the below code for testing (thanks to user Larry_Croft for helping me with this): 我认为我需要使用某种联接/子查询来完成此操作...我尝试首先查找正确的选择查询以确保我提取了正确的数据,并且我一直在使用以下代码测试(感谢用户Larry_Croft为我提供的帮助):
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;
But this correctly brings back 65 rows (65 matches) BUT i then tried it with: 但这正确地带回了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;
But this brings back only 61 rows (should be 65) so i must have an error in the logic. 但这只会带回61行(应该是65行),因此我在逻辑上必须有一个错误。
Once I get this select to work, i then need to somehow include it into my 'INSERT INTO.....SELECT statement to put all the data into 'testMatch' table. 一旦选择成功,我就需要以某种方式将其包含到我的“ INSERT INTO ..... SELECT”语句中,以将所有数据放入“ testMatch”表中。
I hope this makes sense and thanks for your help! 我希望这是有道理的,感谢您的帮助!
Well for me it looks that there is eitehr a hometeamKEEPER that has a null value or that the value of hometeamKEEPER is not in the players table. 对我来说,看起来好像有一个hometeamKEEPER的值为空,或者hometeamKEEPER的值不在players表中。
Using the following query you should be able to find the hometownKEEPER that are not in the players table: 使用以下查询,您应该能够找到不在players表中的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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.