【问题标题】:Combining AND OR statements in Postgres在 Postgres 中组合 AND OR 语句
【发布时间】:2017-11-16 21:30:06
【问题描述】:

我想编写一个查询,通过多个不同的条件选择记录并使用运算符 AND 和 OR。

如果我编写以下查询,例如,我如何保留前两个条件(network_id = 1 and payment_plan_id = 77)而不在OR 语句之后重写它们?

select * from transactions where network_id = 1 and payment_plan_id = 
77 and payment_type = 'Subscription' OR payment_type = 'Renewal'

【问题讨论】:

  • 除非我误解了您的问题,否则您只需将 OR 语句括在括号中即可:select * from transactions where network_id = 1 and payment_plan_id = 77 and (payment_type = 'Subscription' OR payment_type = 'Renewal')

标签: sql postgresql logical-operators


【解决方案1】:

使用括号:

select *
from transactions
where network_id = 1 and payment_plan_id = 77 and 
      (payment_type = 'Subscription' OR payment_type = 'Renewal')

或者,更好的是,使用in

select *
from transactions
where network_id = 1 and payment_plan_id = 77 and 
      payment_type in ('Subscription', 'Renewal')

【讨论】:

    【解决方案2】:

    使用IN避免括号混淆

    select * from transactions 
    where network_id = 1 
    and payment_plan_id = 77 
    and payment_type IN ('Subscription' , 'Renewal')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多