AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 290684
Accepted
alexandre9865
alexandre9865
Asked: 2021-04-29 17:00:38 +0800 CST2021-04-29 17:00:38 +0800 CST 2021-04-29 17:00:38 +0800 CST

根据 SQLite 中的 list_order 对具有路径索引的嵌套表进行排序

  • 772

我有一个具有这种嵌套结构的 SQL 表:

CREATE TABLE items (`id` INTEGER, `item_name` VARCHAR(6), `parent_id` INTEGER, `list_order` INTEGER);

我的 SQLite 版本不支持窗口函数,目前我在链接SQLite 查询中的查询返回结果如下,但它不是根据 list_order 排序的,另一个细节是 list_order 列可能所有值都为零。

   id      name_item   parent_id  level  path_index list_order
---------------------------------------------------------------
   1        Pois           0          0     1           4
   2        Então          0          0     2           5
   3        Teste          0          0     3           3
   11       Teste 3        3          1     3.1         2
   12       Teste 2        3          1     3.2         0
   13       Teste 1        3          1     3.3         1
   4        Fundo          0          0     4           2
   7        Profundo       4          1     4.1         1
   8        Dhdhd          4          1     4.2         0
   9        Gagagaga       8          2     4.2.1       1
   10       Fsfsfsfs       8          2     4.2.2       0
   5        Profundo       0          0     5           1
   6        Gigante        0          0     6           6
   14       Teste          0          0     7           0

我希望 path_index 列遵循 list_order 和 level 列,如下所示:

   id      name_item   parent_id  level  path_index list_order
---------------------------------------------------------------
   14       Teste          0          0     1           0
   5        Profundo       0          0     2           1
   4        Fundo          0          0     3           2
   8        Dhdhd          4          1     3.1         0
   10       Fsfsfsfs       8          2     3.1.1       0
   9        Gagagaga       8          2     3.1.2       1
   7        Profundo       4          1     3.2         1
   3        Teste          0          0     4           3
   12       Teste 2        3          1     4.1         0
   13       Teste 1        3          1     4.2         1
   11       Teste 3        3          1     4.3         2
   1        Pois           0          0     5           4
   2        Então          0          0     6           5
   6        Gigante        0          0     7           6

如何对齐根据 list_order 和 level 列排序的 path_index?

sqlite order-by
  • 1 1 个回答
  • 116 Views

1 个回答

  • Voted
  1. Best Answer
    Akina
    2021-04-30T02:12:50+08:002021-04-30T02:12:50+08:00

    下一个查询可以执行所需的排序:

    WITH
    cte AS ( SELECT *, CAST(list_order AS TEXT) as ord
             FROM items
             WHERE parent_id = 0
           UNION ALL
             SELECT items.*, cte.ord || '.' || items.list_order
             FROM items
             JOIN cte ON cte.id = items.parent_id )
    SELECT * 
    FROM cte
    ORDER BY ord
    

    https://dbfiddle.uk/?rdbms=sqlite_3.27&fiddle=a7957edeabb5a9a1c9dff2d2e92ffe0c

    如果存在list_order上述值,9则必须对其值应用零填充表达式。例如

    WITH
    cte AS ( SELECT *, printf('%04d', list_order) as ord
             FROM items
             WHERE parent_id = 0
           UNION ALL
             SELECT items.*, cte.ord || printf('%04d', items.list_order)
             FROM items
             JOIN cte ON cte.id = items.parent_id )
    SELECT * 
    FROM cte
    ORDER BY ord
    

    https://dbfiddle.uk/?rdbms=sqlite_3.27&fiddle=52d6d43db054cb64754c37d3820c0b1e


    您有什么技巧可以使零填充动态化吗?– 亚历山大9865

    WITH
    printf_format AS ( SELECT '%0' || LENGTH(MAX(list_order)) || 'd' AS format
                       FROM items ),
    cte AS ( SELECT items.*, printf(printf_format.format, list_order) as ord
             FROM items
             CROSS JOIN printf_format
             WHERE parent_id = 0
           UNION ALL
             SELECT items.*, cte.ord || printf(printf_format.format, items.list_order)
             FROM items
             JOIN cte ON cte.id = items.parent_id
             CROSS JOIN printf_format )
    SELECT * 
    FROM cte
    ORDER BY ord
    

    https://dbfiddle.uk/?rdbms=sqlite_3.27&fiddle=728695f41db0913bcde76370f70c885a

    • 3

相关问题

  • SQLite 是否有可用于数据复制的免费软件?[关闭]

  • 向此查询添加 order by 会比不添加 order by 返回更快,为什么?

  • 在客户端应用程序上使用 CoreData

  • SQLite 的限制

  • 是否可以将 SQLite 用作客户端-服务器数据库?[关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve