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

作者:梦若辰宇 时间:2024-01-22 00:00:25 

实现思路

需求

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

但是当前数据库记录的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.执行批量生成的所有语句

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

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

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

标签:mysql,sql命令,时间戳,datetime类型
0
投稿

猜你喜欢

  • 在Python中操作时间之tzset()方法的使用教程

    2022-10-28 22:22:01
  • vue 修改 data 数据问题并实时显示操作

    2024-05-02 17:00:53
  • Python 使用with上下文实现计时功能

    2022-08-20 02:43:21
  • Django如何创作一个简单的最小程序

    2022-09-04 04:57:28
  • Python利用3D引擎写一个Pong游戏

    2021-09-13 15:56:58
  • django的模型类管理器——数据库操作的封装详解

    2024-01-12 19:12:53
  • 详尽解析javascript的event对象

    2008-01-16 11:27:00
  • python3的url编码和解码,自定义gbk、utf-8的例子

    2022-08-02 10:07:39
  • python获取网页状态码示例

    2023-11-23 00:57:10
  • C#调用python文件执行

    2023-11-16 21:35:42
  • Oracle Index 的三个问题

    2024-01-14 20:20:40
  • PHP仿微信多图片预览上传实例代码

    2024-05-03 15:27:45
  • 使用Filter实现信息的二次检索

    2007-10-08 19:19:00
  • python爬虫神器Pyppeteer入门及使用

    2022-07-05 08:09:16
  • Python3 读、写Excel文件的操作方法

    2023-05-21 15:14:32
  • CSS文件的编码要和页面的编码相一致

    2010-06-06 13:59:00
  • 利用索引提高SQL Server数据处理的效率

    2009-01-08 15:32:00
  • Go方法接收者值接收者与指针接收者详解

    2024-02-17 04:30:45
  • MySQL Group By用法

    2012-01-05 19:05:14
  • Mac下mysql5.7.10安装教程

    2024-01-20 15:50:07
  • asp之家 网络编程 m.aspxhome.com