gpt4 book ai didi

postgresql - 在Postgresql中Order Query Results by int[] Used in Query

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

我想使用与以下类似的查询来查询表:

SELECT * FROM table WHERE id=ANY(ARRAY[454, 345, 987, 247]) ORDER BY ?;

有没有办法替换?在 ORDER BY 中使用将按 ARRAY[454, 345, 987, 247] 中整数的索引顺序对行进行排序的东西?

最佳答案

CREATE EXTENSION intarray;

SELECT *
FROM table
WHERE id=ANY(ARRAY[454, 345, 987, 247])
ORDER BY idx(ARRAY[454, 345, 987, 247], id);

虽然只适用于整数数组。对于其他人,您必须编写自己的 idx(而且不会很快)。

参见 intarray .

我认为在 9.4 中,使用 UNNEST(...) WITH ORDINALITY 可以更快地完成此操作。

关于postgresql - 在Postgresql中Order Query Results by int[] Used in Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25330820/

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