Zaki Aziz
Zaki Aziz

Reputation: 3882

Select rows with certain combination

First of all let me give you guys the SQLFiddle: http://sqlfiddle.com/#!2/1d8bd/11

I've been working on this for a while and am facing some programmers (and writers) block.

This is a sample pseudo code of what I am trying to accomplish in MySQL:

Return all rows WHERE {
  (`to_user_id` = '27' AND `to_delete` >= SUBDATE(NOW(), INTERVAL 720 HOUR) )
      AND 
  (`from_user_id` = '27' AND `from_delete` >= SUBDATE(NOW(), INTERVAL 720 HOUR) )
}

Upvotes: 0

Views: 55

Answers (2)

peterm
peterm

Reputation: 92845

Are you looking for this (using OR instead of AND between groups of conditions)?

SELECT * 
  FROM messages
 WHERE (to_user_id   = 27 AND `to_delete`   >= NOW() - INTERVAL 720 HOUR)
    OR (from_user_id = 27 AND `from_delete` >= NOW() - INTERVAL 720 HOUR)

Here is SQLFiddle demo

Upvotes: 1

sgeddes
sgeddes

Reputation: 62861

One issue you're having is your to_delete and from_delete fields are both NULL in your Fiddle example. Therefore, they can never meet your WHERE criteria.

That apart, no need for parentheses if all of your operators are AND. Perhaps you're looking for OR instead?

Upvotes: 1

Related Questions