mysql 查询 - 插入数据 unix_timestamp ( now () ) 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11558418/
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 query - insert data unix_timestamp ( now ( ) ) issue
提问by Reteras Remus
I have an INT (11) column for storing the current timestamp in seconds. The query looks like:
我有一个 INT (11) 列,用于以秒为单位存储当前时间戳。查询如下所示:
INSERT INTO `abc` (id, timestamp) VALUES ('', UNIX_TIMESTAMP ( NOW () ) )
I don't know why, but the date isn't changed. It doesn't matter when I send the query, the column value isn't changed. It has 1342692014 value, but I don't know why.
我不知道为什么,但日期没有改变。发送查询时无关紧要,列值不会更改。它有 1342692014 值,但我不知道为什么。
Is there any option or other function for timestamps? I must store dates in seconds.
时间戳是否有任何选项或其他功能?我必须以秒为单位存储日期。
回答by álvaro González
You never refer to the timestamp
columnin your query. You only have a string:
您永远不会在查询中引用该timestamp
列。你只有一个字符串:
INSERT INTO `abc` (id, 'timestamp') VALUES ('', UNIX_TIMESTAMP ( NOW () ) )
^^^^^^^^^^^
Edit:
编辑:
I get this with your updated code:
我用你更新的代码得到了这个:
ERROR 1630 (42000): FUNCTION test.NOW does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual
错误 1630 (42000):FUNCTION test.NOW 不存在。检查参考手册中的“函数名称解析和解析”部分
Assuming it's not still the actual code and after fixing the syntax error, I can't reproduce your results. My educated guess is that id
is an auto-incremented integer primary key, your current SQL mode is making MySQL take ''
as NULL
and inserting a new row... But I haven't really tested this hypothesis.
假设它不是实际代码并且在修复语法错误后,我无法重现您的结果。我有根据的猜测是这id
是一个自动递增的整数主键,您当前的 SQL 模式使 MySQL''
成为NULL
并插入新行......但我还没有真正测试过这个假设。
My working code is this:
我的工作代码是这样的:
CREATE TABLE `abc` (
`pk` INT(10) NOT NULL AUTO_INCREMENT,
`id` VARCHAR(10) NULL DEFAULT NULL,
`timestamp` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`pk`)
)
ENGINE=InnoDB;
INSERT INTO abc (id, timestamp) VALUES ('', UNIX_TIMESTAMP());
-- Wait a few seconds
INSERT INTO abc (id, timestamp) VALUES ('', UNIX_TIMESTAMP());
-- Wait a few seconds
INSERT INTO abc (id, timestamp) VALUES ('', UNIX_TIMESTAMP());
SELECT timestamp FROM abc WHERE id='';
... and returns this:
...并返回:
+------------+
| timestamp |
+------------+
| 1342694445 |
| 1342694448 |
| 1342694450 |
+------------+
3 rows in set (0.00 sec)