Search code examples
mysqlsqlsql-deletemysql-error-1054

Failed mysql date query


I want to delete rows from a table that has a column more than 7200 secs old.

The Query

mysql_query("delete from logged where DATE_ADD ( log, INTERVAL $this->loginDuration SECOND) < NOW()",$this->link);

where:

name of table = logged; 
name of column = log; 
$this->loginDuration = 7200; 

The value of log in db: 2011-06-25 09:56:51.
Todays date and time [ Now() ] : 2011-07-05 11:39:02

The query is meant to delete the row with log value 2011-06-25 09:56:51 because it is older than 7200 seconds but it does not.

What am I not getting right?


Solution

  • You have a space between DATE_ADD and the parenthesis: ( log, ....

    Use DATE_ADD( log, ...

    From MySQL docs, Functions and Operators :

    Note

    By default, there must be no whitespace between a function name and the parenthesis following it. This helps the MySQL parser distinguish between function calls and references to tables or columns that happen to have the same name as a function. However, spaces around function arguments are permitted.

    And:

    You can tell the MySQL server to accept spaces after function names by starting it with the --sql-mode=IGNORE_SPACE option. (See Section 5.1.6, “Server SQL Modes”.) Individual client programs can request this behavior by using the CLIENT_IGNORE_SPACE option for mysql_real_connect(). In either case, all function names become reserved words.