mysql中使用sql命令将时间戳解析成datetime类型存入

实现思路

需求

需要将本数据库的数据进行处理(添加前缀),然后导入主数据库。

但是当前数据库记录的create_time、update_time 是bigint 类型,存放的是时间戳。eg.1646124455

而主数据库的 create_time、update_time 是 datetime 类型的字段,所以需要将时间戳解析成时间并存放到对应位置。

  • 给所有的表添加前缀
  • 给所有的表新增字段,用于存储解析后的时间 即 datetime 类型
  • 解析时间戳字段,将解析后的时间存到对应的字段中
  • 删除时间戳的字段
  • 将第二步新增的字段的名称改成create_time、update_time

一、修改库中所有表名,添加前缀

1.sql更改表名

rename table test to test1;

2.sql一次更改多个表名

rename table `name` to name1 , tel to tel1;

3.sql生成批量执行的语句

select concat('rename table ',table_name,' to hts_',table_name,';') 
from information_schema.tables 
where TABLE_SCHEMA ="demo";

4.执行批量生成的所有语句

在这里插入图片描述

二、给库中所有的表添加字段

1.sql给表添加字段

alter table hts_name add column create_time int;

2.sql一次给表中添加多个字段

alter table hts_user_profile 
add column (create_time_date datetime , update_time_date datetime);

3.sql生成批量执行的语句

select concat('alter table ',table_name,' add column (create_time_date datetime , update_time_date datetime);') from information_schema.tables 
where table_name like'hts_%'
and TABLE_SCHEMA ="hts";

三、将时间戳解析并赋值到新的字段

1.sql将表中a字段的值解析后赋值给b字段

update hts_user_profile 
set create_time_date = FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s');
update hts_user_profile 
set update_time_date = FROM_UNIXTIME(update_time,'%Y-%m-%d %H:%i:%s');

2.sql一次更新多个字段的数据

update hts_user_profile set 
create_time_date = FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s'),
update_time_date = FROM_UNIXTIME(update_time,'%Y-%m-%d %H:%i:%s');

3.sql生成批量执行的语句

select concat('update ',table_name,' set create_time_date = FROM_UNIXTIME(create_time,"%Y-%m-%d %H:%i:%s");') 
from information_schema.tables where table_name like'hts_%';
select concat('update ',table_name,' set update_time_date = FROM_UNIXTIME(update_time,"%Y-%m-%d %H:%i:%s");') 
from information_schema.tables where table_name like'hts_%';

四、删除库中所有表的某个字段

1.sql将表的某个字段删除

alter table hts_user_profile drop column create_time;

2.sql生成批量执行的语句

select concat('alter table ',table_name,' drop column create_time;') 
from information_schema.tables where table_name like'hts_%';

五、修改库中所有表的某个字段名称

1.sql修改表中的某个字段名称

ALTER TABLE hts_user_profile change create_time_date create_time datetime;

2.sql一次修改表的多个字段名称

ALTER TABLE hts_user_profile 
CHANGE create_time_date create_time datetime,
CHANGE update_time_date update_time datetime;

3.sql生成批量执行的语句

select concat('alter table ',table_name,' CHANGE create_time_date create_time datetime,CHANGE update_time_date update_time datetime;') 
from information_schema.tables where table_name like'hts_%';

汇总

/*
前提:项目库存在本地mysql,从库(需要数据迁移的库)拷贝到本地数据库;

1.修改所有的从库表名,添加需要的前缀。
2.给所有的从库表添加字段:create_time_date,update_time_date
3.将从库所有的表读取一遍,将时间戳转成时间然后存在新字段中
4.删除从表的create_time 和 update_time 字段
5.修改所有的create_time_date,update_time_date 字段名为 create_time 和 update_time
6.同步数据(可在Navicat执行)
*/

-- 1.修改所有的从库表名,添加需要的前缀。
select concat('alter table ',table_name,' rename to ',table_name) from information_schema.tables where table_name like'dmsck_%';

-- 2.给所有的从库表添加字段:create_time_date,update_time_date
alter table hts_user_profile add column (create_time_date datetime , update_time_date datetime);

alter table hts_user_profile add column create_time int;

select concat('alter table ',table_name,' add column (create_time_date datetime , update_time_date datetime);') from information_schema.tables 
where table_name like'hts_%';

-- 3.将从库所有的表读取一遍,将时间戳转成时间然后存在新字段中

update hts_user_profile set create_time_date = FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s');
update hts_user_profile set update_time_date = FROM_UNIXTIME(update_time,'%Y-%m-%d %H:%i:%s');

SELECT * FROM hts_user_profile WHERE create_time != update_time;

select concat('update ',table_name,' set create_time_date = FROM_UNIXTIME(create_time,"%Y-%m-%d %H:%i:%s");') from information_schema.tables 
where table_name like'hts_%';
select concat('update ',table_name,' set update_time_date = FROM_UNIXTIME(update_time,"%Y-%m-%d %H:%i:%s");') from information_schema.tables 
where table_name like'hts_%';

-- 4.删除从表的create_time 和 update_time 字段
alter table hts_user_profile drop column create_time;
alter table hts_user_profile drop column update_time;

select concat('alter table ',table_name,' drop column create_time;') from information_schema.tables 
where table_name like'hts_%';
select concat('alter table ',table_name,' drop column update_time;') from information_schema.tables 
where table_name like'hts_%';

-- 5.修改所有的create_time_date,update_time_date 字段名为 create_time 和 update_time

ALTER TABLE hts_user_profile CHANGE create_time_date create_time datetime,CHANGE update_time_date update_time datetime;

select concat('alter table ',table_name,' CHANGE create_time_date create_time datetime,CHANGE update_time_date update_time datetime;') from information_schema.tables 
where table_name like'hts_%';
作者:梦若辰宇原文地址:https://blog.csdn.net/Jason_A/article/details/125300563

%s 个评论

要回复文章请先登录注册