zoukankan      html  css  js  c++  java
  • MySQL中的运算符和时间运算

    一.MySQL中运算符的分类

    算术运算符,比较运算符,逻辑运算符,按位运算符

    二.算数运算符

          符号                            作用

       +         加法

        -         减法

        *         乘法

        /         除法

          %          取余

       DIV(X,Y)     商

      MOD(X,Y)      余数

    实例:首先创建表:

    create table text1(
       tid int not null  primary key auto_increment,
       tnum1 int not null ,
       tday datetime not null
    );
    
    select * from text1;
    truncate table text1;
    insert into text1 values(1,0,"2017-05-19 08:15:00");
    insert into text1 values(null,1,"2017-05-19 08:15:00");
    insert into text1 values(null,2,"2015-05-19 09:00:00");

    下面来测试:

    int加法:

    update text1 set 
            tnum1 = tnum1+10
    where tid=1;

    二. 针对daytime类型的计算:

     函数1:datediff(时间1,时间2)

    select datediff((select tday from text1 where tid=2),(select tday from text1 where tid=3))  as diff;

     从中可以看出datediff函数是一个计算天数差的函数,他不计算小时,而且他是时间2减去时间1

    函数2:TIMESTAMPDIFF(DAY/HOUR/MINUTE  时间1,时间2);

     第一个参数:代表你要计算的时间类型差  day精确到天数  hour精确到小时,minute精确到分钟

    select timestampdiff(minute,(select tday from text1 where tid=2),(select tday from text1 where tid=3)) as diff;

    函数3:获得当前时间

     datetime类型:sysdate()

     time类型:curtime()

     函数4:时间相加函数  date_add()

     可以直接加上一天,一周,一年

    select date_add((select tday from text1 where tid=1),interval 1 day) as newday;
    select date_add((select tday from text1 where tid=1),interval 1 year) as newday;

     减去一天

    select date_add((select tday from text1 where tid=1),interval -1 day) as newday;

     加上随意的时间点:

    select date_add((select tday from text1 where tid=1),interval "1 2:10:0" day_second) as newday;

     三.比较运算符

          符号             含义

            =     相同

            >               大于

            <               小于

            >=             大于等于

            <=             小于等于

          !=            不等于

           is null        是否为空

           is not null    是否不为空

          between   and   是否在两个值之间

          in(.....)                在in的范围内部符合

          not  in              不是这个范围中的任何一个

          like/not  like             模糊查询

          regexp              正则表达式

    四.逻辑运算符

         符号                   作用

         &&或and               与

         | |或 or                   或

        !或not                  非

         xor                        异或

  • 相关阅读:
    zabbix_QQ邮件告警部署(待续。。。。。。)
    FTP服务的搭建
    项目: NFS服务的搭建
    用ngxin搭建简单的下载站点
    脚本设定开机自启动
    自建小项目:自动化脚本安装ngnix项目
    Zabbix Web操作深入(待续------)
    DHCP原理与配置
    PXE高效能网络批量装机
    DNS域名解析服务
  • 原文地址:https://www.cnblogs.com/SAM-CJM/p/9650379.html
Copyright © 2011-2022 走看看