SQL:如何在建表时创建一个13位的时间戳字段

本文介绍了如何在MySQL中创建一个13位时间戳字段,建议使用BIGINT类型,结合current_timestamp()获取当前时间,通过unix_timestamp()转换为整数,再利用replace()函数处理精确到毫秒的时间戳。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

知识预备:

- 1、 INT - 正常大小的整数,可以带符号。如果是有符号的,它允许的范围是从-2147483648到2147483647。如果是无符号,允许的范围是从0到4294967295;
- 2、BIGINT - 一个大的整数,可以带符号。如果有符号,允许范围为-9223372036854775808到9223372036854775807。如果无符号,允许的范围是从0到18446744073709551615。
则13位时间戳应选用bigint
- 3、current_timestamp()时间戳函数,可获取到当前时间
select current_timestamp(3)
--  2020-12-04 18:58:45.859in
- 4、1秒=1,000毫秒 上面获取到的时间其实精确到了毫秒
- 5、unix_timestamp(),把获取到的时间按戳转换为一串数字
select unix_timestamp(current_timestamp(3))
 -- 1607079535.702
- 6、replace( 被替换的字段, 其中被替换的部分,被替换成什么) 字符串替换函数
select REPLACE(unix_timestamp(current_timestamp(3)),'.',''); 
-- 1607079669549

进入正题,如何创建如下表格?time_1字段为13位整数的时间戳

1秒=1000毫秒;

解答:

-- 先建个表
create table test(
age int(4),
time_1 bigint(13)
)

要把时间戳转换为13位的整数,再插入表中

-- 每一行后面没加“‘;”,运行的时候一行一行运行
insert into test (age,time_1)values (1,REPLACE(unix_timestamp(current_timestamp(3)),'.','')) 
insert into test (age,time_1)values (2,REPLACE(unix_timestamp(current_timestamp(3)),'.','')) 
insert into test (age,time_1)values (3,REPLACE(unix_timestamp(current_timestamp(3)),'.','')) 
insert into test (age,time_1)values (4,REPLACE(unix_timestamp(current_timestamp(3)),'.','')) 
insert into test (age,time_1)values (4,REPLACE(unix_timestamp(current_timestamp(3)),'.','')) 

okkk了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值