2015-12-03 100 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()) 
相關問題