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 ...
随机推荐
- 多维标度法(MDS)的Python实现
多维标度法(multidimensional scaling,MDS)是一种在低维空间展示“距离”数据结构的多元数据分析技术,是一种将多维空间的研究对象( 样本 或 变量 ) 简化到低维空间进行定位. ...
- EF基础知识小记三(设计器=>数据库)
本文主要介绍通过EF的设计器来同步数据库和对应的实体类.并使用生成的实体上下文,来进行简单的增删查该操作 1.通过EF设计器创建一个简单模型 (1).右键目标项目添加新建项 (2).选择ADO.Net ...
- C# 对象相等性判断和同一性判断
在日常开发中经常需要编写代码比较不同的对象.例如,有时需要将对象都放到一个集合中,并编写代码对集合中的对象进行排序.搜索或者比较. System.Object类有两个Equals方法,如下: 1.实例 ...
- 从一个例子学习 instanceof 和 getclass 的区别
判断两个对象是否为同一类型,时常用到getclass 和 instanceof ,而这两个函数又是时常让人混淆.下面从一个例子说明两者的区别: public class Test_drive { pu ...
- spring 接收前台ajax传来的参数的几个方法
知识补充 JSON.stringify(), 将value(Object,Array,String,Number...)序列化为JSON字符串JSON.parse(), 将JSON数据解析为js原生值 ...
- php如何使用rabbitmq实现发布消息和消费消息(tp框架)(第一篇)
1,默认已经安装好了rabbitmq: 参考 http://www.cnblogs.com/spicy/p/7017603.html 2,安装rabbitmq客户端: 方法1: pecl 扩展安装 ...
- javascript数组原型方法
1.javascript数组原型方法. <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- chroot的用法
chroot命令用来在指定的根目录下运行指令.chroot,即 change root directory (更改 root 目录).在 linux 系统中,系统默认的目录结构都是以/,即是以根 (r ...
- 通过wireshark抓包来讲解HTTP中Connection: keep-alive头部的作用
今天周末时间,有空给大家讲解一个小知识点,即HTTP的keep-alive头部.我使用wireshark来抓取网络包来在实战中讲解.希望能让大家更容易.更直观的理解! HTTP中keep-alive头 ...
- 熟悉一下oncontextmenu事件的知识
定义和使用 只要点击鼠标右键,就触发oncontextmenu事件并打开上下文菜单. 需要注意的是:所有主流浏览器都支持oncontextmenu事件,但其中的contextmenu元素只有FireB ...