mysql日期类型运算_mysql 日期时间类型 自动转型 及 运算

日期时间类型自动转型

--now()、字符串、数字转datetime类型

create table t(dt datetime);

insert into t values(now());

insert into t values('2007-9-3 12:10:10');

insert into t values('2007/9/3 12+10+10');

insert into t values('2007#9#3 12+10+10');

insert into t values('2007+9+3 12+10+10');

insert into t values('20070903121010');

insert into t values(20080903121010);

--now()、字符串、数字转date类型

create table test(dt date);

insert into test values(now());

insert into test values('2007-9-3 12:10:10');

insert into test values('2007/9/3 12+10+10');

insert into test values('2007#9#3 12+10+10');

insert into test values('2007+9+3 12+10+10');

insert into test values('20070903121010');

insert into test values(20080903121010);

--now()、字符串、数字转time类型

create table t3(dt time);

insert into t3 values(now());

insert into t3 values('2007-9-3 12:10:10');

insert into t3 values('2007/9/3 12+10+10');

insert into t3 values('2007#9#3 12+10+10');

insert into t3 values('2007+9+3 12+10+10');

insert into t3 values('20070903121010');

insert into t3 values(20080903121010);

--now()、字符串、数字转timestamp类型

create table t4(dt timestamp);

insert into t4 values(now());

insert into t4 values('2007-9-3 12:10:10');

insert into t4 values('2007/9/3 12+10+10');

insert into t4 values('2007#9#3 12+10+10');

insert into t4 values('2007+9+3 12+10+10');

insert into t4 values('20070903121010');

insert into t4 values(20080903121010);

--在任何时间,now()、任意分隔符的年月日时分秒字符串、年月日时分秒数字串都可以拿来当日期类型、时间戳使用。但月日时分秒要保持2位数。

--日期类型可以转时间戳,时间戳不能转时期类型

drop table test;

create table test(dt date);

insert into test values(unix_timestamp()); --  报错

drop table test;

create table test(dt timestamp);

insert into test values(curdate());

--加减运算

--now(),sysdate(),current_timestamp(),curdate(),curtime() 函数和日期时间类字段都可以加特定格式的数字

select now(),now()+1,now()-1,now()+101,now()+050000;

--加1秒,减1秒,加1分1秒,加5小时,不会报错,但最后一个结果有问题:20180714265220,26点是错的

--2018-07-14 21:52:20, 20180714215221, 20180714215219, 20180714215321, 20180714265220

select sysdate()+1, current_timestamp()+1, curtime()+1, curdate()+1; -- 最后这个是加一天

-- 20180714215353,20180714215353,215353,20180715

drop table test;

create table test(id datetime);

insert into test values(now()+010000);

insert into test values(now()+050000);  -- 报错,因为现在是21:48,加上5小时就成26点了就不对了,他只会自顾自地往上加

drop table test;

create table test(id timestamp);

insert into test values(now());

select id, id+1, id+101, id+010000 from test;

drop table test;

create table test(id date);

insert into test values(now());

select id, id+1, id+101, id+010000 from test;

--比较运算

-- 日期类型

-- datetime类型

drop table test;

create table test(id datetime);

insert into test values(now());

select * from test where id > '1999-1-1'; //结果:2018-07-13 03:38:35

select * from test where id > '1999=1=1'; //结果:2018-07-13 03:38:35

select * from test where id > '1999'; //结果:2018-07-13 03:38:35

select * from test where id > 1999; //结果:2018-07-13 03:38:35

select * from test where id < now(); //结果:2018-07-13 03:38:35

-- year 类型

drop table test;

create table test(id year);

insert into test values(now());

select * from test where id > '1999-1-1'; //结果:2018

select * from test where id > '1999=1=1'; //结果:2018

select * from test where id > '1999'; //结果:2018

select * from test where id > 1999; //结果:2018

select * from test where id > '1999=1=1'; //结果:2018

select * from test where id < now();

-- timestamp 类型

drop table test;

create table test(id timestamp);

insert into test values(now());

select * from test where id > '1999-1-1'; //结果:2018-07-13 03:44:44

select * from test where id < now(); //结果:2018-07-13 03:44:44

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值