sql - 如何在 JSONB 中查询空数组?

标签 sql json postgresql postgresql-9.6

考虑这个例子:

postgres=# CREATE TABLE emptyarray (fields jsonb);                                                                                                                            
CREATE TABLE                                                                                                                                                                  
postgres=# INSERT INTO emptyarray VALUES ('{"key":["a","b"]}');                                                                                                               
INSERT 0 1                                                                                                                                                                    
postgres=# INSERT INTO emptyarray VALUES ('{"key":[]}');                                                                                                                      
INSERT 0 1                                                                                                                                                                    
postgres=# SELECT * from emptyarray where Fields@>'{"key":["b"]}';                                                                                                            
       fields                                                                                                                                                                 
---------------------                                                                                                                                                         
 {"key": ["a", "b"]}                                                                                                                                                          
(1 row)                                                                                                                                                                       

postgres=# SELECT * from emptyarray where Fields@>'{"key":[]}';                                                                                                               
       fields                                                                                                                                                                 
---------------------                                                                                                                                                         
 {"key": ["a", "b"]}                                                                                                                                                          
 {"key": []}                                                                                                                                                                  
(2 rows)

在第二个查询中,我期望结果中只有一行(具有空数组的一条记录)。但如您所见,结果中有两行。如何使用 @> 语法查询空数组?

我正在使用 PostgreSQL 9.6

最佳答案

你可以使用:

SELECT * from emptyarray where Fields-> 'key' = '[]'::jsonb;

Rextester Demo

关于sql - 如何在 JSONB 中查询空数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46450606/

相关文章:

sql - 按 SQL 进行分组并计数

android - W/System.err : org. json.JSONException

java - Spring MVC 3 AJAX

mysql - PostgreSQL 中的排序 : strings with parenthesis

python - PostgreSQL 和 Python

sql - Postgres 重叠日期范围以供查看

sql - Clickhouse,要数组的列值

mysql - 在插入时创建 MySQL 触发器

php - 在 codeigniter 中通过查询转义订单

javascript - 遍历嵌套的 JSON 对象并添加属性