我想写一个查询,它通过许多不同的条件选择记录,并使用运算符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'
答案 0 :(得分:2)
使用括号:
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')
答案 1 :(得分:1)
使用IN
避免括号混淆
select * from transactions
where network_id = 1
and payment_plan_id = 77
and payment_type IN ('Subscription' , 'Renewal')