SQL - IN运算符获取位置

Pau*_*Pau 2 sql postgresql

是否可以使用运算符获取参数列表的第一个参数的位置(索引)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
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 5

你不能用"普通"IN子句来做这个,但是你可以用一个数组和不需要的函数来做到这一点:

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;
Run Code Online (Sandbox Code Playgroud)

但是,如果要搜索的值不在数组中,则根本不会显示任何行.