Mysql日期时间函数

1.时间日期函数

1)now()    返回服务器当前的时间  格式YYYY-MM-DD HH:MM:SS

2)curdate()    返回当前日期

3)curtime()     返回当前时间

4)year(date)     返回指定时间的年份

5)date(date)     返回指定时间的日期

6)time(date)      返回指定时间的时间

2.日期时间运算

语法:

           select *  from  表名  where 字段名  运算符 (时间-interval  时间间隔单位)

时间间隔单位:

              1 day | 2 hour | 1 minute | 2 year | 3 month

示例:

         查询1天以内的记录
           select * from t1
           where shijian > (now()-interval 1 day);
         查询1年以前的记录
         select * from t1
         where shijian > (now()-interval 1 year);
         查询一天以前三天以内的记录
         select * from t1
         where
         shijian < (now()-interval 1 day) and
         shijian > (now()-interval 3 day);

3.运算符操作

    1.数值比较/字符比较
        1)数值比较:=  !=  >  >=  <  <=
        2)字符比较:=  !=
        
        示例:

        查找攻击力高于150的英雄的名字和攻击力:
        select name,gongji from sanguo where gongji>=150;

        将赵云的攻击力设置为360,防御力设置为68:
        update sanguo set gongji=360,fangyu=68 where name="赵云";

    2.逻辑比较
        1)and (两个或多个条件同时成立)
        2)or  (任意一个条件成立即可)        示例:

        找出攻击值高于200的蜀国英雄的名字、攻击力
        select name,gongji from sanguo where gongji>200 and country="蜀国";

        将吴国英雄中攻击值为110的英雄的攻击值改为100,防御值改为60:
        update sanguo set gongji=100,fangyu=60 where gongji=110 and country="吴国";

        查找蜀国和魏国的英雄信息
        select * from sanguo where country="蜀国" or country="魏国";

    3.范围内比较
        1)between 值1 and 值2
        2)where 字段名 in(值1,值2,...)
        3)where 字段名 not in(值1,值2,...)

         示例:

      1、查找攻击值100-200的蜀国英雄信息
        select * from sanguo
	where gongji between 100 and 200 and
	country="蜀国";

      2、找到蜀国和吴国以外的国家的女英雄信息
        select * from sanguo
	where country not in("蜀国","吴国") 
	and sex="女";

      3、找到id为1、3或5的蜀国英雄 和 貂蝉的信息
        select * from sanguo
	where 
	(id in(1,3,5) and country="蜀国") or name="貂蝉";

    4.匹配空、非空
       1.空:where name is null
       2.非空:where name is not null

     注意:
       null : 空值,只能用 is 或者 is not 去匹配
       “ ”  : 空字符串,用 = 或者 != 去匹配  

      1、姓名为NULL值的蜀国女英雄信息
        select * from sanguo
        where
        name is null and country="蜀国" and sex="女";

      2、姓名为 "" 的英雄信息
        select * from sanguo where name="";

    5.模糊比较:
        1.where 字段名 like 表达式
        2.表达式
            1)_ : 匹配单个字符
            2)% :匹配0到多个字符

示例:

      select name from sanguo where name like "_%_";
      select name from sanguo where name like "%";
        ## NULL不会被统计,只能用is、is not去匹配
      select name from sanguo where name like "___";
      select name from sanguo where name like "赵%";

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值