我正在使用预订系统,用户可以预订他们的汽车,在数据库/表格中添加任何行程之前我想检查日期和时间之间已存在的行程。 这是我在模型中的查询。
$this->db->select('trId')
->from('trips')
->where('carId',$data['carId'])//car Id
->where('tripStart <=',$data['tripStart'])//trip start date
->where('tripEnd >= ',$data['tripEnd'])//trip end date
->where('tripStartTime <= ',$data['tripStartTime'])//trip start time
->where('tripStartEnd >=',$data['tripStartEnd'])//trip end time
->get();
我已经将值存储在我的旅行表中
tripStart = '2018-03-01'
tripEnd = '2018-03-03'
tripStartTime = '09:00:00'
tripStartEnd = '13:00:00'
答案 0 :(得分:1)
select trId from trips
where carId = value AND
(Between tripStart = "value" AND tripEnd = "value") AND
(Between tripsStartTime = "value" AND tripStartEnd = "value");
这就是你想要的东西
答案 1 :(得分:0)
是的,你可以但在此之前你必须连接sql列进行比较
了解SQL
CONCAT
此外,您需要使用strtotime
PHP函数
<?php
$tripStart = '2018-03-01';
$tripEnd = '2018-03-03';
$tripStartTime = '09:00:00';
$tripStartEnd = '13:00:00';
$from = date('Y-m-d H:i:s',strtotime($tripStart.$tripStartTime));
$to = date('Y-m-d H:i:s',strtotime($tripEnd.$tripStartEnd));
$this->db->select('trId')
->from('trips')
->where('carId',1)//car Id
->where("CONCAT(tripStart,'',tripStartTime) > ",$from)
->where("CONCAT(tripEnd,'',tripStartEnd) < ",$to)
->get();
?>