使用JSON9.4,是否可以在带有比较运算符的PostgreSQL数据类型中找到数值(例如。给我所有的记录,JSON列中的age属性优于18)?
CREATE TABLE data
(
id serial NOT NULL,
attributes jsonb
);
INSERT INTO data (id, attributes) VALUES (1, '{"name": "Value A", "value": 20}');
INSERT INTO data (id, attributes) VALUES (2, '{"name": "Value B", "value": 10}');我想知道如何查询这个表,以获得"value“属性优于18的所有记录。
在本例中,唯一的结果是使用id 1进行记录。
等式是有效的(但它是一个字符串比较):
SELECT * from data WHERE attributes->>'value' = '10';如何处理数字?
SELECT * from data WHERE attributes->>'value' > 18;
==> ERROR: operator does not exist: text > integer
SELECT * from data WHERE attributes->>'value'::integer > 18;
==> ERROR: invalid input syntax for integer: "value"谢谢。
发布于 2015-01-07 11:48:59
在计算优先级方面,::强制转换运算符几乎先于任何其他运算符(.除外),因此您需要添加括号:
SELECT * from data WHERE (attributes->>'value')::integer > 18;符合标准的备选办法:
SELECT * from data WHERE cast(attributes->>'value' AS integer) > 18;https://stackoverflow.com/questions/27817613
复制相似问题