mysql毫秒转换日期时间_MySQL日期类型和毫秒值相互转换

有时需要将日期类型值转换成毫秒值,有时也要将毫秒值转换成日期,为了更方便,满足查询的需要。

现在,新建一张数据库表t_stu_info,并向表里插入数据

use test;

show tables;

create table t_stu_info(

id int not null primary key,

stu_name varchar(20),

stu_date timestamp,

stu_age int(3),

stu_sex varchar(5)

);

insert into t_stu_info(id,stu_name,stu_date,stu_age,stu_sex) values ('1','zhangsan','2000-12-12 12:30:00','15','man');

commit;

select * from t_stu_info;

cb7e1cdb4028847c873693ac648f8850.png

1、日期转换成毫秒值

select UNIX_TIMESTAMP(t.stu_date) from t_stu_info t

693174f6e1914752f6c9d449eb358c95.png

2、毫秒值转换成日期

SELECT

FROM_UNIXTIME(UNIX_TIMESTAMP(t.stu_date),

'%Y-%m-%d %h:%i:%s') AS stu_date

FROM

t_stu_info t

aee2f3999bd5ea64787ff86b4a3090f2.png

SELECT

FROM_UNIXTIME(UNIX_TIMESTAMP(t.stu_date),

'%Y-%m-%d') AS stu_date

FROM

t_stu_info t

f595e8ec2bec88dee477dd5e63a55c57.png


版权声明:本文为weixin_33204101原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。