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                        异或

  • 相关阅读:
    Python开发【第六篇】循环语句
    Python开发【第四篇】语句与函数
    Python开发【第三篇】数据类型
    Python开发【第二篇】:初始Python
    2019-10-11入博客第一篇文章
    vim学习2-文档编辑
    vim学习1-入门指令
    linux学习9-进程管理知识
    linux学习8-正则表达式基础
    linux学习7-数据流重定向
  • 原文地址:https://www.cnblogs.com/SAM-CJM/p/9650379.html
Copyright © 2011-2022 走看看