1
私は次の動作を取得しようとしている
:3つの表は、以下のように定義されているMySQLの改善選択ジャンクションテーブルクエリ
+-----+--------------+-----+-----+-----+-------+-------+-------+--------+--------+
| id | name | hp | atk | def | spatk | spdef | speed | type1 | type2 |
+-----+--------------+-----+-----+-----+-------+-------+-------+--------+--------+
| 1 | Bulbasaur | 45 | 49 | 49 | 65 | 65 | 45 | GRASS | POISON |
| 2 | Ivysaur | 60 | 62 | 63 | 80 | 80 | 60 | GRASS | POISON |
| 3 | Venusaur | 80 | 82 | 83 | 100 | 100 | 80 | GRASS | POISON |
+-----+--------------+-----+-----+-----+-------+-------+-------+--------+--------+
:
pokedex:
+----+------------+----+-----+-----+-------+-------+-------+
| id | name | hp | atk | def | spatk | spdef | speed |
+----+------------+----+-----+-----+-------+-------+-------+
| 1 | Bulbasaur | 45 | 49 | 49 | 65 | 65 | 45 |
| 2 | Ivysaur | 60 | 62 | 63 | 80 | 80 | 60 |
| 3 | Venusaur | 80 | 82 | 83 | 100 | 100 | 80 |
+----+------------+----+-----+-----+-------+-------+-------+
poke_type:
+------------+---------+
| pokedex_id | type_id |
+------------+---------+
| 1 | 13 |
| 1 | 5 |
| 2 | 13 |
| 2 | 5 |
+------------+---------+
type:
+----+----------+
| id | name |
+----+----------+
| 1 | NONE |
| 5 | POISON |
| 13 | GRASS |
+----+----------+
ことが知られています、各pokedex
エントリー仲間と2 type
はpoke_type
テーブルにあります。私は次のようなクエリを作成しようとしました:
USE pokemon;
SELECT dex.*,
t1.name AS type1,
t2.name AS type2
FROM pokedex AS dex,
(SELECT pt.pokedex_id AS dexid, t.name AS name
FROM pokedex AS d
INNER JOIN poke_type AS pt ON d.id = pt.pokedex_id
INNER JOIN type as t ON pt.type_id = t.id
) AS t1,
(SELECT pt.pokedex_id AS dexid, t.name AS name
FROM pokedex AS d
INNER JOIN poke_type AS pt ON d.id = pt.pokedex_id
INNER JOIN type as t ON pt.type_id = t.id
) AS t2
WHERE dex.id = t1.dexid AND dex.id = t2.dexid AND t1.name <> t2.name
GROUP BY dex.id -- remove duplicates
これは醜いクエリであり、おそらく効率的ではありません。どのようにこのクエリを改善するか/別の方法で選択する上の任意のアイデア?