【问题标题】:Grouping results from multiple tables对来自多个表的结果进行分组
【发布时间】:2018-08-11 22:42:05
【问题描述】:

我有 6 个表:3 个用于有关动漫/漫画/ova/whatever 的一般数据,3 个用于它们的类型,这是一对一的关系

anime:                manga:                   ova:

aid | data | ... |    mid | data | ... |       oid | data | ... |
----+------+-----+    ----+------+-----+       ----+------+-----+
  1 | .... | ... |      1 | .... | ... |         1 | .... | ... | 
  2 | .... | ... |      2 | .... | ... |         2 | .... | ... |
  3 | .... | ... |      3 | .... | ... |         3 | .... | ... |
  4 | .... | ... |      4 | .... | ... |         4 | .... | ... |

anime_genre:          manga_genre:             ova_genre:

aid | genre           mid | name               oid | genre
----+--------         ----+----------          ----+--------
  1 | Ecchi             1 | Drama                1 | Action
  1 | Action            2 | Ecchi                2 | Action
  2 | Action            3 | Fighting             3 | Drama
  3 | Drama             4 | Action               4 | Ecchi
  4 | Action

每当有人搜索流派以在一个查询中获取所有信息时,我都会尝试对结果进行分组

案例一:

result on genre = Action:

  genre | ids                                         
--------+---------------------------------------------
 Action | 1:anime 2:anime 4:anime 4:manga 1:ova 2:ova


result on genre = Ecchi:

  genre | ids
--------+-------------------------------
  Ecchi | 1:anime 4:anime 2:manga 4:ova

案例2(首选):

result on genre = Action:

  genre |   id    | common data | common data | common data
--------+---------+-------------+-------------+-------------
 Action | 1:anime |     ...     |     ...     |     ...
 Action | 2:anime |     ...     |     ...     |     ...
 Action | 4:anime |     ...     |     ...     |     ...
 Action | 4:manga |     ...     |     ...     |     ...
 Action | 1:ova   |     ...     |     ...     |     ...
 Action | 2:ova   |     ...     |     ...     |     ...


result on genre = Ecchi:

 genre |   id    | common data | common data | common data
-------+---------+-------------+-------------+-------------
 Ecchi | 1:anime |     ...     |     ...     |     ...
 Ecchi | 4:anime |     ...     |     ...     |     ...
 Ecchi | 2:manga |     ...     |     ...     |     ...
 Ecchi | 4:ova   |     ...     |     ...     |     ...

我整天都在打我的头,我只能在一张桌子上得到想要的结果。我无法获得所有三合一查询的结果。

有什么想法吗?有人可以指出我正确的方向吗? (数据表有一些不常见的列,但我真的不在乎这些列是否为空值,因为我在 js 中解析结果)

【问题讨论】:

标签: mysql sql join concat nested-select


【解决方案1】:

lookup mysql join,可以join表,在多表之间搜索匹配的答案。

【讨论】:

    【解决方案2】:

    您可以使用union all获取ids的列表:

    select ag.genre, 'anime' as which, ag.aid as id
    from anime_genre ag
    union all
    select mg.genre, 'manga' as which, mg.mid as id
    from manga_genre mg 
    union all
    select og.genre, 'ova' as which, og.oid as id;
    from ova_genre og;
    

    您可以在子查询中加入其他列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 2012-10-06
      相关资源
      最近更新 更多