Conversion between timestamp and datetime of mysql time attribute

1. Convert datetime to timestamp

    Option 1: Mandatory conversion of field types

use`nec`;
SET SQL_SAFE_UPDATES=0;
ALTER TABLE `usr_user_info` CHANGE COLUMN `registerTime` `registerTime` BIGINT(20) NOT NULL COMMENT '注册时间' ,
ALTER TABLE `usr_user_info` CHANGE COLUMN `lastLoginTime` `lastLoginTime` BIGINT(20) NULL DEFAULT NULL COMMENT '最后登录时间' ;
UPDATE `usr_user_info` SET  `lastLoginTime` = unix_timestamp(`lastLoginTime`);
UPDATE `usr_user_info` SET  `registerTime` = unix_timestamp(`registerTime`);

Option 2: Add temporary columns

--
-- table alter for usr_user_info
--
/*增加字段*/
use `nec`;
ALTER TABLE `usr_user_info` ADD COLUMN tempRegisterTime BIGINT(20) NULL ;
ALTER TABLE `usr_user_info`  ADD COLUMN tempLastLoginTime BIGINT(20) NULL ;

/*进行时间转化,并复制列*/
UPDATE  usr_user_info SET tempRegisterTime=unix_timestamp(registerTime);
UPDATE  usr_user_info SET tempLastLoginTime=unix_timestamp(lastLoginTime);

/*删除原有字段*/
ALTER TABLE usr_user_info
    DROP registerTime,DROP lastLoginTime;

/*更新临时字段名称*/
ALTER TABLE usr_user_info CHANGE tempRegisterTime registerTime BIGINT(20) NOT NULL  COMMENT '注册时间';
ALTER TABLE usr_user_info CHANGE tempLastLoginTime lastLoginTime BIGINT(20)  COMMENT '最近登录时间';

Second, the timestamp is converted to datetime

     A bunch of Google tutorials, mainly involving the conversion function 'FROM_UNIXTIME (registerTime)'

Published 23 original articles · won praise 2 · Views 5256

Guess you like

Origin blog.csdn.net/bianlitongcn/article/details/83781177