我目前有一个 mysql Union 查询,当通过命令行、PHP MyAdmin 直接放入 mysql 或在 Mysql 编辑器(如 Mysql Workbench)中运行时,它可以工作。当我通过我的 PHP 类运行查询时,它返回两个重复的行。
查询:
SELECT `n`.`draftrd`, `n`.`draftteam`, `n`.`nflteam`, `nt`.`logo`,
`nt`.`name`, `nt`.`nflcity`,
`p`.`class`, `p`.`first_name`, `p`.`last_name`
FROM `players` as `p`
LEFT JOIN `nfl` as `n` ON `p`.`playerid` = `n`.`playerid`
LEFT JOIN `nflteams` as `nt` ON `n`.`nflteam` = `nt`.`nflid`
WHERE `n`.`playerid` = 2007000001
UNION
SELECT `n`.`draftrd` as `draftRd`, `n`.`draftteam` as `draftTm`, `n`.`nflteam`,
`nt`.`logo`,
`nt`.`name` as `draftName`, `nt`.`nflcity` as `draftCity`, `p`.`class`,
`p`.`first_name`, `p`.`last_name`
FROM `players` as `p`
LEFT JOIN `nfl` as `n` ON `p`.`playerid` = `n`.`playerid`
LEFT JOIN `nflteams` as `nt` ON `n`.`draftteam` = `nt`.`nflid`
WHERE `n`.`playerid` = 2007000001
在编辑器中它返回:
+---------+-----------+---------+-------------+---------+----------+-------+------------+-----------+---------+
| draftrd | draftteam | nflteam | logo | name | nflcity | class | first_name | last_name | tableid |
+---------+-----------+---------+-------------+---------+----------+-------+------------+-----------+---------+
| 2 | 2 | 12 | giants.png | Giants | New York | 2007 | Martellus | Bennett | 1 |
| 2 | 2 | 12 | cowboys.png | Cowboys | Dallas | 2007 | Martellus | Bennett | 1 |
+---------+-----------+---------+-------------+---------+----------+-------+------------+-----------+---------+
2 rows in set (0.00 sec)
PHP 返回是:(var_dump 版本)
array(18) {
[0]=> string(1) "2"
["draftrd"]=> string(1) "2"
[1]=> string(1) "2"
["draftteam"]=> string(1) "2"
[2]=> string(2) "12"
["nflteam"]=> string(2) "12"
[3]=> string(10) "giants.png"
["logo"]=> string(10) "giants.png"
[4]=> string(6) "Giants"
["name"]=> string(6) "Giants"
[5]=> string(8) "New York"
["nflcity"]=> string(8) "New York"
[6]=> string(4) "2007"
["class"]=> string(4) "2007"
[7]=> string(9) "Martellus"
["first_name"]=> string(9) "Martellus"
[8]=> string(7) "Bennett"
["last_name"]=> string(7) "Bennett"
}
我不确定问题是什么以及为什么它不能正常工作。我尝试创建一个临时表并将查询存储在其中,但它仍然给了我重复的行。有没有更简单的方法来做到这一点或解释为什么会发生这种情况?我已经在 PHP 中转储了查询,以查看问题是否与它的构造有关,但是转储的查询在命令行上运行时返回了我正在寻找的行。