views:

495

answers:

2

When I use this query:

SELECT COUNT(*) FROM `my_table` WHERE DATEDIFF(NOW(), updated) > 2

MySQL executes the query with no errors and I get the count of rows that have not been updated within the last 2 days. However, if I change the query like this:

SELECT * FROM `my_table` WHERE DATEDIFF(NOW(), updated) > 2

I get the following error:

#1305 - FUNCTION mydatabase.DATEDIFF does not exist

Any ideas why this is so?

+2  A: 

Verify that you don't have a space between DATEDIFF and the bracket (.

Possibly you could also try SET sql_mode = "IGNORE_SPACE";

Also check this bugreport.

Yannick M.
Thanks for the reply. I definitely don't have a space when I enter the query using phpMyAdmin. However, it may be that phpMyAdmin is modifying the query before it is executed. As an experiment, I tried a newer version of phpMyAdmin (3.1.3.1) on my localhost, which executed the problem query just fine. On my production server (which is hosted by a 3rd-party) the phpMyAdmin version is 2.6.4-pl3. On my production server, phpMyAdmin is the only means I have of executing an ad-hoc query, since I don't have direct mysql shell access.
Geoff
Well you can make certain phpMyAdmin isn't altering the query string and adding a space by setting sql_mode to `"IGNORE_SPACE"`. If this doesn't help as Frankie and the people in the bugreport have suggested, adding LIMIT apparently makes it parse correctly.
Yannick M.
Setting sql_mode to IGNORE_SPACE also works. Thanks again for your help.
Geoff
+1  A: 

I've had a similar problem where it would not work on SELECT if I had not set the LIMIT.

Try to do something like:

SELECT * FROM `my_table` WHERE DATEDIFF(NOW(), updated) > 2 LIMIT 0, 10

It got corrected on a MySQL update. Try to update your MySQL version also.

Frankie
Interestingly, phpMyAdmin was adding a LIMIT clause before executing the query, which fails. If I add the LIMIT to the query myself, as you suggest, the query executes fine. Must be a bug in phpMyAdmin version 2.6.4-pl3. Thanks for the help.
Geoff