gpt4 book ai didi

sql - 按 hstore 中任意属性的值排序

转载 作者:行者123 更新时间:2023-11-29 11:40:32 24 4
gpt4 key购买 nike

我有这样的记录:

id, hstore_col
1, {a: 1, b: 2}
2, {c: 3, d: 4}
3, {e: 1, f: 5}

如何在 hstore 中为 any 属性按最大/最小值对它们进行排序?

结果应该是这样的(从小到大排序):

id, hstore_col
1, {a: 1, b: 2}
3, {e: 1, f: 5}
2, {c: 3, d: 4}

我知道,我只能按这样的特定属性对它们进行排序:my_table.hstore_fields -> 'a',但这对我的问题不起作用。

最佳答案

使用 avals 转换为数组并将生成的数组从文本转换为整数。然后对数组进行排序,并按排序数组的第一个元素对结果进行排序。

select * from mytable
order by (sort(avals(attributes)::int[]))[1]

http://sqlfiddle.com/#!15/84f31/5

关于sql - 按 hstore 中任意属性的值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26591244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com