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

 更新时间:2022年11月22日 10:06:02   作者:梦若辰宇  
这篇文章主要介绍了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_%';

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • MySQL学习之数据库备份详解

    MySQL学习之数据库备份详解

    本篇文章主要介绍了MySQL中的 数据库备份详解,有需要的朋友可以借鉴参考下,希望可以有所帮助,祝大家多多进步,早日升职加薪
    2021-09-09
  • SQL查询至少连续n天登录的用户

    SQL查询至少连续n天登录的用户

    这篇文章介绍了SQL查询至少连续n天登录用户的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • Mysql 索引 BTree 与 B+Tree 的区别(面试)

    Mysql 索引 BTree 与 B+Tree 的区别(面试)

    这篇文章主要介绍了Mysql索引BTree与B+Tree的区别,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • mysql 8.0.22.0 下载安装配置方法图文教程

    mysql 8.0.22.0 下载安装配置方法图文教程

    这篇文章主要为大家详细介绍了mysql 8.0.22.0 下载安装配置方法图文教程,文中安装步骤介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • Mysql using使用详解

    Mysql using使用详解

    本文主要介绍了Mysql using使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • MySQL5.7免安装版配置图文教程

    MySQL5.7免安装版配置图文教程

    Mysql是一个比较流行且很好用的一款数据库软件,如下记录了我学习总结的mysql免安装版的配置经验,感兴趣的的朋友参考下吧
    2017-09-09
  • 解析MySQL设置当前时间为默认值的方法

    解析MySQL设置当前时间为默认值的方法

    本篇文章是对MySQL设置当前时间为默认值的方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • MySQL 最基本的SQL语法/语句

    MySQL 最基本的SQL语法/语句

    MySQL 最基本的SQL语法/语句,使用mysql的朋友可以参考下。
    2011-09-09
  • mysql myisam 优化设置设置

    mysql myisam 优化设置设置

    mysql myisam 优化设置设置,需要的朋友可以参考下。
    2010-03-03
  • MySQL数据库innodb启动失败无法重启的解决方法

    MySQL数据库innodb启动失败无法重启的解决方法

    这篇文章给大家分享了MySQL数据库innodb启动失败无法重启的解决方法,通过总结自己遇到的问题分享给大家,让遇到同样问题的朋友们可以尽快解决,下面来一起看看吧。
    2016-09-09

最新评论