简体   繁体   English

从MySQL中的同一表中选择父子

[英]selecting parent child from same table in mysql

After searching a lot here, I could not find solution to my problem. 在这里搜索了很多之后,我找不到解决问题的方法。 So, I am posting this question. 所以,我要发布这个问题。

I have a Database Table which has the structure like this: 我有一个数据库表,其结构如下:

folder_id    folder_name    parent_id
--------------------------------------
    1          Private          0
    2          Public           0
    3          Photos           0
    4          December         3
    5          Bday             4
    6          Celeb            5

In hierarchical form, it will be like a folder structure : 在分层形式中,它将类似于文件夹结构:

-- Private
-- Public
-- Photos
----- December
-------- Bday
----------- Celeb

Now, I would like to select a Path upto a particular folder, like to Bday or Celeb folder. 现在,我想选择一个到特定文件夹的路径,例如BdayCeleb文件夹。 Thus, I want a MySQL query which will return me only the rows containing the folders between the path to a specific folder. 因此,我想要一个MySQL查询,该查询将仅向我返回包含特定文件夹路径之间的文件夹的行。

For Example, If I want a path to Celeb folder, then the Query should return these rows only : 例如,如果我想要Celeb文件夹的路径,则查询应仅返回以下行:

folder_id    folder_name    parent_id
--------------------------------------
    3          Photos           0
    4          December         3
    5          Bday             4
    6          Celeb            5

Currently, I am stuck with this query and I am not able to make it work. 目前,我对此查询感到困惑,并且无法使其正常工作。 The Query I am currently trying : 我目前正在尝试查询:

SELECT f.*
FROM fd_folders f
LEFT JOIN fd_folders p
ON f.folder_id = p.parent_id
WHERE f.folder_id <=6
ORDER BY f.folder_id;

But, the problem is that it is also returning the two other folders, ie, Private and Public . 但是,问题在于它还会返回另外两个文件夹,即PrivatePublic

Please help. 请帮忙。

Thank You 谢谢

Best Regards 最好的祝福

I'll just post this even though it does not DIRECTLY answer your question. 即使它没有直接回答您的问题,我也将发布它。

It looks like you may have to set up your table to have hierarchical data: http://www.sitepoint.com/hierarchical-data-database-2/ 看来您可能需要将表设置为具有分层数据: http : //www.sitepoint.com/hierarchical-data-database-2/

This will take some playing with to get set up correctly, and inserting new data will not be a fun process. 这需要花些时间才能正确设置,并且插入新数据将不是一个有趣的过程。 Your life may be much easier if you use an additional language that processes the results for you. 如果使用其他语言为您处理结果,您的生活可能会容易得多。

If you have the option of using SQL Server instead (not mysql), this whole thing actually becomes quite simple. 如果您可以选择使用SQL Server(而不是mysql),那么整个过程实际上就变得非常简单。 You just need a Common Table Expression: 您只需要一个公共表表达式:

;WITH DirectoryTree (folderId, folderName, parentId)
AS
(

    SELECT d.folderId, d.folderName, d.parentId
    FROM zzz_Directories d
    WHERE d.folderId = 4 -- target folderId

    UNION ALL

    SELECT d.folderId, d.folderName, d.parentId
    FROM zzz_Directories d
    INNER JOIN DirectoryTree dt ON dt.parentId = d.folderId

)

SELECT dt.folderId, dt.folderName, dt.parentId
FROM DirectoryTree dt

(Semicolon intentional) (分号是有意的)

Regrettably, mysql doesn't support this. 遗憾的是,mysql不支持此功能。 Instead, you may have to do what was suggested in the comments: use a stored procedure. 相反,您可能必须执行注释中建议的操作:使用存储过程。 I don't have an environment up to test this, but I imagine such a procedure would have two parts: 我没有测试该环境,但是我想这样的过程将包含两个部分:

  1. if my parent is 0, return me 如果我父母是0,还给我
  2. if my parent is not 0, return me UNION the procedure 如果我的父母不为0,回到我UNION程序

the procedure would simply recursively call itself until the parent = 0. I don't know if this is even possible, but it's a place to start. 该过程将简单地递归调用自身,直到parent =0。我什至不知道这是否有可能,但这是一个起点。

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

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