订购MySQL结果以服从父母/子女 [英] Ordering MySQL results to obey parent/child

查看:107
本文介绍了订购MySQL结果以服从父母/子女的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的表:

ID   NAME                     TYPE     PARENT
1    Letters of Jane Austen   book     0
2    Chapter 1                chapter  1
3    Chapter 2                chapter  1
4    Chapter 3                chapter  1
5    Title 1                  title    2
6    Title 2                  title    2

我想创建如下这样的有序表格:

I want to create a ordered form like this:

[]Letters of Jane Austen
  []Chapter 1
    []Title 1
    []Title 2
  []Chapter 2
  []Chapter 3
[]Another book...

数据库中有很多书,而不仅仅是一本书.

In the database there are many books, not just one.

有没有办法以这种方式对查询"SELECT"进行排序? 这只是一个表面问题,不需要做多维数组之类的事情,因为我可以在标签上贴上它们的类型,然后根据类型更改它们的外观.但是我需要按正确的顺序查找查询.

There any way to sort the query "SELECT" in this way? It is only a cosmetic issue and it does not need to do a multidimensional array or something, because I can put on the label their type and then to change their appearance depending on the type. But I need to fech the query in the right order.

P.D:对不起,我的英语:S

P.D: sorry for my english :S

推荐答案

不要认为这对数据库是个好主意.查询将很难. Imo更好-您选择所有书籍,以严格的顺序写到页面上,然后使用js重新排序.

Don't think it's a good idea for database. Query would be kind of hard to it. Imo better - you select all books, write it to page in a streight order and just reorder it with js.

HTML可能是:

<div id="books">
    <div id="book_1" class="book" parent="0">Letters of Jane Austen</div>
    <div id="book_2" class="book" parent="1">Chapter 1</div>
    <div id="book_3" class="book" parent="1">Chapter 2</div>
    <div id="book_4" class="book" parent="1">Chapter 3</div>
    <div id="book_5" class="book" parent="2">Title 1</div>
    <div id="book_6" class="book" parent="2">Title 2</div>
</div>

那么执行此层次结构的js:

So js that does this hierarchy:

$(function () {
    $('#books .book').each(function () {
        var p = $(this).attr("parent");
        if ($('#book_' + p).length) {
            $(this).appendTo($('#book_' + p));
        }
    });
});

通过 JSFiddle

更新

如果您希望它与非js客户端一起正常工作,则可以使用php在后端上对其进行重新排序,但是解决方案不会那么漂亮:D

And you can reorder it on a backend with php if you want it work right with non-js clients, but solution won't be that beautiful :D

这篇关于订购MySQL结果以服从父母/子女的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆