简体   繁体   English

MYSQL TIMESTAMP比较

[英]MYSQL TIMESTAMP comparison

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 ,存储Name的列和存储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? 我究竟做错了什么?

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'

Live example at SQL Fiddle. SQL Fiddle的实例。

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')

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'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM