帮助计算行数

时间:2011-08-08 16:59:43

标签: php mysql

我需要一些关于行数的帮助。我设法今天添加和总成员数(行)。我想算上这周和本月。任何人都可以指出我该怎么做?感谢。

$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";

2 个答案:

答案 0 :(得分:4)

如果您将日期存储为类型日期,则可以使用mysql built-in time functions

例如,您可以按月(日期)分组。

答案 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
;

试一试!!!