日期时间类型自动转型

-- 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

mysql 日期时间类型 自动转型 及 运算的更多相关文章

  1. mysql日期时间类型总结

    MySQL 日期类型:日期格式.所占存储空间.日期范围 比较.  日期类型        存储空间       日期格式                 日期范围  ------------ ---- ...

  2. Mysql 日期时间类型详解

    MySQL 中有多种数据类型可以用于日期和时间的表示,不同的版本可能有所差异,表3-2 中列出了MySQL 5.0 中所支持的日期和时间类型. 这些数据类型的主要区别如下: * 如果要用来表示年月日 ...

  3. mysql 日期时间类型

    datetime timestamp year date time drop table test;create table test (dt datetime, ts timestamp, y ye ...

  4. MySQL 日期时间类型怎么选?千万不要乱用!

    构建数据库写程序避免不了使用日期和时间,对于数据库来说,有多种日期时间字段可供选择,如 timestamp 和 datetime 以及使用 int 来存储 unix timestamp. 不仅新手,包 ...

  5. Mysql 建表时,日期时间类型选择

    mysql(5.5)所支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: 日期时间类型 占用空间 日期格式 最小值 最大值 零值表示  D ...

  6. MySQL学习分享-->日期时间类型

    日期时间类型 ①如果要用来表示年月日时分秒,一般使用datetime类型: ②如果要用来表示年月日,一般使用date类型: ③如果要表示时分秒,一般使用time类型: ④如果只是表示年份,一般使用ye ...

  7. MySQL之日期时间类型

    mysql(5.5)所支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: 日期时间类型 占用空间 日期格式 最小值 最大值 零值表示  D ...

  8. MySQL 中的日期时间类型

    日期时间类型中包含以下几种数据类型: DATE TIME DATETIME TIMESTAMP YEAR 各类型都有具体的取值范围,超出或非法的其他值时,MySQL 会回退到 0.TIMESTAMP ...

  9. MySQL建表时,日期时间类型选择

    MySQL(5.5)所支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: 日期时间类型 占用空间 日期格式 最小值 最大值 零值表示  D ...

随机推荐

  1. 如何在NAS上安装Git Server

    前段时间一时兴起,买了一个NAS,具体型号是QNAP TS-269L.一方面用作硬盘存储数据,另一方面为了方便就在上面搭了一个Git代码服务器.下面详述一下这个Git Server是如何搭建起来的. ...

  2. 【Java并发编程】:内存可见性

    加锁(synchronized同步)的功能不仅仅局限于互斥行为,同时还存在另外一个重要的方面:内存可见性.我们不仅希望防止某个线程正在使用对象状态而另一个线程在同时修改该状态,而且还希望确保当一个线程 ...

  3. 【Java并发编程】:线程挂起、恢复与终止

    挂起和恢复线程     Thread 的API中包含两个被淘汰的方法,它们用于临时挂起和重启某个线程,这些方法已经被淘汰,因为它们是不安全的,不稳定的.如果在不合适的时候挂起线程(比如,锁定共享资源时 ...

  4. @Override 注解compiler1.5和compiler1.6不同

    说到注解问题,@interface 来定义注解类 这个注解出现是在jdk1.5版本出现. jdk1.5只支持@override对类的继承中方法重写的使用,不支持接口实现中方法重写的使用(这种情况下会报 ...

  5. java学习-struts基础(一)

    struts发展 struts是Apache软件基金会赞助的一个开源项目,是一个基于Java EE的MVC开源实现. 它为Servlet/JSP技术的应用提供技术框架2001.7--Struts1正式 ...

  6. 11-hdfs-NameNode-HA-wtihQJM解决单点故障问题

    在hdfs中, NN只有一个, 但其中保存的数据尤其重要, 所以需要将元数据保存, 其中源数据有2个形式, fsimage 和 edit文件, 最简单的解决方法就是复制fsimage, 并在文件修改时 ...

  7. 【云+社区极客说】新一代大数据技术:构建PB级云端数仓实践

    本文来自腾讯云技术沙龙,本次沙龙主题为构建PB级云端数仓实践 在现代社会中,随着4G和光纤网络的普及.智能终端更清晰的摄像头和更灵敏的传感器.物联网设备入网等等而产生的数据,导致了PB级储存的需求加大 ...

  8. vue里面computed的运用理解

    computed 计算属性,是用来声明式的描述一个值依赖了其它的值,当你在模板里把数据绑定到一个计算属性上时,Vue 会在其依赖的任何值导致该计算属性改变时更新 DOM.这个功能非常强大,它可以让你的 ...

  9. Border属性的各种变化

    本文前部分转自http://www.cnblogs.com/binyong/archive/2009/02/21/1395386.html,但是文章并未解释实现的原理,因此,后面本文也对次进行了解释. ...

  10. IE浏览器TAB清空设置

    1.Regedit 2.HKEY_USERS——搜索NewTabPage 3.清空除了Default之外的所有东西 4.也可以添加NumRows——TAB的行数 可以参考:http://tieba.b ...