a表
aa,    bb13     2010-8-1
12     2010-8-2
11    2010-8-3
10     2010-8-4如果我输入的日期是 2010-8-4
那么得到的结果就是13+12+11+1如果我输入的日期是 2010-8-3
那么得到的结果就是13+12+11规律是,输入一个日期,然后从本月的1号开始到输入的日期,aa字段相加

解决方案 »

  1.   


    select sum(aa) from a where bb>='2010-8-1' and  bb<='2010-8-4'
      

  2.   


    mysql> create table a(aa int,bb date);
    Query OK, 0 rows affected (0.23 sec)mysql> insert into a values(13,'2010-08-01'),(12,'2010-08-02'),(11,'2010-08-03'),(10,'2010-08-04')
        -> ;
    Query OK, 4 rows affected (0.05 sec)
    Records: 4  Duplicates: 0  Warnings: 0mysql> select * from a;
    +------+------------+
    | aa   | bb         |
    +------+------------+
    |   13 | 2010-08-01 |
    |   12 | 2010-08-02 |
    |   11 | 2010-08-03 |
    |   10 | 2010-08-04 |
    +------+------------+
    4 rows in set (0.00 sec)mysql> select sum(aa) from a where year(bb)=year('2010-08-04') and month(bb)=month('2010-08-04') and day(bb)<=day('2010-08-04');
    +---------+
    | sum(aa) |
    +---------+
    |      46 |
    +---------+
    1 row in set (0.02 sec)mysql> select sum(aa) from a where year(bb)=year('2010-08-03') and month(bb)=month('2010-08-03') and day(bb)<=day('2010-08-03');
    +---------+
    | sum(aa) |
    +---------+
    |      36 |
    +---------+
    1 row in set (0.01 sec)
      

  3.   

    aa, bb13 2010-8-1
    12 2010-8-2
    11 2010-8-3
    10 2010-8-4===>select sum(aa)
    from A
    where bb between concat(date_format(输入的日期,'%Y-%m'),'-01') and 输入日期
      

  4.   

    select sum(aa)
    from a表
    where bb between @输入一个日期-interval DAYOFMONTH(@输入一个日期)-1 day and @输入一个日期