MySQL assigns a date field in the same data table to another timestamp field

Let me talk about the requirements first:

When the user_amount table was previously designed, the field to save the date was creatTime, the type was timestamp, and the date in the format of '2021-02-01 17:50:21' was saved.

After the design of the user_amount table is changed, you need to save the date in the create_time field, type int, and save the timestamp in the format "1612173021".

DROP TABLE IF EXISTS `user_amount`;
CREATE TABLE `user_amount` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `creatTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `create_time` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

1. A function to convert a timestamp to a date: FROM_UNIXTIME

select FROM_UNIXTIME('1612173021','%Y年%m月%d') from user_amount;

Second, the function to convert the date into a timestamp: UNIX_TIMESTAMP

select UNIX_TIMESTAMP('2021-02-01 17:50:21') from user_amount;

3. Assign the date field in the data table to another timestamp field:

1. Before executing the code

select id,creatTime,create_time from user_amount;

2. Update operation

UPDATE user_amount u1, pay_user_amount u2 SET u1.create_time = UNIX_TIMESTAMP(u2.creatTime) where u1.id=u2.id;

3. The result after executing the update

Guess you like

Origin blog.csdn.net/qq15577969/article/details/113749549