需要 mysql 查询帮助 - 按父级分组

发布于 2024-10-13 03:04:36 字数 1719 浏览 2 评论 0原文

我正在尝试执行一个 mysql 查询,该查询将创建一个父项数组,其子项位于下面。但我不是 100% 确定如何。这是我到目前为止所做的:

SELECT * FROMcategories as rf ORDER BY Parent, name ASC

这是输出的内容(数组):

Array
(
    [0] => stdClass Object
        (
            [id] => 7
            [name] => Safety Clothing
            [parent] => 0
        )

    [1] => stdClass Object
        (
            [id] => 8
            [name] => Safety Footwear
            [parent] => 0
        )

    [2] => stdClass Object
        (
            [id] => 9
            [name] => Workwear
            [parent] => 0
        )

    [3] => stdClass Object
        (
            [id] => 4
            [name] => Polos
            [parent] => 7
        )

    [4] => stdClass Object
        (
            [id] => 3
            [name] => Shirts
            [parent] => 7
        )

    [5] => stdClass Object
        (
            [id] => 6
            [name] => Jackets
            [parent] => 9
        )

    [6] => stdClass Object
        (
            [id] => 1
            [name] => Pants
            [parent] => 9
        )

    [7] => stdClass Object
        (
            [id] => 2
            [name] => Shirts
            [parent] => 9
        )

    [8] => stdClass Object
        (
            [id] => 5
            [name] => Shorts
            [parent] => 9
        )

)

如您所见,子项目具有父项目的 id(父项目设置为 0) ),但我不确定如何将它们合并在一起以执行类似这样的数组:

parent

-- child

-- child

parent

parent

-- 孩子

-- 孩子

-- 孩子

任何帮助将不胜感激:)

I'm trying to do a mysql query that will create an array of parent items, with their child items underneath. But I'm not 100% sure how. Here's what I have done so far:

SELECT * FROM categories as rf ORDER BY parent, name ASC

And here's what is being outputted (array):

Array
(
    [0] => stdClass Object
        (
            [id] => 7
            [name] => Safety Clothing
            [parent] => 0
        )

    [1] => stdClass Object
        (
            [id] => 8
            [name] => Safety Footwear
            [parent] => 0
        )

    [2] => stdClass Object
        (
            [id] => 9
            [name] => Workwear
            [parent] => 0
        )

    [3] => stdClass Object
        (
            [id] => 4
            [name] => Polos
            [parent] => 7
        )

    [4] => stdClass Object
        (
            [id] => 3
            [name] => Shirts
            [parent] => 7
        )

    [5] => stdClass Object
        (
            [id] => 6
            [name] => Jackets
            [parent] => 9
        )

    [6] => stdClass Object
        (
            [id] => 1
            [name] => Pants
            [parent] => 9
        )

    [7] => stdClass Object
        (
            [id] => 2
            [name] => Shirts
            [parent] => 9
        )

    [8] => stdClass Object
        (
            [id] => 5
            [name] => Shorts
            [parent] => 9
        )

)

As you can see the child items have the id of the parent items (parent is set to 0), but I'm not sure how to merge it all together to do an array something like this:

parent

-- child

-- child

parent

parent

-- child

-- child

-- child

Any help would be appreciated :)

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

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

发布评论

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

评论(2

紫南 2024-10-20 03:04:36

SELECT Parent,GROUP_CONCAT(name) 作为名称 FROM 类别作为 rf GROUP BY 1;

您将为每个父项获取一行,其中父项 ID 在第一列中,子项名称在第二列中以逗号分隔。

Also

SELECT parent,GROUP_CONCAT(name) as names FROM categories as rf GROUP BY 1;

You will get one row for every parent with parent id in the first column and child names separated by comma on the second column.

梦魇绽荼蘼 2024-10-20 03:04:36

如果可以使用该数组中的数据构建嵌套数组:
每个对象都有一个数组变量。然后,在构建初始数组后,通过解析初始数组手动将子级移动到其父级下。

解析的方法将是递归的,并且将采用初始数组和当前正在构建的子数组。

If it is possible to build nested arrays using the data from that array:
Each object would have an Array variable. Then after the initial array is built, move the children under their parent manually by parsing the initial array.

The method that parses would be recursive, and would take the initial array and the child array it is currently building.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文