MySQL UNION问题 - 无法获取正确的数据

时间:2010-02-27 14:02:04

标签: mysql union

我需要规范化包含板球运动员的桌子:

 
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| matchID    | int(11)      | NO   | PRI | 0       |       | 
| innings    | int(11)      | NO   | PRI | 0       |       | 
| Bat1Name   | varchar(30)  | YES  |     | NULL    |       | 
| Bat1Score  | int(11)      | YES  |     | NULL    |       | 
| balls1     | int(11)      | YES  |     | NULL    |       | 
| sixes1     | int(11)      | YES  |     | NULL    |       | 
| fours1     | int(11)      | YES  |     | NULL    |       | 
| out1       | varchar(10)  | YES  |     | NULL    |       | 
| catcher1   | varchar(30)  | YES  |     | NULL    |       | 
| bowler1    | varchar(30)  | YES  |     | NULL    |       | 
| Bat2Name   | varchar(30)  | YES  |     | NULL    |       | 
| Bat2Score  | int(11)      | YES  |     | NULL    |       | 
| balls2     | int(11)      | YES  |     | NULL    |       | 
| sixes2     | int(11)      | YES  |     | NULL    |       | 
| fours2     | int(11)      | YES  |     | NULL    |       | 
| out2       | varchar(10)  | YES  |     | NULL    |       | 
| catcher2   | varchar(30)  | YES  |     | NULL    |       | 
| bowler2    | varchar(30)  | YES  |     | NULL    |       | 
| Bat3Name   | varchar(30)  | YES  |     | NULL    |       | 
| Bat3Score  | int(11)      | YES  |     | NULL    |       | 
| balls3     | int(11)      | YES  |     | NULL    |       | 
| sixes3     | int(11)      | YES  |     | NULL    |       | 
| fours3     | int(11)      | YES  |     | NULL    |       | 
| out3       | varchar(10)  | YES  |     | NULL    |       | 
| catcher3   | varchar(30)  | YES  |     | NULL    |       | 
| bowler3    | varchar(30)  | YES  |     | NULL    |       | 
| Bat4Name   | varchar(30)  | YES  |     | NULL    |       | 
| Bat4Score  | int(11)      | YES  |     | NULL    |       | 
| balls4     | int(11)      | YES  |     | NULL    |       | 
| sixes4     | int(11)      | YES  |     | NULL    |       | 
| fours4     | int(11)      | YES  |     | NULL    |       | 
| out4       | varchar(10)  | YES  |     | NULL    |       | 
| catcher4   | varchar(30)  | YES  |     | NULL    |       | 
| bowler4    | varchar(30)  | YES  |     | NULL    |       | 
| Bat5Name   | varchar(30)  | YES  |     | NULL    |       | 
| Bat5Score  | int(11)      | YES  |     | NULL    |       | 
| balls5     | int(11)      | YES  |     | NULL    |       | 
| sixes5     | int(11)      | YES  |     | NULL    |       | 
| fours5     | int(11)      | YES  |     | NULL    |       | 
| out5       | varchar(10)  | YES  |     | NULL    |       | 
| catcher5   | varchar(30)  | YES  |     | NULL    |       | 
| bowler5    | varchar(30)  | YES  |     | NULL    |       | 
| Bat6Name   | varchar(30)  | YES  |     | NULL    |       | 
| Bat6Score  | int(11)      | YES  |     | NULL    |       | 
| balls6     | int(11)      | YES  |     | NULL    |       | 
| sixes6     | int(11)      | YES  |     | NULL    |       | 
| fours6     | int(11)      | YES  |     | NULL    |       | 
| out6       | varchar(10)  | YES  |     | NULL    |       | 
| catcher6   | varchar(30)  | YES  |     | NULL    |       | 
| bowler6    | varchar(30)  | YES  |     | NULL    |       | 
| Bat7Name   | varchar(30)  | YES  |     | NULL    |       | 
| Bat7Score  | int(11)      | YES  |     | NULL    |       | 
| balls7     | int(11)      | YES  |     | NULL    |       | 
| sixes7     | int(11)      | YES  |     | NULL    |       | 
| fours7     | int(11)      | YES  |     | NULL    |       | 
| out7       | varchar(10)  | YES  |     | NULL    |       | 
| catcher7   | varchar(30)  | YES  |     | NULL    |       | 
| bowler7    | varchar(30)  | YES  |     | NULL    |       | 
| Bat8Name   | varchar(30)  | YES  |     | NULL    |       | 
| Bat8Score  | int(11)      | YES  |     | NULL    |       | 
| balls8     | int(11)      | YES  |     | NULL    |       | 
| sixes8     | int(11)      | YES  |     | NULL    |       | 
| fours8     | int(11)      | YES  |     | NULL    |       | 
| out8       | varchar(10)  | YES  |     | NULL    |       | 
| catcher8   | varchar(30)  | YES  |     | NULL    |       | 
| bowler8    | varchar(30)  | YES  |     | NULL    |       | 
| Bat9Name   | varchar(30)  | YES  |     | NULL    |       | 
| Bat9Score  | int(11)      | YES  |     | NULL    |       | 
| balls9     | int(11)      | YES  |     | NULL    |       | 
| sixes9     | int(11)      | YES  |     | NULL    |       | 
| fours9     | int(11)      | YES  |     | NULL    |       | 
| out9       | varchar(10)  | YES  |     | NULL    |       | 
| catcher9   | varchar(30)  | YES  |     | NULL    |       | 
| bowler9    | varchar(30)  | YES  |     | NULL    |       | 
| Bat10Name  | varchar(30)  | YES  |     | NULL    |       | 
| Bat10Score | int(11)      | YES  |     | NULL    |       | 
| balls10    | int(11)      | YES  |     | NULL    |       | 
| sixes10    | int(11)      | YES  |     | NULL    |       | 
| fours10    | int(11)      | YES  |     | NULL    |       | 
| out10      | varchar(10)  | YES  |     | NULL    |       | 
| catcher10  | varchar(30)  | YES  |     | NULL    |       | 
| bowler10   | varchar(30)  | YES  |     | NULL    |       | 
| Bat11Name  | varchar(30)  | YES  |     | NULL    |       | 
| Bat11Score | int(11)      | YES  |     | NULL    |       | 
| balls11    | int(11)      | YES  |     | NULL    |       | 
| sixes11    | int(11)      | YES  |     | NULL    |       | 
| fours11    | int(11)      | YES  |     | NULL    |       | 
| out11      | varchar(10)  | YES  |     | NULL    |       | 
| catcher11  | varchar(30)  | YES  |     | NULL    |       | 
| bowler11   | varchar(30)  | YES  |     | NULL    |       | 
| extras     | int(11)      | YES  |     | NULL    |       | 
| wides      | int(11)      | YES  |     | NULL    |       | 
| noBalls    | int(11)      | YES  |     | NULL    |       | 
| byes       | int(11)      | YES  |     | NULL    |       | 
| legByes    | int(11)      | YES  |     | NULL    |       | 
| score      | int(11)      | YES  |     | NULL    |       | 
| wickets    | int(11)      | YES  |     | NULL    |       | 
| overs      | float        | YES  |     | NULL    |       | 
| runrate    | float        | YES  |     | NULL    |       | 
| team       | varchar(100) | YES  |     | NULL    |       | 
+------------+--------------+------+-----+---------+-------+

我感兴趣的行是Bat?Name和Team字段,例如。 Bat1Name,Bat2Name .... Bat11Name和他们所属的团队。

我创建了一个播放器表:

 
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    |                | 
    +----------------+--------------+------+-----+---------+----------------+
3 rows in set (0.03 sec)

我使用下面的UNION代码成功地将玩家的名字(没有重复)选入玩家的桌子:


INSERT INTO players (player_surname)
SELECT DISTINCT bat1Name from details
UNION
SELECT DISTINCT bat2Namen from details
UNION
SELECT DISTINCT bat3Name from details
(etc............................)
SELECT DISTINCT bat11Name from details; 

这给了我300条记录是正确的。

然后意识到我需要包括团队,所以我添加了团队:

 
INSERT INTO players (player_surname, team)
SELECT DISTINCT bat1Name,team from details
UNION
SELECT DISTINCT bat2Namen,team from details
UNION
SELECT DISTINCT bat3Name,team from details
(etc.............................)
SELECT DISTINCT bat11Name,team from details;

但这为我提供了310条记录,并说(例如)史密斯为英国,新西兰和南非效力,但这是不正确的。它还错误地推出了其他几个。我不知所措!

我认为我的UNION方法存在缺陷,所以如何才能让所有击球手与其相应的球队一起退出桌面?

2 个答案:

答案 0 :(得分:1)

如果有两个姓氏相同的球员在不同的球队中比赛,他们将在第一个查询中合并但不会在第二个查询中合并:

bat1   bat2   team
Smith  Jones  UK
Doe    Smith  Zealand

第一个查询将返回Smith一次,第二个返回两次。

您的第一个查询很可能是错误的,而不是第二个。尝试运行第一个查询:

SELECT  batname
FROM    (
        SELECT  bat1Name AS batname, team
        FROM   details
        UNION
        SELECT  bat2Name, team
        FROM   details
        UNION  …
        ) q

您无需在此执行DISTINCTUNION会自动处理此问题。

答案 1 :(得分:0)

正如Quassnoi所说,这可能是第一个错误的查询,因为第二个查询将提取正确的数据。

此外,玩家表只应存储与玩家相关的数据,并与存储团队的另一个表存在关联。