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 ...
随机推荐
- 微信小程序开发笔记2,底部导航栏tablebar
底部导航(要在app.js里面配置,也就是把导航的代码写到app.js) 官方文档说最少2个导航最多5个 , "tabBar": { "color": &quo ...
- 3、Xamarin Forms 调整安卓TabbedPage 下置
降低学习成本是每个.NET传教士义务与责任. 建立生态,保护生态,见者有份. 教程晦涩难懂是我的错误. 对于默认的TabbedPage 上面进行页面切换 上面是安卓默认的情况 对我们大部分人来说都 ...
- 使用webpack打包js文件(隔行变色案例)
使用webpack打包js文件(隔行变色案例) 1.webpack安装的两种方式 运行npm i webpack -g全局安装webpack,这样就能在全局使用webpack的命令 在项目根目录中运行 ...
- spring boot整合RabbitMQ(Direct模式)
springboot集成RabbitMQ非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-starter-amqp项目对消息各种支持. Direct Excha ...
- 移动端模拟hover
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <meta name=& ...
- Hadoop网站日志数据清洗——正则表达式实现
周旭龙前辈的Hadoop学习笔记—网站日志分析项目案例简明.经典,业已成为高校大数据相关专业的实验项目.上周博主也完成了这个实验,不同于周前辈使用特殊符号切割字符串得到数据的做法,博主使用了正则表达式 ...
- .Net4.5新特性:正则表达式超时介绍
“Regex” 在数据验证方面最受欢迎.考虑到您可能对“Regex”完全陌生的.请参考我介绍Regex如何运作的视频. But because of the typical parsing logic ...
- Ionic项目中如何使用Native Camera
本文介绍如何在ionic项目中使用设备的camera. Ionic版本:v3.2.0 / 2017-05-10 / MIT Licensed / Release Notes ============= ...
- sql中替换字符串
select REPLACE(CONVERT(varchar ,CreateDate,23),'-','年') CreateDate from SG_Client 2018年06年11
- attempt to write a readonly database错误的解决(C#,SQLite)
今天打包WPF程序,安装后总是打不开,查看监控日志原来是SQLite的问题,报错如图 当向SQLite数据库中存入新纪录时总是显示attempt to write a readonly a datab ...