现在需要一条sql 查询当天前一天0点到24点期间 a字段的数量

解决方案 »

  1.   

    select * from table where datediff()=-1datediff函数忘记了,有这个功能,网上可查
      

  2.   

    select a 
    from table
    where convert(char(10),日期列,111) = '2009/09/25'
      

  3.   

    select count(a) from tablename where trunc(datecolumn) = trunc(sysdate) - 1;
    oracle的
      

  4.   

    不好意思啊 忘了告诉大家我是mysql数据库了
      

  5.   

    select count(a) from tablename where trunc(datecolumn) = trunc(sysdate) - 1; 这句话比较好
      

  6.   

    SELECT * FROM table WHERE DATE(date_column) = '2009-09-25'
      

  7.   

    TSQLselect count(*) aCount from [table] 
    where [columnName] = 'a'
    and [表中记录时间的那个字段] >= convert(varchar(10),dateadd(day,-1,getdate()) ,120)+' 00:00:00'
    and [表中记录时间的那个字段] <= convert(varchar(10),dateadd(day,-1,getdate()) ,120)+' 23:59:59'
      

  8.   

    我这么写的 报错
    SELECT count(*) allpages  FROM lybbs_posttopic where postattime between CONVERT(datetime,CONVERT(varchar,GetDate()-1,120)+' '+'00:00:00')  and CONVERT(datetime,CONVERT(varchar,GetDate()-1,120)+' '+'23:59:59')  ;
    mysql中 postattime 记录时间的字段
      

  9.   

    convert是转化函数,111标识格式化之后是'2009/09/25'这种形式
    120格式化之后是'2009-09-25'哪位仁兄中的日期列是你表里面时间的字段,'2009/09/25'你换成当成日期的前一天,再转换一下,用dateadd,看我的回复
      

  10.   

    select count(*) from 表名 where [表中记录时间的那个字段]=day(getdate())-1你试试这句
      

  11.   

    我的是sqlserver的写法,我看了没问题,mysql现在这里没装你的写法,我看了,你convert的外层怎么又一个convert呢?最外层的第一个参数我看你的是个时间字段,我记得第一个参数应该是字符串吧你去网上查下convert在mysql中的用法吧?大体思想是没错的
      

  12.   

    上面的错了select count(*) from 表名 where day(表中记录时间的那个字段)=day(getdate())-1
      

  13.   

    longds7的方法比较好,楼主试试这个吧,我查了下mysql去当前日期是curdate()所以改下:
    select count(*) from 表名 where day(表中记录时间的那个字段)=day(curdate())-1这个你试试吧
      

  14.   

    不好意思,错了,day取的只是天,那只要是天一样就行了,上个月的也行了,此方法不行的
      

  15.   

    String str1=new Integer(postattime.substring(0,3)).tostring();
    String str2=new Integer(postattime.substring(5,6)).tostring();
    String str3=new Integer(postattime.substring(8,9)).tostring();select count(*) 列名  from 表名 where  year((getdate()-1))='"+str1+"' and month((getdate()-1))='"+str2+"' and day((getdate()-1))='"+str3"'
      

  16.   

    看错 原来是数据库里的字段,那就这样!
    select   count(*)   列名     from   表名   where  year(postattime)= year((getdate()-1  and   month(postattime)=month((getdate()-1))   and   day(postattime)=day((getdate()-1))
      

  17.   

    头有点大了
    我再说说需求吧
    现在需要一条sql 查询当天前一天0点到24点期间 a字段的数量 我的数据库是mysql数据库postattime这个字段是存时间的字段 类型是datetime 那位大侠帮帮忙哦
      

  18.   

    上面少个括号!补正!
    select  count(*)  列名    from  表名  where  year(postattime)= year((getdate()-1))  and  month(postattime)=month((getdate()-1))  and  day(postattime)=day((getdate()-1)) 
    sqlserver2005验证过了!
      

  19.   

    加上也报错 我用的是mysql 不过还是谢谢你了
      

  20.   

    Getdate()  MYSQL不能用?那你把Getdate() 改为selectnow() 或者now()试下!
      

  21.   

    now()-1 就是前天的,就像getdate()-1一样,SQL2005中getdate()-1就是昨天的,我试过,now()在MYSQL中应该同理呀!
      

  22.   

    select *  from  lybbs_posttopic  where  year(postattime)= year((now()))  and  month(postattime)=month((now()))  and  day(postattime)=day((now()))-1
    这样写可以解决
      

  23.   

    不能这样,万一是一个月的第一天是今天,你要昨天的数据那么月份就不对了,应改为
    select *  from  lybbs_posttopic  where  year(postattime)= year((now()-1))  and  month(postattime)=month((now()-1))  and  day(postattime)=day((now()-1))
      

  24.   

    select now() from lybbs_posttopic

    select now()-1 from lybbs_posttopic
    你试下打印出来有什么区别
      

  25.   

    select count(a) from table where data like '%2009-01-01%';
      

  26.   

    select now() from lybbs_posttopic 
    和 
    select now()-1 from lybbs_posttopic 
    两个打印出来都一样?
      

  27.   

    郁闷了!那我这样问吧:
    问题1:select now() from lybbs_posttopic 打印出来是什么?
    问题2:select now()-1 from lybbs_posttopic 打印出来是什么?
      

  28.   

    SELECT count(*) FROM 表名 WHERE DATEDIFF(NOW(),时间字段)=1
      

  29.   

    我原来写的,那你把getdate改成Now就应该行了再引用下josy的,改成:
    SELECT count(*) FROM 表名 WHERE DATEDIFF(day,时间字段,NOW())=1
    这个可以解决,代码也少些
      

  30.   

    不用改啊
    mysql> SELECT DATEDIFF(NOW(),'2009-09-24');
    +------------------------------+
    | DATEDIFF(NOW(),'2009-09-24') |
    +------------------------------+
    |                            1 |
    +------------------------------+
    1 row in set (0.14 sec)mysql>
      

  31.   

    上面的写话简单些,但是从sql的优化角度考虑,还不是特好,sql中,尽量不要对你表里的字段进行运算,
    convert,datediff,再运算的时候都会需要时间的,尤其是表里很多记录的时候,
    所以,你可以在程序里面定义一个存储前一天的起始时间和一个结束时间,然后,拼成一个sql
    比如你最后算完的一个startDate,一个是endDate那么拼成sqlselect count(*) aCount from [table] 
    where [columnName] = 'a' 
    and [表中记录时间的那个字段] >= startDate 
    and [表中记录时间的那个字段] <= dateDate
    如果你时间设置了聚集索引,直搜索范围内的记录,这样效率会高些而运算要把每条记录都进行一次运算
      

  32.   

    不好意思,看来mysql和Sqlserver的用法太不一样了,抱歉
      

  33.   

    我以前也是这么做的,用datediff()=-1 出现数据库配置问题
      

  34.   

    where里最好别对字段部分用函数,影响性能select count(1)
    from table
    where postattime >=  CURDATE()
    and postattime <  DATE_ADD(CURDATE(), INTERVAL 1 DAY);