ฉันมีตารางด้านล่างในฐานข้อมูล
id | list_id |venue_id |name | start_date | end_date |start_time | end_time
1 | 1 | 1 |asdf | 2019-02-02| 2019-02-28 |05:30 |10:00
2 | 7 | 2 |awed | 2019-02-10| 2019-02-20 |07:30 |14:00
3 | 7 | 1 |mjgd | 2019-02-04| 2019-02-13 |09:30 |18:00
ตอนนี้ ฉันต้องค้นหา start_date และ end_date ของ venue_id=1
ระหว่างช่วง 2019-02-04
ถึง 2019-03-01
ดังนั้นฉันจึงใช้แบบสอบถามด้านล่าง
SELECT * FROM `batch_list` WHERE `venue_id` = 1 and ((start_date between '2019-02-04' and '2019-03-01') OR (end_date between '2019-02-04' and '2019-03-01'))
ตอนนี้ฉันต้องค้นหาวันที่และเวลาของ place_id=1 ซึ่งเป็นช่วง 2019-02-04
และ 2019-03-01
และ 05:00
ถึง 18:00
ดังนั้นฉันจึงลองค้นหาด้านล่าง
SELECT * FROM `batch_list` WHERE `venue_id` = 1 and ((start_date between '2019-02-04' and '2019-03-01') OR (end_date between '2019-02-04' and '2019-03-01')) and((start_time <= `end_time`) and (`start_time` between '05:00' and '18:00')and (end_time between '05:00' and '18:00'))
ดังนั้นจึงไม่มีปัญหาจนถึงขณะนี้
ฉันมีเวลาเป็น 05:00
ถึง 20:00
ฉันจะได้รับบันทึกทั้งหมดที่เกี่ยวข้องกับ place_id 1 แต่หากฉันเปลี่ยนเวลา 10:00
เป็น 13:00
ฉันจะไม่ได้รับบันทึก
ฉันต้องค้นหาว่ามี start_time และ end_time อยู่ในช่วงหรือไม่ หากพบให้ตรวจสอบ start_date และ end_date
คุณจะช่วยฉันในเรื่องนี้หรือไม่?
แบบอย่าง
function fetchBatches($venue_id,$new_batch_start_date,$new_batch_end_date,$new_batch_start_time,$new_batch_end_time)
{
$where="batch_venue_id='$venue_id' and ((start_date between '$new_batch_start_date' and '$new_batch_end_date')OR (end_date between '$new_batch_start_date' and '$new_batch_end_date')) and ((start_time<=end_time) and (start_time < '$new_batch_start_time' and start_time < '$new_batch_end_time'))";
$result =$this->db->select('*')
->from('batch_list')
->where($where)
->get()
->result();
if ($result) {
return $result;
}
else{
return 0;
}
}
2019-02-02 05:30
ถึง2019-02-28 10:30
- person splash58   schedule 01.02.2019