简体   繁体   English

Mysql 改进选择连接表查询

[英]Mysql improve select junction table query

I'm trying to get the following behavior:我正在尝试获得以下行为:

+-----+--------------+-----+-----+-----+-------+-------+-------+--------+--------+
| 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 |
+-----+--------------+-----+-----+-----+-------+-------+-------+--------+--------+

Where 3 tables are defined as the following:其中 3 个表定义如下:

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    |
+----+----------+

It's known that each pokedex entry associates with 2 type in the poke_type table.众所周知,每个pokedex条目都与poke_type表中的 2 个type相关联。 I attempted to make a query like:我尝试进行如下查询:

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

It's an ugly query and probably not really efficient.这是一个丑陋的查询,可能效率不高。 Any idea on how to improve this query/select in a different way?关于如何以不同的方式改进此查询/选择的任何想法?

If it's not important for you which type is type1 and which type is type2, this is probably the simplest you can get:如果您认为哪种类型是 type1 哪种类型是 type2 对您来说并不重要,那么这可能是您可以获得的最简单的方法:

SELECT  
    dex.*, 
    MAX(t.name) AS type1, 
    MIN(t.name) AS type2 
FROM 
    pokedex AS dex 
    JOIN poke_type AS pt ON dex.id = pt.pokedex_id 
    JOIN type as t ON pt.type_id = t.id
GROUP BY dex.id

sqlfiddle sqlfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM