如何将这两个表连接在一起(MySQL,分层查询)?

发布于 2024-10-14 07:58:25 字数 466 浏览 4 评论 0原文

我有一个 categories 表,如下所示:

id | name       | parent    
-----------------------    
1  | Toys       | 1
2  | Clothing   | 1
3  | Kid's Toys | 0 

我有另一个名为 category_relationships 的表,如下所示:

id | category_id | parent_id    
----------------------------    
1  | 3           | 1

我想要以下输出:

类别:

Toys
  - Kid's Toys
Clothing

如何实现此目的一个查询?

I have a categories table that looks like this:

id | name       | parent    
-----------------------    
1  | Toys       | 1
2  | Clothing   | 1
3  | Kid's Toys | 0 

I have another table called category_relationships which looks like this:

id | category_id | parent_id    
----------------------------    
1  | 3           | 1

I want to have the following output:

Categories:

Toys
  - Kid's Toys
Clothing

How to achieve this with one query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-10-21 07:58:26

一个更好/适当/稳健的答案可能是为此创建一个 MySQL PROCEDURE,但是如果您的数据可以满足这些限制,您可以使用以下内容:

  • 不超过 5 个级别(或根据需要扩展模式)
  • ID 不再超过 6 位数字(或更改 concat 表达式)

此查询使用 Concat 构建可排序引用,以便 A 的子级位于 A 后面,等等。名称使用 concat 和前导空格手动缩进。

    select concat(1000000 + a.id, '|') SORT
          ,a.name
    from categories a
    where a.parent = 1 # top level parents only
union all
    select concat(1000000 + a.id, '|', 
             1000000 + IFNULL(b.id,0), '|')
          ,concat('  - ', b.name)
    from categories a
    inner join category_relationships a1 on a1.parent_id = a.id
    inner join categories b on b.id = a1.category_id
    where a.parent = 1
union all
    select concat(1000000 + a.id, '|', 
             1000000 + IFNULL(b.id,0), '|',
             1000000 + IFNULL(c.id,0), '|')
          ,concat('    - ', c.name)
    from categories a
    inner join category_relationships a1 on a1.parent_id = a.id
    inner join categories b on b.id = a1.category_id
    inner join category_relationships b1 on b1.parent_id = b.id
    inner join categories c on c.id = b1.category_id
    where a.parent = 1
union all
    select concat(1000000 + a.id, '|', 
             1000000 + IFNULL(b.id,0), '|',
             1000000 + IFNULL(c.id,0), '|',
             1000000 + IFNULL(d.id,0), '|')
          ,concat('      - ', d.name)
    from categories a
    inner join category_relationships a1 on a1.parent_id = a.id
    inner join categories b on b.id = a1.category_id
    inner join category_relationships b1 on b1.parent_id = b.id
    inner join categories c on c.id = b1.category_id
    inner join category_relationships c1 on c1.parent_id = c.id
    inner join categories d on d.id = c1.category_id
    where a.parent = 1
union all
    select concat(1000000 + a.id, '|', 
             1000000 + IFNULL(b.id,0), '|',
             1000000 + IFNULL(c.id,0), '|',
             1000000 + IFNULL(d.id,0), '|',
             1000000 + IFNULL(e.id,0))
          ,concat('        - ', e.name)
    from categories a
    inner join category_relationships a1 on a1.parent_id = a.id
    inner join categories b on b.id = a1.category_id
    inner join category_relationships b1 on b1.parent_id = b.id
    inner join categories c on c.id = b1.category_id
    inner join category_relationships c1 on c1.parent_id = c.id
    inner join categories d on d.id = c1.category_id
    inner join category_relationships d1 on d1.parent_id = d.id
    inner join categories e on e.id = d1.category_id
    order by SORT

A better/proper/robust answer will probably be create a MySQL PROCEDURE for this, but if your data can fit in these limitations, you can use the below:

  • no more than 5 levels (or expand the pattern as required)
  • IDs are no more than 6 digits (or change the concat expressions)

This query uses Concat to build a sortable reference so that children of A come after A etc. The names are indented manually using concat and leading spaces.

    select concat(1000000 + a.id, '|') SORT
          ,a.name
    from categories a
    where a.parent = 1 # top level parents only
union all
    select concat(1000000 + a.id, '|', 
             1000000 + IFNULL(b.id,0), '|')
          ,concat('  - ', b.name)
    from categories a
    inner join category_relationships a1 on a1.parent_id = a.id
    inner join categories b on b.id = a1.category_id
    where a.parent = 1
union all
    select concat(1000000 + a.id, '|', 
             1000000 + IFNULL(b.id,0), '|',
             1000000 + IFNULL(c.id,0), '|')
          ,concat('    - ', c.name)
    from categories a
    inner join category_relationships a1 on a1.parent_id = a.id
    inner join categories b on b.id = a1.category_id
    inner join category_relationships b1 on b1.parent_id = b.id
    inner join categories c on c.id = b1.category_id
    where a.parent = 1
union all
    select concat(1000000 + a.id, '|', 
             1000000 + IFNULL(b.id,0), '|',
             1000000 + IFNULL(c.id,0), '|',
             1000000 + IFNULL(d.id,0), '|')
          ,concat('      - ', d.name)
    from categories a
    inner join category_relationships a1 on a1.parent_id = a.id
    inner join categories b on b.id = a1.category_id
    inner join category_relationships b1 on b1.parent_id = b.id
    inner join categories c on c.id = b1.category_id
    inner join category_relationships c1 on c1.parent_id = c.id
    inner join categories d on d.id = c1.category_id
    where a.parent = 1
union all
    select concat(1000000 + a.id, '|', 
             1000000 + IFNULL(b.id,0), '|',
             1000000 + IFNULL(c.id,0), '|',
             1000000 + IFNULL(d.id,0), '|',
             1000000 + IFNULL(e.id,0))
          ,concat('        - ', e.name)
    from categories a
    inner join category_relationships a1 on a1.parent_id = a.id
    inner join categories b on b.id = a1.category_id
    inner join category_relationships b1 on b1.parent_id = b.id
    inner join categories c on c.id = b1.category_id
    inner join category_relationships c1 on c1.parent_id = c.id
    inner join categories d on d.id = c1.category_id
    inner join category_relationships d1 on d1.parent_id = d.id
    inner join categories e on e.id = d1.category_id
    order by SORT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文