2015-12-03 52 views
1

我有bellow snippet表格格式。如何在mysql当前月的当前年份中统计总数

表名称: - 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 

在这里我要计算总计数count_set列。本月&年start_dateWHEREuser_id = 3185。

start_date当年假设是2015年&当前月份是12: -

year month count total 
2015 12  2 

这个月一年的用户ID 3185 count_set总数= 2

所以任何的身体会告诉我如何发起查询以得到count_set当前当月的总计user_id = 3185。

我已经试过了波纹管查询,但它不工作。

$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']; 

    } 

回答

1

尝试计数使用像这样的地方,年份和月份方法:

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

我已创建查询,但有一些语法错误,你可以引导我使它可行查询: - $ query = mysql_query(“SELECT sum(count_set)AS'meta_sum'FROM wp_lead_count WHERE user_id = $ wp_lead_count_user_id AND DATE(end_date)='$ wp_lead_count_end_date'”)或DIE(mysql_error()); –

+0

'$ wp_lead_count_end_date'的外观如何? – Tarek

0

尝试此查询:

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()) 
相关问题