【问题标题】:Postgres Sorting of concat(date,string)Concat(日期,字符串)的Postgres排序
【发布时间】:2021-12-05 11:33:44
【问题描述】:

我想知道 postgres 如何对以下类型的列表进行排序

  1. 2021-10-18 15:12:09苹果
  2. 2021-10-18 12:10:09男孩
  3. 2021-10-18 11:12:15stack

我在 postgres 中试过,结果如下

with temp_table(a) as (
values ('2021-10-18 15:12:09apple'),
  ('2021-10-18 12:10:09boy'),
 ('2021-10-18 11:12:15stack')
) select * from temp_table order by a

“2021-10-18 11:12:15stack”
《2021-10-18 12:10:09男孩》
"2021-10-18 15:12:09苹果"

这是如何工作的?

【问题讨论】:

  • 为什么这会让您感到惊讶?显然2021-10-18 11:12:15 将被排序before 2021-10-18 12:10:09
  • 想要的顺序是什么?

标签: sql postgresql date sorting


【解决方案1】:

正如预期的那样,它将数据排序为字符串(从最左边的字符开始)

所以2021-10-18 11:12:15stack 2021-10-18 12:10:09boy 2021-10-18 15:12:09apple

因为,除了常见的2021-10-18 之外,11:12作为字符串是12:10作为字符串和15:12作为字符串

【讨论】:

    猜你喜欢
    • 2020-12-14
    • 2019-11-07
    • 1970-01-01
    • 2012-11-09
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    相关资源
    最近更新 更多