我目前正在尝试将日期与我datetime
数据库集中的值进行比较。格式为yyyy-mm-dd 00:00:00
。我有一个如下所示的SQL字符串:
SELECT * FROM table WHERE (start BETWEEN $from AND $to)
$from
= "2015-14-10 00:00:00"
和$to
= "2016-14-10 00:00:00"
当我预先形成此查询时,我会将此返回给我:
SELECT * FROM Events WHERE (start BETWEEN 2015-14-10 00:00:00 AND 2016-14-10 00:00:00) LIMIT 20 OFFSET 0Error: 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 '00:00:00 AND 2015-14-10 00:00:00) AND subcategory_id = 9999 LIMIT 20 OFFSET 0' at line 1
由此我假设它是我创建的日期时间字符串中的空格。当我在日期时间周围加上引号并将其输入我的数据库时,它会正确查询。在我的查询字符串中,是否需要在变量周围添加引号以使空格有效?
答案 0 :(得分:2)
您错过了必须围绕常量日期值的引号字符。
您之前的查询必须是:
SELECT * FROM Events WHERE(开始BETWEEN'2015-14-10 00:00:00'和'2016-14-10 00:00:00')