1

我试图在 MySQL 表中节省时间。但是,日期恢复为 2000-01-01。

1.9.2p320 :036 > vv = Visitor.new
 => #<Visitor id: nil, ip_address: nil, num_day_visits: nil, last_visit: nil> 
1.9.2p320 :037 > vv.last_visit = Time.now; vv.ip_address = "3.3.3.3"
 => "3.3.3.3" 
1.9.2p320 :038 > vv.num_day_visits = 1
 => 1 
1.9.2p320 :039 > vv
 => #<Visitor id: nil, ip_address: "3.3.3.3", num_day_visits: 1, last_visit: "2012-10-11 01:31:04"> 
1.9.2p320 :040 > vv.save
  SQL (0.2ms)  BEGIN
  SQL (0.7ms)  INSERT INTO `visitors` (`ip_address`, `last_visit`, `num_day_visits`) VALUES (?, ?, ?)  [["ip_address", "3.3.3.3"], ["last_visit", 2012-10-11 01:31:04 -0400], ["num_day_visits", 1]]
   (0.5ms)  COMMIT
 => true 
1.9.2p320 :042 > vv
 => #<Visitor id: 1199, ip_address: "3.3.3.3", num_day_visits: 1, last_visit: "2012-10-11 01:31:04"> 
1.9.2p320 :043 > Visitor.find(:all,:conditions=>{:ip_address => "3.3.3.3"})
  Visitor Load (1.4ms)  SELECT `visitors`.* FROM `visitors` WHERE `visitors`.`ip_address` = '3.3.3.3'
 => [#<Visitor id: 1199, ip_address: "3.3.3.3", num_day_visits: 1, last_visit: "2000-01-01 05:31:04">] 

所以当我检索记录时,日期是 2000-01-01。

MySQL中的表:

mysql> describe visitors ;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment |
| ip_address     | varchar(255) | NO   |     | NULL    |                |
| num_day_visits | int(11)      | NO   |     | NULL    |                |
| last_visit     | time         | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

更新:我创建了一个玩具表来玩从时间到日期时间的转换。这就是发生的事情:

mysql> select * from example ;
+----+----------+
| id | mytime   |
+----+----------+
|  1 | 11:13:00 |
+----+----------+
1 row in set (0.00 sec)
mysql> alter table example change mytime mytime datetime;
Query OK, 1 row affected, 1 warning (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 1

mysql> select * from example ;
+----+---------------------+
| id | mytime              |
+----+---------------------+
|  1 | 0000-00-00 00:00:00 |
+----+---------------------+
1 row in set (0.00 sec)

所以这破坏了价值。我试图通过将表格更改为时间并使用新行来返回。重新开始:

mysql> select * from example ;
+----+----------+
| id | mytime   |
+----+----------+
|  2 | 11:13:00 |
+----+----------+
1 row in set (0.00 sec)
mysql> ALTER TABLE example CHANGE mytime mytime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP();
ERROR 1067 (42000): Invalid default value for 'mytime'
mysql> ALTER TABLE example CHANGE mytime mytime DATETIME NOT NULL DEFAULT CURRENT_DATE();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURRENT_DATE()' at line 1

显然,这很难

4

3 回答 3

6

这是因为您的last_visit列属于time而不是datetime类型。最初我假设 mysql 只是01-01-2000用作某种默认日期来在内部表示时间字段,但似乎 01-01-2000 是 rails 而不是 mysql。看看在红宝石中表示没有日期的时间

于 2012-10-11T06:00:28.700 回答
1

在你的 mysql 提示符下运行它,

ALTER TABLE 访问者更改 last_visit last_visit 日期时间;

于 2012-10-11T06:01:43.367 回答
0

使用AshishBeck的答案了解我的问题时,我只是将时间存储在 MySQL 中,而 rails 正在为其添加默认的 2000-01-01。我试图找到一种方法来更新现有数据以包含日期,使用今天的日期来更新记录。以下是我能得到的。

假设我们有

mysql> select * from example ;
+----+----------+
| id | mytime   |
+----+----------+
|  2 | 11:13:00 |
+----+----------+
1 row in set (0.00 sec)

这就是我如何将 mytime 更改为更新的日期时间

mysql> alter TABLE example ADD  new_time datetime;
Query OK, 1 row affected (0.04 sec)
mysql> UPDATE example SET new_time = CONCAT( CURDATE(), " ", mytime );
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> ALTER TABLE example DROP mytime ;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE example CHANGE new_time mytime DATETIME;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from example ;
+----+---------------------+
| id | mytime              |
+----+---------------------+
|  2 | 2012-10-11 11:13:00 |
+----+---------------------+
1 row in set (0.00 sec)

在一个示例中,我找不到一种方法来结合数据更新和更改字段类型。有人有更好的想法吗?

于 2012-10-11T16:14:56.897 回答