mysql中使用sql命令将时间戳解析成datetime类型存入
作者:梦若辰宇 发布时间:2024-01-22 00:00:25
标签: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


猜你喜欢
- Python练习内容:SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。Pyth
- MySQL DATE_FORMAT函数简介要将日期值格式化为特定格式,请使用DATE_FORMAT函数。 DATE_FORMAT函数的语法如
- (一)单一独立的参数如果命令行输入的参数都是各自单一独立的,直接用个循环把所有参数逐一读出来就行了。sys模块里面直接用args = sys
- 具体代码如下所示:import osfrom PIL import ImageUNIT_SIZE = 220 # the size of i
- 1.准备代码# coding=utf-8class TestDebug: def __init__(self):
- Numpy提供了几种数据保存的方法。以3*4数组a为例:1. a.tofile("filename.bin")这种方法只
- 这篇文章主要介绍了python通过递归获取目录下指定文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- Python 中有 while 和 for 两种循环机制,其中 while 循环
- 导语:Python如何下载网页上的图片呢?今天小编给大家分享另一个Python应用小程序,就是:用Python控制摄像头录制视频!学会了也可
- 业务场景由于项目需求,需要对相关类目进行多选,类目数据量又特别大,业务逻辑是使用懒加载方式加载各级类目数据,编辑时回显用户选择的类目。问题描
- 需求:1.用户输入密码正确登录2.用户输入密码错误退出并调用函数继续输入3.用户输入密码符合原先给定的一个值时,允许用户重置密码,并且可以用
- 这篇文章主要介绍了Python代码块及缓存机制原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- web开发中避免不了运行环境的搭建,个人认为这是没有什么技术含量而又浪费时间的工作.所以将环境搭建的步骤记录下来,希望可以帮到有需要的朋友少
- 如何调用多个不同的ip接口灵感来源:项目的登录登出权限是调A的ip下面的接口,其他的功能调的接口是B的ip下面的接口思路:其实就是多写几个r
- PyTorch上的常用数据类型如下Data typedtypeCPU tensorGPU tensorSize/bytes32-bit fl
- 看一个网站其实就好比品评一个美女。一看长相,我们很多时候关注的是视觉,比如老板经常会说,你做几个页面让我看看!二看身材,也有很多关注标准和s
- 从容器、可迭代对象谈起所有的容器都是可迭代的(iterable),迭代器提供了一个next方法。iter()返回一个迭代器,通过next()
- 今天给大家介绍一个电商中常见的场景 —— MySQL 数据同步 Elasticsearch。商品检索
- hello,大家好,我是Dream。最近有小伙伴私信我,说让我出一篇海龟画图,这其实我也不太能弄得明白,那在这里我和大家一块梳理一下!记得给
- 一、问题目前为止,M1系统上还不能使用pip3安装pandas库,无法使用pandas进行数据分析和处理。虽然网上也有专门适配M1的pyth