mysql 语句

查询区间

如 1月5日到2-31号



SELECT *  FROM ( SELECT DATE_FORMAT( birthday,%c-%d’ ) AS birthday, `name` FROM person ) AS t  WHERE `birthday` BETWEEN1-5AND2-31
SQL

年底 今年12月5号到明年月31号



SELECT  *  FROM ( SELECT DATE_FORMAT( birthday,%c-%d’ ) AS birthday, `name` FROM person ) AS t  WHERE `birthday` BETWEEN12-5AND12-31AND `birthday` BETWEEN1-1AND1-30
SQL

TP写法



 $arrWhere = array();
    $i = 100; //设置今天前后三十天
    $arrWhere = array();
    $nowTime = date(Y-m-d’);
    // $nowTime = date(‘Y-m-d’,strtotime(‘2020-05-01’));//如果要测试其他时间把这个注释打开
    $nowYear =  date(Y, strtotime($nowTime)); //获取今年
    if ($nowYear < date(Y, strtotime(+. $i . ” days”, strtoTime($nowTime)))) {
      //如果现在时间加上$i天后是明年了
      $r = explode(-, date(‘m-d’, strtotime(+. $i . ” days”, strtotime($nowTime))));
      $arrWhere[] = [‘birthday’, ‘between’, intval(date(‘m’, strtotime($nowTime))) .-. intval(date(‘d’, strtotime($nowTime))) .,. intval(date(‘m’, strtotime($nowTime))) .-. intval(date(‘t’, strtotime($nowTime)))]; //本年当天到月底最后一天
      $arrWhere[] = [‘birthday’, ‘between’,1-1,. intval($r[0]) .-. intval($r[1])]; //明年一月一号到从当天计算起的$i天后的日期
    } else {
      //今年
      $l = explode(-, date(‘m-d’, strtotime($nowTime)));
      $r = explode(-, date(‘m-d’, strtotime(+. $i . ” days”, strtotime($nowTime))));
      $arrWhere[] = [‘birthday’, ‘between’, intval($l[0]) .-. intval($l[1]) .,. intval($r[0]) .-. intval($r[1])];
    }
    $a[‘verification’] = Db::table(( SELECT DATE_FORMAT( birthday,%c-%d’ ) AS birthday, `name` FROM person ) AS t “)
      ->whereOr($arrWhere)
      ->select();
    halt($a);


PHP

 

说点什么
支持Markdown语法
好耶,沙发还空着ヾ(≧▽≦*)o
Loading...