私は現在、コマンドライン、PHP MyAdminを介してmysqlに直接配置するか、MysqlWorkbenchなどのMysqlエディターで実行すると機能するmysqlUnionクエリを持っています。PHPクラスを介してクエリを実行すると、2つの重複する行が返されます。
クエリ:
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でクエリをダンプして、問題がその構造にあるかどうかを確認しましたが、ダンプされたクエリは、コマンドラインで実行したときに探している行を返します。