users用户表   cridet 积分表
 id            chang_item()(+的加分 -的减分) chang_time left_item(剩下的积分)现在要统计每个月月初积分 每个月消耗积分 每个月增加积分 月末积分 该怎么统计这条语句统计2008-1-1的月处积分(只要统计2008-1-1之前消耗的积分就行了) 这样写对不对 
select  u.id,c.left_item  from users u  join cridet c on u.id=c.user_id where c.chang_time<='2008-1-1' group by u.id order by u.id limit 1

解决方案 »

  1.   

    select id,sum(chang_item)
    from cridet
    where chang_time <='2008-1-1'
    group by id这样就行了,好像无需 join[align=center]====  ====
    [/align]
      

  2.   

    2008-1-1的月初积分 还可以,这个速度应该快一点select *
    from cridet
    where chang_time <='2008-1-1'
    group by id
    having max(chang_time)=chang_time[align=center]====  ====
    [/align]
      

  3.   

    2008-1-1的月初积分 还可以,select a.id,a.left_item
    from cridet a inner join (
    select id,max(chang_time) as mchang_time 
    from cridet 
    group by id) as b 
    on a.id=b.id and a.chang_time=b.mchang_time[align=center]====  ====
    [/align]