where子句postgres中的JSONB
JSONB in where clause postgres
我有这样的数据-
所以我需要找出所有 id=203498 的行。我如何为此编写查询?
有帮助吗?
这取决于您的数据,因此请添加示例数据,但我会给您示例数据。
如果你的数据是这样的
id | cars_info
----+------------------------------------------------------------------------------------
1 | {"id": 1, "sold": true, "brand": "Toyota", "color": ["red", "black"], "price": 285000}
2 | {"id": 2, "sold": false, "brand": "Honda", "color": ["blue", "pink"], "price": 25000}
3 | {"id": 3, "sold": true, "brand": "Mitsubishi", "color": ["black", "gray"], "price": 604520}
您的查询是这样的。也许它会发生一些错误,但你的查询看起来是这样的。
SELECT * FROM cars WHERE cars_info -> 'id' = '1';
使用 JSON 包含运算符 @>
:
WHERE jsoncol @> '[ { "id" = 203498 }]';
我有这样的数据-
所以我需要找出所有 id=203498 的行。我如何为此编写查询? 有帮助吗?
这取决于您的数据,因此请添加示例数据,但我会给您示例数据。
如果你的数据是这样的
id | cars_info
----+------------------------------------------------------------------------------------
1 | {"id": 1, "sold": true, "brand": "Toyota", "color": ["red", "black"], "price": 285000}
2 | {"id": 2, "sold": false, "brand": "Honda", "color": ["blue", "pink"], "price": 25000}
3 | {"id": 3, "sold": true, "brand": "Mitsubishi", "color": ["black", "gray"], "price": 604520}
您的查询是这样的。也许它会发生一些错误,但你的查询看起来是这样的。
SELECT * FROM cars WHERE cars_info -> 'id' = '1';
使用 JSON 包含运算符 @>
:
WHERE jsoncol @> '[ { "id" = 203498 }]';