mysql - mysql 日期查询失败

标签 mysql sql sql-delete mysql-error-1054

我想从表中删除包含超过 7200 秒的列的行。

查询

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

地点:

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

该查询旨在删除日志值为 2011-06-25 09:56:51 的行,因为它早于 7200 秒,但事实并非如此。

我哪里做错了?

最佳答案

DATE_ADD 和括号之间有一个空格:( log, ...

使用DATE_ADD(log, ...

来自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.

还有:

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.

关于mysql - mysql 日期查询失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6581410/

相关文章:

php - 无法连接到 mySQL 数据库

sql - 以编程方式构建 SQL 查询的可靠方法

java - 当 where 子句条件可以为 null 时更新 SQL

mysql - 根据最大值删除一行

sql - oracle sql中删除大记录

mysql - 如何使包含两个父表的内部连接的 sql 查询?

mysql - 为什么 MySQL 不接受我的外键?

sql - 为相等性和时间戳范围查询创建索引

mysql - 删除一条Mysql记录

java - 如何在 MySQL 的 SQL 查询中将 ArrayList<> 作为 IN 子句传递