如何在python中使用带有datetime字段的like查询
更新时间在mysql中是时间戳类型。我想在python中执行以下sql如何在python中使用带有datetime字段的like查询,python,mysql,Python,Mysql,更新时间在mysql中是时间戳类型。我想在python中执行以下sql select * from test_table where update_time like "%2017-05-11%" 我用python这样写 sql="select * from test_table where update_time like %s;" cursor.execute(sql,("%2017-05-11%",)) Warning: Incorrect datetime value: '%2017
select * from test_table where update_time like "%2017-05-11%"
我用python这样写
sql="select * from test_table where update_time like %s;"
cursor.execute(sql,("%2017-05-11%",))
Warning: Incorrect datetime value: '%2017-05-10%' for column 'update_time' at row 1
它确实得到了结果,但我也得到了这样的警告
sql="select * from test_table where update_time like %s;"
cursor.execute(sql,("%2017-05-11%",))
Warning: Incorrect datetime value: '%2017-05-10%' for column 'update_time' at row 1
如何避免警告?我认为没有必要添加第一个
%
,因此删除第一个%
,它不会返回警告:
cursor.execute(sql,("2017-05-11%",))
实际上,如果您运行这个sql语句
select * from test_table where update_time like '2017-01-%';
它仍然返回警告,当您使用类似于运算符的时,实际上是在进行字符串比较<代码>更新时间
将动态转换为字符串,因此这里有避免这种情况的方法,您可以使用cast
函数:
强制转换函数和运算符允许从一个数据转换值
键入另一个
e、 g:
工作正常,没有更多的警告。事实上,这里有一个更好的方法,您可以使用它来使用索引,在这种情况下,日期字符串将转换为日期时间
select * from test_table where update_time between '2017-05-11 00:00:00' and '2017-05-11 23:59:59';