如何创建 MySQL 分层递归查询? - How to create a MySQL hierarchical recursive query?
问题:
I have a MySQL table which is as follows:我有一个 MySQL 表,如下所示:
id ID | name名称 | parent_id parent_id |
---|---|---|
19 19 | category1类别1 | 0 0 |
20 20 | category2类别2 | 19 19 |
21 21 | category3类别3 | 20 20 |
22 22 | category4类别4 | 21 21 |
... ... | ... ... | ... ... |
Now, I want to have a single MySQL query to which I simply supply the id [for instance say id=19
] then I should get all its child ids [ie result should have ids '20,21,22']....现在,我想要一个 MySQL 查询,我只需提供 id [例如说id=19
] 然后我应该得到它的所有子 ID [即结果应该有 ids '20,21,22'] ... .
The hierarchy of the children is not known;孩子的等级是未知的; it can vary....它可以变化....
I know how to do it using a for
loop... but how to achieve the same using a single MySQL query?我知道如何使用for
循环来实现……但是如何使用单个 MySQL 查询来实现相同的目标?
解决方案:
参考一: http://stackoom.com/question/1Mp2O参考二: How to create a MySQL hierarchical recursive query?
「其他文章」
- POST 未选中的 HTML 复选框 - POST unchecked HTML checkboxes
- 获得 π 值的最快方法是什么? - What is the fastest way to get the value of π?
- 连接到 MySQL 数据库时关于 SSL 连接的警告 - Warning about SSL connection when connecting to MySQL database
- 如何检查 UITextField 何时更改? - How do I check when a UITextField changes?
- 如何在 Javascript 中开始和结束一天? - How to get start and end of day in Javascript?
- 在 package.json 中查找未使用的 npm 包 - Find unused npm packages in package.json
- 如何在 Linux 中生成带有绝对路径的文件列表? - How can I generate a list of files with their absolute path in Linux?
- 如何将对象添加到数组 - How to add an object to an array
- 检测是通过 require 调用还是直接通过命令行调用 - Detect if called through require or directly by command line
- 如何使用 IntelliJ IDEA 查找所有未使用的代码? - How to use IntelliJ IDEA to find all unused code?
- 如何创建 MySQL 分层递归查询? - How to create a MySQL hierarchical recursive query?
- 计算文件的 MD5 校验和 - Calculate MD5 checksum for a file
- git rebase:“错误:无法统计‘文件’:权限被拒绝” - git rebase: "error: cannot stat 'file': Permission denied"
- 类型错误:method() 需要 1 个位置参数,但给出了 2 个 - TypeError: method() takes 1 positional argument but 2 were given
- 读取和写入文件的最简单方法 - Easiest way to read from and write to files
- `enable_shared_from_this` 有什么用处? - What is the usefulness of `enable_shared_from_this`?
- Docker将什么添加到lxc-tools(用户空间LXC工具)? - What does Docker add to lxc-tools (the userspace LXC tools)?
- 如何在 string.replace 中输入正则表达式? - How to input a regex in string.replace?
- 如何从shell中将一个文件附加到Linux中的另一个文件? - How to append one file to another in Linux from the shell?
- CSS Selector,适用于具有两个类的元素 - CSS Selector that applies to elements with two classes