zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    HDC,CDC,CWindowDC,CClientDC,CPaintDC基础 笑风生 博客频道 CSDN.NET
    用web用户控件的方式添加到webpart,使用ajax实现无刷新总结
    .net 中利用owc 画制图表
    未安装在此服务器场中,无法添加到该范围
    敏捷软件开发宣言及原则
    调试 Windows SharePoint Services Workflow
    系统提示"System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本"解决办法
    安装及删除网站模板
    无法安装功能“265e193c6477431e8d3c98f51e6d3247”,因为加载事件接收器程序集“Microsoft.Office.Workflow.Feature, Version=12.0.0.0, Culture=neutral, PublicKeyToken=a5e1d8429183b844”
    表单加载出错
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/9307622.html
Copyright © 2011-2022 走看看