Jason Small
Jason Small

Reputation: 1054

MYSQL Query using AND OR

I have a query where I need to select rows where the a I'm checked if 2 fields are newer than a timestamp that I am providing. I need to provide the result if either or both the fields are newer.

In my mind this is how I think that it should work, but I know that you can't have "AND OR" is there some other command that should be using?

SELECT pid 
from   parts 
WHERE  ( lastupdate > '2011-11-29 17:30:57' ) 
       AND OR (othertimestamp > '2011-11-29 17:30:57') 

Upvotes: 2

Views: 67

Answers (2)

Conrad Frix
Conrad Frix

Reputation: 52675

Its just OR that you want

SELECT pid 
from   parts 
WHERE  (lastupdate > '2011-11-29 17:30:57') 
OR     (othertimestamp > '2011-11-29 17:30:57')

If you want to show which if one or both satisfied the condition you could add this

SELECT pid , 
       CASE
            WHEN  lastupdate > '2011-11-29 17:30:57' AND othertimestamp > '2011-11-29 17:30:57'  THEN 'both'
            WHEN lastupdate > '2011-11-29 17:30:57' THEN 'lastupdate'
            WHEN othertimestamp > '2011-11-29 17:30:57' THEN 'othertimestamp'
       END as which_field

FROM   parts 
WHERE  (lastupdate > '2011-11-29 17:30:57') 
OR     (othertimestamp > '2011-11-29 17:30:57')

Upvotes: 2

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29985

Just use OR - 1 OR 1 still returns true. It's not exclusive OR, which would be XOR. In your case, AND OR is simply OR.

Upvotes: 0

Related Questions