java根据始末年份获得中间年份_比较年份,用substr截取前4位快,还是date('Y')快?...

1问题描述:根据始末日期查询数据,由于数据库数据量大,用between and很慢,就添加了year,month,day字段,在比较年月日的时候有substr和date两种方法,两种都尝试过每次加载时间都在变化,也没找到相关的技术文章。理论上是两种速度都差不多,还是有相对快的方法?感谢每一个答题者。

2.相关代码片段:

//substr

if(substr($startTime,0,4) == substr($endTime,0,4)){

$str .= " L.year = ".substr($startTime,0,4);

if(substr($startTime,4,2) == substr($endTime,4,2)){

$str .= " and L.month = ".substr($startTime,4,2);

if(date('Ynd',strtotime($startTime)) == date('Ynd',strtotime($endTime))){

$str .= " and day = ".date('d',strtotime($startTime));

}else{

$str .= " and log_date between '$startTime' and '$endTime' ";

}

}else{

$str .= " and log_date between '$startTime' and '$endTime' ";

}

}else{

$str .= " L.log_date between $startTime and $endTime";

}

//date

if(date('Y',strtotime($startTime)) == date('Y',strtotime($endTime))){

$str = " where year = ".date('Y',strtotime($startTime));

if(date('Yn',strtotime($startTime)) == date('Yn',strtotime($endTime))){

$str .= " and month = ".date('n',strtotime($startTime));

if(date('Ynd',strtotime($startTime)) == date('Ynd',strtotime($endTime))){

$str .= " and day = ".date('d',strtotime($startTime));

}else{

$str .= " and log_date between '$startTime' and '$endTime' ";

}

}else{

$str .= " and log_date between '$startTime' and '$endTime' ";

}

}else{

$str = " where log_date between '$startTime' and '$endTime' ";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值