【问题标题】:Order By order, parent_id and group?按顺序、parent_id 和组排序?
【发布时间】:2021-06-28 15:56:07
【问题描述】:

我真的不确定如何准确表达我想要做的事情,因此我无法搜索。我有一张pages 的表格,如果parent_idNULL,则每个表格都有idtitleorderparent_id,这被认为是顶级页面。我几乎可以使用ORDER BY by parent_idorder 使用以下查询正确排序:


select `id`, `title`, `order`, `parent_id`
from pages
order by `order` AND COALESCE(`parent_id`, `order`), `parent_id` is not null, `order`

查询结果如下:

id title order parent_id
107fa138 video 0 NULL
8eeda86c mn 2 NULL
cac640ad xxe title 3 NULL
1ce4d070 sdfsdfsdf 4 NULL
b45dc24d another 1 8eeda86c
d3490141 hello 9 8eeda86c

几乎是我想要的。理想情况下,我会将带有 parent_ids 的行直接放在带有 id 的行下方,因此理想情况下排序顺序如下所示:

id title order parent_id
107fa138 video 0 NULL
8eeda86c mn 2 NULL
b45dc24d another 1 8eeda86c
d3490141 hello 9 8eeda86c
cac640ad xxe title 3 NULL
1ce4d070 sdfsdfsdf 4 NULL

我什至不知道我会怎么做。如果有人能指出我正确的方向,那就太棒了。

【问题讨论】:

    标签: mysql mysql-8.0


    【解决方案1】:

    我认为你的问题在这里:

    order by `order` AND COALESCE(`parent_id`, `order`), ...
    

    这可能不是你认为的那样。 AND 是一个布尔运算符,所以就好像你写了这个表达式:

    order by (`order` > 0) AND (COALESCE(`parent_id`, `order`) > 0), ...
    

    也就是说,如果order 和其他项都不为零,则为 1,否则为 0。

    我认为以下内容会更接近您的描述:

    order by COALESCE(`parent_id`, `id`), 
        `parent_id` is not null, 
        `order`
    

    演示:

    create table pages ( id varchar(10) primary key, title text, `order` int, parent_id varchar(10) );  
    
    insert into pages (id, title, `order`, parent_id) values 
    ('107fa138', 'video',     0, NULL),
    ('8eeda86c', 'mn',        2, NULL),
    ('cac640ad', 'xxe title', 3, NULL),
    ('1ce4d070', 'sdfsdfsdf', 4, NULL),
    ('b45dc24d', 'another',   1, '8eeda86c'),
    ('d3490141', 'hello',     9, '8eeda86c');
    
    select `id`, `title`, 
      COALESCE(parent_id, id) as cpi, 
      parent_id is not null as pinn, 
      `order`, 
      `parent_id` 
    from pages 
    order by COALESCE(`parent_id`, `id`), `parent_id` is not null, `order`
    
    +----------+-----------+----------+------+-------+-----------+
    | id       | title     | cpi      | pinn | order | parent_id |
    +----------+-----------+----------+------+-------+-----------+
    | 107fa138 | video     | 107fa138 |    0 |     0 | NULL      |
    | 1ce4d070 | sdfsdfsdf | 1ce4d070 |    0 |     4 | NULL      |
    | 8eeda86c | mn        | 8eeda86c |    0 |     2 | NULL      |
    | b45dc24d | another   | 8eeda86c |    1 |     1 | 8eeda86c  |
    | d3490141 | hello     | 8eeda86c |    1 |     9 | 8eeda86c  |
    | cac640ad | xxe title | cac640ad |    0 |     3 | NULL      |
    +----------+-----------+----------+------+-------+-----------+
    

    通过添加显示排序表达式的列,我们可以看到排序是如何发生的。

    首先它按字母顺序按cpi 排序。如果已设置,则首选 parent_id,但默认为 id

    对于cpi 的关系,则按pinn 排序。所以 0 在 1 之前。

    对于 pinn 的关系(即当多行的值为 1 时),则按 order 排序。

    这不是你想要的吗?

    【讨论】:

    • 我删除了order by order 部分,它似乎返回了相同的结果。我认为你是对的,它没有达到我的预期,但它仍然没有返回任何不同的顺序
    • 我在上面做了一个编辑。抱歉,我正在考虑,但我没有在原始答案中包含更改。
    • 嘿,感谢@BillKarwin 的帮助,这确实让我更接近了!我将在这里再提出一个问题,因为我只是不明白这是如何工作的。孩子确实在父母之下,这很好,但是排序顺序与null 值混淆了。我得到 0, 4, 2...3 我希望(或希望)得到 0, 2...3, 4。
    • 啊!收到了。谢谢!这正是我想要的,我没有完全理解它,所以它看起来不对,但这是一个很好的解释。谢谢!
    猜你喜欢
    • 2016-11-26
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2022-12-16
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多