1

How do these results make sense? I don't understand.
Shouldn't for example the last query return an empty set since that there are no threads with date > 2010? Why does it return a result from 2003?

mysql> SELECT * 
       FROM thread 
       WHERE newsgroup_id = '64654' 
       AND 'thread_date' < '2010-09-10 21:43:05' 
       LIMIT 1;

Empty set (0.00 sec)

mysql> SELECT * 
       FROM thread 
       WHERE newsgroup_id = '64654' 
       AND 'thread_date' < '2000-09-10 21:43:05' 
       LIMIT 1;

Empty set (0.00 sec)

mysql> SELECT * 
       FROM thread 
       WHERE newsgroup_id = '64654' 
       AND 'thread_date' > '2000-09-10 21:43:05' 
       LIMIT 1;
+--------------+-----------+-----------+----------+---------------------+---------------------+---------------------------------+-------------------------+
| newsgroup_id | thread_id | postcount | hash     | thread_date         | thread_date_last        | thread_title                    | title_has_valid_charset |
+--------------+-----------+-----------+----------+---------------------+---------------------+---------------------------------+-------------------------+
|        64654 |         1 |         0 | O2gvcPRl | 2003-06-06 22:51:24 | 0000-00-00 00:00:00 | Vendo fotodigit  2.1  megapixel |                       0 |
+--------------+-----------+-----------+----------+---------------------+---------------------+---------------------------------+-------------------------+

1 row in set (0.00 sec)

mysql> SELECT * 
       FROM thread 
       WHERE newsgroup_id = '64654' 
       AND 'thread_date' > '2010-09-10 21:43:05' 
       LIMIT 1;
+--------------+-----------+-----------+----------+---------------------+---------------------+---------------------------------+-------------------------+
| newsgroup_id | thread_id | postcount | hash     | thread_date         | thread_date_last    | thread_title                    | title_has_valid_charset |
+--------------+-----------+-----------+----------+---------------------+---------------------+---------------------------------+-------------------------+
|        64654 |         1 |         0 | O2gvcPRl | 2003-06-06 22:51:24 | 0000-00-00 00:00:00 | Vendo fotodigit  2.1  megapixel |                       0 |
+--------------+-----------+-----------+----------+---------------------+---------------------+---------------------------------+-------------------------+

1 row in set (0.00 sec)

3
  • 1
    If I'm correct, you can't do this. This is just an ASCII comparison (text based) and that's why you are getting these weird but 'correct' comparisons. Look up how to compare date/time in mysql Commented Aug 14, 2011 at 18:49
  • 1
    @Nupul: you do can compare datetime as if they were strings in MySQL Commented Aug 14, 2011 at 18:55
  • @ypercube: My bad...should have clarified. I meant the overall comparison is string based (both LHS and RHS of <,>) Commented Aug 14, 2011 at 19:19

2 Answers 2

7

You are using wrong type of quotes. MySQL thinks that you are comparing string "thread_date". Here is how it should be:

`thread_date` > '2010-09-10 21:43:05'

This way MySQL knows that you mean field thread_date and compares it to the date at the right side of expression.

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

1 Comment

Or no backquotes at all: ... AND thread_date > '2010-09-10 21:43:05' ...
0

'thread_date' is a string, not the value of that field.

You meant:

`thread_date` > '2010-09-10 21:43:05'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.