2017-06-14 2 views
0

レコードをさまざまな条件で選択し、演算子ANDおよびORを使用するクエリを作成したいと思います。PostgresでのAND OR文の結合

たとえば、次のクエリを書くと、最初の2つの条件(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

を避けるために、あなたの質問、よOR文を括弧で囲むことができます: 'select * from transactions where network_id = 1 and payment_plan_id = 77 and(payment_type = 'Subscription' OR payment_type = 'Renewal')' – RToyo

答えて

1

使用IN私は誤解していない限り、括弧の混乱

select * from transactions 
where network_id = 1 
and payment_plan_id = 77 
and payment_type IN ('Subscription' , 'Renewal') 
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') 
関連する問題