在 Postgres 中组合 AND OR 语句
Combining AND OR statements in Postgres
我想编写一个查询,根据许多不同的条件选择记录,并使用运算符 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'
使用括号:
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')
使用IN
避免括号混淆
select * from transactions
where network_id = 1
and payment_plan_id = 77
and payment_type IN ('Subscription' , 'Renewal')
我想编写一个查询,根据许多不同的条件选择记录,并使用运算符 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'
使用括号:
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')
使用IN
避免括号混淆
select * from transactions
where network_id = 1
and payment_plan_id = 77
and payment_type IN ('Subscription' , 'Renewal')