hammy
hammy

Reputation: 23

Multiple AND OR logic in the where clause

Consider the following scenario

I want to select all columns from table A where

  1. SubscribeDate is between 2016-01-01 and now() and clubid=2
  2. timesbilled>0 and clubid=2

I am trying the following (The sample is big enough to have the sample)

Select *

From Table A

Where 
SubscribeDate between '2016-01-01' and now()
and clubid=2
OR
timesbilled>0

Will the query meet my requirement?

Upvotes: 0

Views: 44

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520908

I would phrase your query as:

SELECT *
FROM TableA
WHERE (SubscribeDate BETWEEN '2016-01-01' AND NOW() OR timesbilled > 0) AND
      clubid = 2

Both of your listed conditions require a clubid of 2. The other requirement is that the date condition or billing condition be true.

Upvotes: 2

Kuru
Kuru

Reputation: 738

Try the following

Select *

From Table A

Where 
(SubscribeDate between '2016-01-01' and now()
and clubid=2)
OR
(timesbilled>0 and clubid=2);

Upvotes: 1

Related Questions