【问题标题】:How to use logical operator in postgres json query where clause?如何在 postgres json 查询 where 子句中使用逻辑运算符?
【发布时间】:2021-09-18 12:13:50
【问题描述】:

我有一个销售数据库

示例:- # 从销售额中选择 *;

 id |                         info              
        
----+-------------------------------------------------------

   1 | {"name": "Alice", "tags": ["admin"], "paying": true}

   2 | {"name": "Alice", "tags": ["admin"], "paying": false}

   3 | {"name": "Faber", "tags": ["admin"], "paying": false}

OR 运算符和 AND 运算符没有给出正确的结果?

示例:- # select * from sales where info ->> 'name' = 'Faber' OR 'paying' = 'true';

   id |                         info     
                 
  ----+-------------------------------------------------------

    3 | {"name": "Faber", "tags": ["admin"], "paying": false}

  (1 row)


 # select * from sales where info ->> 'name' = 'Alice' AND 'paying' = 'true';

    id | info 

   ----+------

   (0 rows)

为什么运营商不以这种方式行事——他们应该如此行事?

【问题讨论】:

    标签: json postgresql logical-operators


    【解决方案1】:

    您正在测试字符串 'paying' 是否等于字符串 'true',显然不是。

    如果要在逻辑的每一边测试一个json的字段,你需要在逻辑的每一边重复info ->>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 2021-05-26
      • 2011-01-16
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 2020-04-30
      相关资源
      最近更新 更多