select *,
(select count(*) from tb where A.name=name and date between date_add(A.date,interval -3 day) and A.date) as count 3,
(select count(*) from tb where A.name=name and date between date_add(A.date,interval -7 day) and A.date) as count7
from tb A

解决方案 »

  1.   

    select a.*, b.c3,c.c7
    from tb_records a,
    (select name,count(*) as c3 from tb_records where `date`>=curdate-interval 3 day group by name ) b,
    (select name,count(*) as c7 from tb_records where `date`>=curdate-interval 7 day group by name ) c,
    where a.name=b.name and a.name=c.name
      

  2.   


    只能在from后面使用子查询
      

  3.   


    大哥,不行,执行不了,貌似是这个思路,我试着改了一下,改不出来还请你出手啊。下面是我之前子查询的,但是需要做成from后面的子查询才行。
    select a.*,(select count(1) from tb_records b 
            where b.num = a.num and b.date <= a.date 
            and datediff(str_to_date(a.date,'%Y-%m-%d %H:%i:%s.%f'),str_to_date(b.date,'%Y-%m-%d %H:%i:%s.%f')) between 0 and 3 ) as count3,
    (select count(1) from tb_records b 
            where b.num = a.num and b.date <= a.date
             and datediff(str_to_date(a.date,'%Y-%m-%d %H:%i:%s.%f'),str_to_date(b.date,'%Y-%m-%d %H:%i:%s.%f')) between 0 and 7 ) as count7
     from tb_records a ;