是否可以通过 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/