简体   繁体   English

根据日期字段更新时间戳列

[英]Updating timestamp column based on datefield

I've got a table with over 50.000 dates and I need to convert it to a Timestamp field.我有一个包含超过 50.000 个日期的表格,我需要将其转换为时间戳字段。

Basic table layout:基本表格布局:

  Fieldname                Type
+------------------------+-----------+
| calendar_date          | DATE      |
| calendar_unixtimestamp | TIMESTAMP |
+------------------------+-----------+

so basically:所以基本上:

update calender set calendar_unixtimestamp = UNIX_TIMESTAMP(calendar_date)

However, wont work, of course.但是,当然行不通。 So I've tried another option which was told me on some goofy website, seems logical.所以我尝试了另一个在一些愚蠢的网站上告诉我的选项,这似乎合乎逻辑。 However can't get it to work:但是无法让它工作:

update
  calendar t1
  join calendar t2 on t2.`calendar_date` = t1.`calendar_date`
set
  t1.calendar_unixtimestamp = UNIX_TIMESTAMP(t2.`calendar_date`)
where
  t1.`calendar_date` = t2.`calendar_date`
  

Anyone?任何人?

The TIMESTAMP data type is used for values that contain both date and time parts, while UNIX_TIMESTAMP returns an unsigned integer. TIMESTAMP 数据类型用于同时包含日期和时间部分的值,而 UNIX_TIMESTAMP 返回一个无符号整数。

You should set calendar_unixtimestamp to INT.您应该将calendar_unixtimestamp设置为 INT。

Please see fiddle here .在此处查看小提琴。

Benjamin.本杰明。

I'm not an expert on mysql, but I think you should read this:我不是 mysql 的专家,但我认为您应该阅读以下内容:

http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html

Especially when it says:尤其是当它说:

By default, the first TIMESTAMP column has both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP if neither is specified explicitly.默认情况下,如果没有明确指定,第一个 TIMESTAMP 列同时具有 DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP。

The on UPDATE clause causes the field value to change to the current timestamp whenever the record is updated, so you update it, then it is automatically changed to the current timestamp, the same (or almost) for all the records in your table. on UPDATE 子句会导致字段值在记录更新时更改为当前时间戳,因此您更新它,然后它会自动更改为当前时间戳,与表中的所有记录相同(或几乎)。

Maybe what you need is a DATETIME column.也许您需要的是 DATETIME 列。 Timestamp columns are intended to register when a record is created and/or updated.时间戳列旨在在创建和/或更新记录时进行注册。 This can be useful for many purposes, including security or conflict resolution when multiple users try to update the same record.这可用于许多目的,包括安全性或在多个用户尝试更新同一记录时解决冲突。

a) If this is what you need, you should first disable the ON UPDATE trigger, then make the update (your first one was OK) and then re-create the trigger(*). a) 如果这是你所需要的,你应该首先禁用 ON UPDATE 触发器,然后进行更新(你的第一个没问题),然后重新创建触发器(*)。 Just add a WHERE condition to make sure that you don't try to update nulls.只需添加一个 WHERE 条件以确保您不会尝试更新空值。 Again, I'm not sure that unix_timestamp is the function you need.同样,我不确定 unix_timestamp 是您需要的功能。

    
    UPDATE calendar
    SET calendar_timestamp = TIMESTAMP(calendar.date) 
    WHERE calendar_date IS NOT NULL

b) If it is not, you simply should change the type of your new column to DATETIME. b) 如果不是,您只需将新列的类型更改为 DATETIME。

(*)Note: Probably it would be easier to drop the column, then create it with the DEFAULT clause but without the ON UPDATE one, or specifying the NULL clause. (*)注意:删除列,然后使用 DEFAULT 子句创建它但不使用 ON UPDATE 或指定 NULL 子句可能会更容易。

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

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