MySql按日期进行统计

1 数据库字段pk_time(Varchar)

当天的数据

     SELECT * FROM 表 WHERE date(fk_time) = curdate();

 当月的数据

     SELECT *FROM 表 WHERE DATE_FORMAT(fk_time,'%Y%m')=DATE_FORMAT(CURDATE( ),'%Y%m')

昨天

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1

7天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)

近30天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

本月

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )

上一月

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1

查询本季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());

查询上季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

查询本年数据

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());

查询上年数据

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

查询当前这周的数据 

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());

查询上周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

查询当前月份的数据

select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')

查询距离当前现在6个月的数据

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

2 时间段数据

id 为方法名,parameterType参数用map保存,resultType为返回对象  参数tj_start tj_end 提交开始和结束时间

复制代码
    <select id="方法名" parameterType="参数类型" resultType="返回类型">
       select *from jw_order where 1=1   
<if test="tj_start!= null and tj_start!=''"> AND submittime&gt;=#{tj_start} </if> <if test="tj_end!=null and tj_end!=''"> AND submittime&lt;=#{tj_end} </if>
</select>
复制代码

3 模糊查询sql(Mybatis)

 <select id="方法" parameterType="参数类型" resultType="返回类型">
select *from 表名 where 1=1   
AND 字段 LIKE CONCAT(CONCAT('%', #{参数}), '%')
</select>

 

MySql按日期进行统计(前一天、本周、某一天)

统计当天

sql语句为:
* user where date(log_time) = curdate();
curdate()默示当天日期
统计前一天
若是默示前一天的数据,则不克不及应用curdate()-1,因为当日期为月初时,curdate()-1 日期就不是上一个月的月末日期。
例如:今天是6月1日,理论上curdate()-1为5月31日,然则curdate()-1获得不是5月31日,而是6月0日。那么统计前一天的日期就不克不及应用curdate()-1了,mysql数据库又有一个新办法统计前一天的数据。
统计前一天的日记sql语句:
* bean where date(log_time) = date_sub(curdate(),interval 1 day);
括号中为当天时候的前一天,若是统计前几天就将括号中的’1’改成响应的天数。若是要算月或年,直接将day改为month或year即可
统计本周
请求: 统计从昨天开端统计前7天的日记包含昨天
* user where date(log_time) >= date_sub(curdate(),interval 7 day)
and date(log_time) <= date_sub(curdate(),interval 1 day)
在网上找的应用week统计一周信息,只能统计到5天的信息,不合适请求,所以改用这种办法。
统计某一天
统计汗青某一天的日记,将date_sub(curdate(),interval 0 day)函数中的curdate()调换为某一天的日期
比如:要统计2012-05-25日期的信息
date_sub(""2012-05-25"",interval 0 day)
关于date_sub()函数的例子:
今天是2013年5月20日。
date_sub(""2012-05-25"",interval 1 day) 默示 2012-05-24
date_sub(""2012-05-25"",interval 0 day) 默示 2012-05-25
date_sub(""2012-05-25"",interval -1 day) 默示 2012-05-26
date_sub(""2012-05-31"",interval -1 day) 默示 2012-06-01
date_sub(curdate(),interval 1 day) 默示 2013-05-19
date_sub(curdate(),interval -1 day) 默示 2013-05-21
date_sub(curdate(),interval 1 month) 默示 2013-04-20
date_sub(curdate(),interval -1 month) 默示 2013-06-20
date_sub(curdate(),interval 1 year) 默示 2012-05-20

date_sub(curdate(),interval -1 year) 默示 2014-05-20

 

如:

<!-- 分页查询订单个数 -->

   <select id="queryOrderCountByCondition" resultClass="order">

                select

count(*) as id

                from

                       t_order_info<dynamic prepend="where">

<isNotNull prepend="and" property="companyId">

company_id = #companyId#

</isNotNull>

 

<isNotNull prepend="and" property="inMonth">

date_sub(CURDATE(), INTERVAL 1 MONTH) &lt;= date(add_time)

</isNotNull>

 

<isNotNull prepend="and" property="productName">

   id in (select id from t_order_info  where id in(select order_id from t_order_product  where product_id in(SELECT t_product.id FROM t_product where t_product.name=#productName#)))

</isNotNull>

 

<isNotNull prepend="and" property="orderDateBegin">

<![CDATA[ add_time >= #orderDateBegin#]]>

</isNotNull>

<isNotNull prepend="and" property="orderDateEnd">

<![CDATA[ add_time <= #orderDateEnd#]]>

</isNotNull>

</dynamic>

   </select>

posted @ 2017-12-25 16:45  总被人想的静静  阅读(3190)  评论(0编辑  收藏  举报