2

I couldn't find a real answer anywhere so I am asking.

Here is my query

("SELECT * FROM messages WHERE message_date < ? AND message_date //not greater than// ? AND to_user = ? OR from_user = ? ORDER BY message_date DESC")

I have no idea how to check that the date is not greater than the defined date

9
  • 1
    Did you try "message_date <= ?"? Commented Nov 26, 2012 at 6:27
  • not greater than nothing but "is less than" Commented Nov 26, 2012 at 6:27
  • 1
    not(message_date > new Date) Commented Nov 26, 2012 at 6:28
  • AND takes precedence over OR, so you might want to wrap the OR in parentheses. Commented Nov 26, 2012 at 6:38
  • Ok I saw a comment about redundancy, so i want to clarify, the < statement at the beginning of the query. For some reason it is showing things that are > the date that I have set.. Instead of only showing items that are < the defined date it is also showing items from > too and I cannot figure out why, this is why I asked the question. I hope that clarifies that my questions isn't entirely moronic. Commented Nov 26, 2012 at 6:39

4 Answers 4

3

There's two ways to write it.

One option is to simply use <= since "not greater than" is equivalent to "less than or equal to."

The other is to use the NOT modifier, e.g. ... AND NOT (message_date > ?).

Sign up to request clarification or add additional context in comments.

1 Comment

x<=y is even eqivalent to NOT(x>y) if either argument can be null.
1

'Not greater than' is the same as saying 'less than or equal to', so:

SELECT * FROM messages
WHERE message_date < ? AND message_date <= ? AND to_user = ? OR from_user = ?
ORDER BY message_date DESC

Comments

0

You just need to check if it is less, but if you want to say not, you can use not!

not(message_date > ?)

2 Comments

is new Date valid in MySQL?
new Date shoudl be replaced with the ?
0

maybe you want like this,

SELECT * 
FROM messages 
WHERE message_date <= ?  AND
      ? IN (to_user, from_user)
ORDER BY message_date DESC

when you say not greater than, it just means less than or equal to

2 Comments

You're missing the equality case.
? IN (to_user, from_user) may not be equivalent to ? = to_user OR ? = from_user (although it is in a considerable amount of use-cases).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.