MySQL----示例知识点整理

示例语句:

 select count(0),hour(c.created_at) from `behavior_client_view` c join `behavior_share` s
 on c.share_uuid=s.uuid
 where s.agent_uuid=(select uuid from user where mobile='12606666333' and deleted=0)
 and DATE_FORMAT(c.created_at,'%Y-%m') >DATE_FORMAT(date_sub(curdate(), interval 12 month),'%Y-%m')
 group by hour(c.created_at) order by hour(c.created_at);#近12个月时段统计分布

 

涉及到的MySQL知识点汇总:

1、date_format(date,format):函数根据format字符串格式化date值。

示例:select DATE_FORMAT(now(),'%Y-%m');

   查询结果:2019-01

2、date_sub(date,interval exp type):函数从日期减去指定的时间间隔。

  说明:date:参数是合法的日期表达式

     expr;参数是您希望添加的时间间隔

     type:参数值为:microsecond、second、minute、hour、day\week\month\quarter\year\seconde_microsecond\minute_microsecond\hour_microsecond\hour_second\hour_minute\day_microsecond\day_second\day_minute\day_hour\year_month

示例:

  select date_sub(now(),interval 2 month);  #查询:根据当前的时间减去2个月,计算得出的时间值

  查询结果:2018-11-07 11:37:48

3、curdate():函数返回当前的日期

示例:select curdate();

  查询结果:2019-01-07

 

扩展常用函数:

4、curtime():返回当前的时间

  查询结果:11:37:48

最后:简单示例一条,select if(curdate()="2019-01-07",(select DATE_FORMAT(now(),'%Y-%m-%d')),1) as d;

 

5、/*获取当前系统时间:年月日 时分*/

  select DATE_FORMAT(now(),'%Y-%m-%d %h:%m');  

  查询结果:2019-01-22 02:01

 

posted @ 2019-01-07 12:29  Syw_文  阅读(147)  评论(0编辑  收藏  举报