mysql 提取codeigniter中日期时间范围之间的行

k4ymrczo  于 2022-10-31  发布在  Mysql
关注(0)|答案(2)|浏览(149)

我的表中有这些行,数据库中有startend列,它们的数据类型为datetime

start                 |    end 
1    2022-10-27 11:59:00       2022-10-27 01:00:00
2    2022-10-28 01:59:00       2022-10-28 05:00:00
3    2022-11-22 11:59:00       2022-11-22 07:00:00
4    2022-11-25 01:59:00       2022-11-25 06:00:00

使用此查询检索位于给定日期时间组合之间的行数

$this->db->query("SELECT * FROM booking WHERE (TIMEDIFF('$start_time', TIME(start)) >=0 AND TIMEDIFF('$start_time', TIME(end)) <= 0) AND (TIMEDIFF('$end_time', TIME(start)) >=0 AND TIMEDIFF('$end_time', TIME(end)) <= 0) AND user_id=$user_id")->num_rows();

其中start_timeend_time的值类似于

$start_time=13:00:00
    $end_time=22:59:00

    $start_time=date('Y-m-d H:i:s', strtotime($fromDate.$start_time));
    $end_time=date('Y-m-d H:i:s', strtotime($toDate.$end_time));

我也试过这个查询,但不起作用

select * from booking where (start between '2022-10-27 12:20:00' and '2022-10-27 14:50:00') AND (end between '2022-10-27 12:20:00' and '2022-10-27 14:50:00')

任何解决方案。谢谢

jvlzgdj9

jvlzgdj91#

请尝试下面查询,希望您能得到您的结果。

select * from booking where start <= '2022-10-27 12:20:00' and  start >= '2022-10-27 14:50:00'AND end between '2022-10-27 12:20:00' and '2022-10-27 14:50:00')
l2osamch

l2osamch2#

请尝试使用以下代码:

SELECT *
FROM booking
WHERE start BETWEEN '2022-10-27 12:20:00'
                AND '2022-10-27 14:50:00';

相关问题