2011-08-08 88 views
2

我需要行計數的一點幫助。我設法添加今天和總成員數(行)。我想在本週和本月計算。任何人都可以指出我該怎麼做?謝謝。行計數幫助

$result = mysql_query("SELECT * FROM members"); 
$num_rows = mysql_num_rows($result); 

echo "$num_rows Members\n"; 

$utoday = date("j. n. Y"); 

$today = mysql_query("SELECT * FROM mambers WHERE date='$utoday' "); 
$num_today = mysql_num_rows($today); 

echo "$num_today Members\n"; 
+0

你用什麼數據類型來存儲日期? – Arjan

+0

在數據庫中「varchar」 – maxlk

+0

您需要將其更改爲DATE或DATETIME。它們是爲日期和日期處理而設計的,varchar不是。你可以找到具有特定日期的記錄,但是沒有辦法,你可以對日期範圍做任何事情。你想使用範圍。 – Arjan

回答

1

如果要算上本週從最近的星期一開始:如果你要計算這個月從第1個查詢開始

SELECT COUNT(1) WeekCount 
FROM members A, 
(
    SELECT 
     (MondayDate + INTERVAL 0 SECOND) PastMonday, 
     ((MondayDate + INTERVAL 7 DAY) + INTERVAL 0 SECOND) NextMonday 
    FROM 
     (SELECT DATE(NOW() - INTERVAL WEEKDAY(NOW()) DAY) MondayDate) AA 
) B 
WHERE date >= PastMonday AND date < NextMonday 
; 

這樣的:

SELECT COUNT(1) MonthCount 
FROM members A, 
(
    SELECT FirstOfThisMonth, 
    ((FirstOfThisMonth + INTERVAL 32 DAY) - INTERVAL (DAY(FirstOfThisMonth + INTERVAL 32 DAY)-1) DAY) FirstOfNextMonth 
    FROM 
    (
     SELECT (DATE(NOW() - INTERVAL (DAY(NOW())-1) DAY) + INTERVAL 0 SECOND) FirstOfThisMonth 
    ) AA 
) B 
WHERE date >= FirstOfThisMonth AND date < FirstOfNextMonth 
; 

給它一試!