SQL 查询 -- 使用 ALL 和“=”运算符未获得预期结果

SQL query -- not getting expected result using ALL and '=' operator

我有一个 table:

id  name
1   A
3   B
2   C
4   D

我正尝试在存储过程中使用以下查询 select 几行:

select * from test where id = all ('{1,2}');

我希望它有 return 行 ID 为 1 和 2,但是,它 return 是空的。

以下查询按预期工作:

select * from test where id <> all ('{3,4}');

它是 returning id 为 1 和 2 的行。我无法理解为什么“=”运算符没有按预期工作但“<>”在工作。我是这种语法的新手。请帮助如何获得使用等于运算符(相当于 IN)的预期结果。

I am expecting it to return rows with ids 1 and 2

你想要any,而不是all

where id = any ('{1,2}');

这带来 id 等于 12。换句话说,这相当于 id in (1, 2).

关于这个表达式:

where id <> all ('{3,4}');

这相当于:

where not (id = any ('{3,4}'));

所以这会过滤掉 ids 34.

关于你原来的表达方式:

where id = all ('{1,2}');

这没有意义;单个值不能同时与数组的所有值匹配 - 所以这会过滤掉所有行。