我有 table :
CREATE TABLE lc (
id CHARACTER(4),
name TEXT
);
“id”列在有item和subitem的地方有code值,例如:
元素
1, 2, 3 ... 10, 11 ...
子项目:
101, 102, 201, 301 ... 1001, 1002, 1101 ...
其中,101是1的子项,201是2的子项,1001是10的子项,以此类推。
如何使用“Order By”并获得以下订单:
1
101
102
2
201
...
10
1001
1002
11
1101
...
最佳答案
你可以使用这个:
select *
from lc
order by case when id::int < 100 then (id::int::text || '00')::int else id::int end
但是,您的表格确实需要重新设计。
关于sql - 我如何订购商品和子商品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54012367/