SQL语句:两个时间区间段,只要有交集,就能筛选出来


设定固定时间段:8.2---》8.5

也就是:两个时间段,只要有交集就能筛选出来:

下面的两个sql语句,实现的效果是一样的:

1:$sql="select * from fs_ads where `start_time` <= '$ed' and `end_time` >= '$sd'";

2:

 $sql="select * from (
select ads_id,file_id,client_type,client_name,brand,model,cal_type start_time,end_time,
CASE 
when (start_time>='$sd') then start_time
when (start_time<'$sd') then '$sd'
end as start1,
CASE 
when (end_time>='$ed') then '$ed'
when (end_time<'$ed') then end_time
end as end1
 from fs_ads )
 a where a.start1<=a.end1;
"; 
注:$sd,$ed是用户选择的时间


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM