Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中使用带有datetime字段的like查询_Python_Mysql - Fatal编程技术网

如何在python中使用带有datetime字段的like查询

如何在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

更新时间在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-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';