MySQL:如何查询父子关系?

标签 mysql database

假设有下表记录:

TABLE: foo
==========================
| foo_id | foo_parent_id |
==========================
| 1      | NULL          |
| 2      | NULL          |
| 3      | 1             |
| 4      | 2             |
| 5      | 1             |
| 6      | 1             |
| 7      | 2             |
| 8      | 1             |
| 9      | NULL          |
--------------------------

我想获取前 10 条父记录(那些 foo_parent_id = NULL 的记录),紧接着是该父记录的前 2 条子记录。所以,我正在寻找这样的结果:

1, NULL
3, 1
5, 1
2, NULL
4, 2
7, 2
9, NULL

如何查询这样的内容?

最佳答案

这是一个想法。但它基于许多关于数据设置方式的假设。不断增加树下的 ID,只有两层,等等。

SELECT f.foo_id,f.foo_parent_id FROM foo f
foo f

--给我前X个parent_ids (这很好,您只需调整 LIMIT 10 来改变要显示的父级别的数量)

INNER JOIN 
(select foo_id from foo where foo_parent_id is null order by foo_parent_id 
LIMIT 10
) top_foo_parent
      on isnull(f.foo_parent_id,f.foo_id) = top_foo_parent.foo_id
WHERE

(这部分有点 hacky,因为你必须放置更长的字符串才能通过两个 child )

--是第一个 child ,或者...

(f.foo_id in (select MIN(foo_id) from foo fc1 where fc1.foo_parent_id =f.foo_parent_id)
 )
 or

--是老二,还是...

(f.foo_id in (select MIN(foo_id) from foo fc1 where fc1.foo_parent_id =f.foo_parent_id  and fc1.foo_id not in (select MIN(foo_id) from foo fc2 where fc2.foo_parent_id=f.foo_parent_id))
 )
 or 

--这是 parent

 f.foo_parent_id is null
order by isnull(f.foo_parent_id,f.foo_id)*100 + f.foo_id

所以我们在这里所做的基本上是按 parent_id 列排序,然后稍微改变一下它下面的子列。如果 parentid 列为 NULL,则我们使用实际 ID。这意味着出于排序目的,我们的表格如下所示:

==============================================================================
| foo_id | foo_parent_id |   isnull(f.foo_parent_id,f.foo_id)
==============================================================================
| 1      | NULL           |         (1)
| 2      | NULL           |         (2)
| 3      |  1             |         1
| 4      |  2             |         2
| 5      |  1             |         1
| 7      |  2             |         2
----------------------------------------------------------------------

然后我们将该排序列乘以 *100

==============================================================================
| foo_id | foo_parent_id |   isnull(f.foo_parent_id,f.foo_id)*100
==============================================================================
| 1      | NULL           |         100
| 2      | NULL           |         200
| 3      |  1             |         100
| 4      |  2             |         200
| 5      |  1             |         100
| 7      |  2             |         200
----------------------------------------------------------------------

最后我们向其中添加我们的 foo_id 列

==============================================================================
| foo_id | foo_parent_id |   isnull(f.foo_parent_id,f.foo_id)*100 + foo_id
==============================================================================
| 1      | NULL           |         101
| 2      | NULL           |         202
| 3      |  1             |         103
| 4      |  2             |         204
| 5      |  1             |         105
| 7      |  2             |         207
----------------------------------------------------------------------

现在我们按该虚拟列对表格进行排序,然后...

==============================================================================
| foo_id | foo_parent_id |   ORDER BY isnull(f.foo_parent_id,f.foo_id)*100 + foo_id
==============================================================================
| 1      | NULL           |         101
| 3      |  1             |         103
| 5      |  1             |         105
| 2      | NULL           |         202    
| 4      |  2             |         204
| 7      |  2             |         207
----------------------------------------------------------------------

我们开始了!

关于MySQL:如何查询父子关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8633497/

相关文章:

php - 如何在没有 "like"参数的情况下在 codeigniter 中进行搜索?

mysql - 如何有效地计算 MySQL 中的序列中断/漏洞?

mysql - Hibernate 查询获取 2 个用户之间的现有对话

java - 使用复合主键更新 JPA 实体会导致重复输入错误

ruby-on-rails - 如何在非 Rails 应用程序中使用 ActiveRecord 创建新的 MySQL 数据库?

php - 如何从数据库行中获取项目列表并在 Laravel 中返​​回列表?

php - 在 Laravel 中的大型表上创建模型时创建唯一的跟踪代码

PHP Foreach 用于从数据库发送电子邮件?

mysql - SQL 批量更改日期格式

mysql - 如何在具有多个条件的MySql中获取计数