我有这样的记录:
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]
关于sql - 按 hstore 中任意属性的值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26591244/