MYSQL 时间戳比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14069256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MYSQL TIMESTAMP comparison
提问by SwenMulderij
I have a table with a column Time
that stores a timestamp value, a column that stores a Name
and a column that stores a Status
.
我有一个表,其中一列Time
存储时间戳值,一列存储 aName
和一列存储Status
.
I'm trying to find a query to update all entries before a given timestamp like this:
我试图找到一个查询来更新给定时间戳之前的所有条目,如下所示:
UPDATE `Table`
SET Status=1
WHERE Name='personname' AND 'Time'<'2012-12-23 18:00:00'
The query is valid but nothing changes.
查询有效但没有任何变化。
When trying to show the results of the WHERE
part there are no results.
当试图显示WHERE
零件的结果时,没有结果。
What am I doing wrong?
我究竟做错了什么?
回答by Andomar
You're comparing the string literal 'Time'
:
您正在比较字符串文字'Time'
:
'Time'<'2012-12-23 18:00:00'
Try comparing the time column instead:
尝试比较时间列:
Time < '2012-12-23 18:00:00'
Or if you have to, surround it in backticks:
或者,如果必须,请将其用反引号括起来:
`Time` < '2012-12-23 18:00:00'
回答by Mari
If you are sure about backticks and single quotes and still it doesnt work then try this out,
如果你确定反引号和单引号仍然不起作用,那么试试这个,
UPDATE `Table`SET Status=1
WHERE Name='personname' AND
Time < STR_TO_DATE('2012-12-23 18:00:00','YYYY-MM-DD HH:MI:SS')
回答by rahul
Try This:
尝试这个:
UPDATE `Table`SET Status=1
WHERE Name='personname' AND
Time < UNIX_TIMESTAMP(STR_TO_DATE('23-12-2012 18:00:00',' "%d-%m-%Y %h:%i:%s'));