简体   繁体   English

如何在MySQL中按当前月份的当前年份计算总数

[英]How to count total by current year of current month in mysql

I have bellow snippet table format. 我有下面的摘要表格式。

Table name:- wp_lead_count_freevendor 表名称: wp_lead_count_freevendor

id entryid start_date end_date   user_id count_set entry_date cancel_set_date
70 11392   2015-12-03 2015-12-03 3185            1 2015-12-03 2015-12-04
71 11393   2015-12-03 2015-12-03 3185            1 2015-12-03 2015-12-04
72 11394   2014-10-01 2014-10-01 3185            1 2014-10-01 2014-10-01

Here i want to calculate count total count_set column. 在这里,我想计算总数count_set列。 of current month & year by start_date column WHERE user_id =3185. start_dateWHERE user_id = 3185 start_date的当前月份和年份。

suppose in start_date current year is 2015 & current month is 12:- 假设在start_date当前年份为2015年,当前月份为12:

year   month   count total 
2015    12     2

For user id 3185 of this month of year count_set total =2 对于一年中该月的用户ID 3185, count_set total = 2

so any body will tell how do i fire the query to get count_set total for current year of current month for user_id =3185. 因此,任何机构都会告诉我如何触发查询以获取user_id = 3185的当前月份的当前年份的count_set总数。

I have tried bellow query but its not working. 我已经尝试过波纹管查询,但是它不起作用。

    $check_per_month=mysql_query("SELECT DATE_FORMAT(end_date, '%Y') as 'year',
    DATE_FORMAT(end_date, '%m') as 'month',
    COUNT(id) as 'total'
    FROM wp_lead_count_freevendor WHERE user_id=$wp_lead_count_user_id
    GROUP BY DATE_FORMAT(end_date, '%Y%m')") OR DIE(mysql_error());
    while($row = mysql_fetch_assoc($check_per_month))
    {

    echo $sql_chk_current_month_count=$row['total'];

    }

尝试使用where,year和month方法进行计数,如下所示:

SELECT .... WHERE YEAR(start_date)=2015 AND MONTH(start_date)=12

Try this query: 试试这个查询:

  SELECT YEAR(NOW()) as `year`,MONTH(NOW()) as `month', SUM(count_set) as `count_set`
  From  wp_lead_count_freevendor
     WHERE YEAR(start_date)=YEAR(NOW()) AND MONTH(start_date)=MONTH(NOW()) AND user_id=3185
        group by YEAR(NOW()),MONTH(NOW())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM