gpt4 book ai didi

SQL - IN 运算符获取位置

转载 作者:行者123 更新时间:2023-11-29 11:53:10 25 4
gpt4 key购买 nike

是否可以通过 IN 运算符获取参数列表的第一个参数的位置(索引)?

我正在寻找的结果如下:

SELECT 2 IN(2, 3, 1); -- Result I want is 0 but with IN is true
SELECT 3 IN(2, 3, 1); -- Result I want is 1 but with IN is true
SELECT 0 IN(2, 3, 1); -- Result I want is -1 but with IN is false
SELECT 1 IN(1, 1, 3); -- RESULT I WANT IS 0 ,1 but with IN is true

最佳答案

你不能用一个“普通”的 IN 子句来做到这一点,但你可以用一个数组和 unnest 函数来做到这一点:

select t.idx
from unnest(array[2,3,1]) with ordinality t(v,idx)
where t.v = 2;

select t.idx
from unnest(array[1, 1, 3]) with ordinality t(v,idx)
where t.v = 1;

但是,如果要搜索的值不在数组中,您将根本找不到任何行。

关于SQL - IN 运算符获取位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39141582/

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