比如我现在有一张帖子表posts 其中关键字段有发贴时间dateline,pid,tid,fid……
现在我需要查询近1、2天,1、3周,1、3月等所发的帖子;该如何写sql语句?

解决方案 »

  1.   

    1、2天:datediff(curdate(),dateline)=1 or 2
    1、3月:DATE_ADD(curdate(),INTERVAL -1 MONTH)=1 or 3, 
    1、3周:DATE_ADD(curdate(),INTERVAL -1 WEEK)=1 or 3
      

  2.   

    近1天
    select *
    from posts  where dateline > curdate()-1 ;近2天
    select *
    from posts  where dateline > curdate()-2 ;
      

  3.   

    近1周
    select *
    from posts  where dateline > curdate()-7 ;select *
    from posts  where dateline > DATE_ADD(curdate(),INTERVAL -1 WEEK) ;
    近3周
    select *
    from posts  where dateline > curdate()-7*3 ;select *
    from posts  where dateline > DATE_ADD(curdate(),INTERVAL -3 WEEK) ;
      

  4.   

    近1月select *
    from posts  where dateline > DATE_ADD(curdate(),INTERVAL -1 MONTH) ;select *
    from posts  where dateline > DATE_SUB(curdate(),INTERVAL 1 MONTH) ;近3月
    select *
    from posts  where dateline > DATE_ADD(curdate(),INTERVAL -3 MONTH) ;select *
    from posts  where dateline > DATE_SUB(curdate(),INTERVAL 3 MONTH) ;
      

  5.   

    1、2天:datediff(curdate(),dateline)=1 or 2
    1、3月:DATE_ADD(curdate(),INTERVAL -1 or -3 MONTH)=1 or 3,
    1、3周:DATE_ADD(curdate(),INTERVAL -1 or -3 WEEK)=1 or 3