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