1

我的 Qt 应用程序中使用了以下 MySQL 查询:

query.prepare(QString("SELECT productName, value, COUNT(value), SUM(value)"
                  "FROM tblmystock"
                  "WHERE time>=\'%1\' AND time<=\'%2\'"
                  "GROUP BY productName, value").arg(startDateTime, endDateTime));

我收到以下错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near'>='2012-04-24 00:00:00' AND time<='2012-04-24 23:59:59:59' GROUP BY productName' at line 1 QMYSQL: Unable to execute query

即使我尝试绑定值,它也会失败并出现相同的错误。

但是当我在下面的 MySQL 提示符上执行相同的查询时,它工作正常:

mysql> SELECT productName, value, COUNT(value), SUM(value) FROM tblmystock WHERE time>='2012-04-24 00:00:00' AND time<='2012-04-25 23:59:59' GROUP BY productName, value;

有人知道解决方法吗?

4

1 回答 1

1

time是保留关键字。使用反引号来逃避它。
您也可以使用between代替<=>=

WHERE `time` between '2012-04-24 00:00:00' AND '2012-04-25 23:59:59' 
于 2012-04-24T09:45:48.333 回答