PostgreSQL JsonB 不能对数字使用 IN 运算符

PostgreSQL JsonB can't use IN operator on numbers

我有一个 table 有 4 列和以下类型。

|id          |businessId|type       |data |
|VARCHAR(100)|BIGINT    |VARCHAR(50)|jsonb|

我的查询需要从 data depID 中获取类型为 'XXX' 和字段的所有项目,这些项目具有以下值之一1 2 或 3。

这是我的查询目前的样子

select * from test_table where type='XXX' and data ->> 'depID' IN (1, 2, 3);

上面的查询给出了以下错误:

[42883] ERROR: operator does not exist: text = integer Hint: No operator matches the given name and argument types. You might need to add explicit type casts.

depID 是 json.

中的类型编号

同样的查询对字符串值也适用,例如:

select * from test_table where type='XXX' and data ->> 'loc' IN ('NYC', 'BST', 'None');

loc在json.

中是字符串类型

我知道它无法确定 depID 是数字类型,但我不确定如何指定它。

如何指定depID的类型为number?

编辑:

原来我在阅读旧规范。我正在使用 Postgres 11.4

正确的语法如下。

select * from test_table where type='XXX' and (data->>'depID'):NUMERIC IN (1, 2, 3);

->> 运算符有 returns 字符串。这解释了为什么字符串字段无需转换即可工作。

->> 运算符的结果数据类型为 text,因此它不能 return 数字。

可以为数字添加一个特殊的访问运算符,但这只会使 ooerator 命名空间不必要地混乱,因为它可以通过简单的类型转换来完成:

CAST (data ->> 'depID' AS integer) IN (1, 2, 3)