现有表结构如下:  id(自增)    mobile    content    addtime(默认getdate())
能否用1条SQL查出  根据某个手机号(mobile字段),当天的的记录和当月的记录数,记住是使用1条SQL语句啊,因为我是在程序里调用,不想打开两次结果集。

解决方案 »

  1.   

    select sum(case when datediff(day,addtime,getdate())=0 then 1 else 0 end)[当天的的记录],
           sum(case when datediff(month,addtime,getdate())=0 then 1 else 0 end)[当yue的的记录] 
    from tb 
    where mobile='某个手机号'
      

  2.   

    select mobile,当日数=sum(case when convert(char(10),addtime,102)=convert(char(10),getdate(),102) then 1 else 0 end ), 当月数sum(case when convert(char(7),addtime,102)=convert(char(7),getdate(),102) then 1 else 0 end ) where mobile='13822234567' group by mobile 
      

  3.   


    --try
    select 当天的记录数=case when datediff(dd,addtime,getdate())=0 then count(*) end,
           当月的记录数=case when datediff(month,addtime,getdate())=0 then count(*) end
     from 表 where mobile='手机号'