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
mysql 日期时间类型 自动转型 及 运算的更多相关文章
- mysql日期时间类型总结
MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型 存储空间 日期格式 日期范围 ------------ ---- ...
- Mysql 日期时间类型详解
MySQL 中有多种数据类型可以用于日期和时间的表示,不同的版本可能有所差异,表3-2 中列出了MySQL 5.0 中所支持的日期和时间类型. 这些数据类型的主要区别如下: * 如果要用来表示年月日 ...
- mysql 日期时间类型
datetime timestamp year date time drop table test;create table test (dt datetime, ts timestamp, y ye ...
- MySQL 日期时间类型怎么选?千万不要乱用!
构建数据库写程序避免不了使用日期和时间,对于数据库来说,有多种日期时间字段可供选择,如 timestamp 和 datetime 以及使用 int 来存储 unix timestamp. 不仅新手,包 ...
- Mysql 建表时,日期时间类型选择
mysql(5.5)所支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: 日期时间类型 占用空间 日期格式 最小值 最大值 零值表示 D ...
- MySQL学习分享-->日期时间类型
日期时间类型 ①如果要用来表示年月日时分秒,一般使用datetime类型: ②如果要用来表示年月日,一般使用date类型: ③如果要表示时分秒,一般使用time类型: ④如果只是表示年份,一般使用ye ...
- MySQL之日期时间类型
mysql(5.5)所支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: 日期时间类型 占用空间 日期格式 最小值 最大值 零值表示 D ...
- MySQL 中的日期时间类型
日期时间类型中包含以下几种数据类型: DATE TIME DATETIME TIMESTAMP YEAR 各类型都有具体的取值范围,超出或非法的其他值时,MySQL 会回退到 0.TIMESTAMP ...
- MySQL建表时,日期时间类型选择
MySQL(5.5)所支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: 日期时间类型 占用空间 日期格式 最小值 最大值 零值表示 D ...
随机推荐
- jQuery过滤选择器:first和:first-child的区别,CSS伪类:first-child
最近项目中遇到需求:只在第一列不能删除,不显示小叉号:点击可添加一列,后面的列右上角显示小叉号,可以点击删除. 我是使用以下方法解决这个小需求 :CSS伪类选择器:first-child设置所有小叉号 ...
- leetcode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
目录 题目描述: 示例: 解法: 题目描述: 给定一个二叉树,原地将它展开为链表. 示例: 给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ ...
- vim实践学习
http://coolshell.cn/articles/5426.html http://www.lagou.com/jobs/138351.html awk:http://coolshell.cn ...
- 安卓的SlidingMenu配置
最近用到了界面的优化,使用SlidingMenu开源库比较方便,为了方便学习,我整理了一下配置过程. 1.准备资料. 首先下载这两个ActionBarSherlock和SlidingMenu,如图:
- C语言初学
输出控制符 输出控制符 含义 %d int 整型数 %ld long int %c char 一个字符 %f float 浮点数,整数/整数=整数,整数/小数=小数 %lf double %x %X ...
- Chapter 3 Phenomenon——17
Dr. Cullen raised his eyebrows. "Do you want to stay?" Cullen医生抬起了他的眉毛“你想待在这吗?” "No, ...
- Python 日期时间处理模块学习笔记
来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...
- ARM的体系结构与编程系列博客——ARM的历史与应用范围
前言 最近我感觉自己比较浮躁,重来没有好好地沉下心来做一件事情,而且针对自己在专业水平上仍然还有很多欠缺,于是我想我应该为自己做些什么来证明一下自己真的是潜心研究东西的人,于是我萌生了一个想法,真正地 ...
- Spring-----AOP深度理解
AOP定义了一些新的概念,要想深入的理解AOP的原理,就必须掌握这些概念的具体含义,本人菜鸡一枚,一下是自己对一些概念的理解,如果哪里不对,欢迎评论区指正 AOP核心概念AOP即Aspect-Orie ...
- 关于webapi加入Route引用出现问题的解决方案
首先在程序包管理器控制台运行安装MVC5.0,因为[Route("/api/..")]只会存在于MVC5.0中间,运行 Install-Package Microsoft.Asp ...