我需要选择start
和due
列范围落入另一个范围的所有行。所有时间都以unix时间戳格式给出。
以下查询有效,但start
< :weekstart
或due
> :weekend
。
$week_start = (new DateTimeImmutable())->setTimestamp($_REQUEST['startdatets']);
$week_start_ts = $week_start->getTimestamp();
$week_end = $week_start->modify('+6 days')->setTime(23,59,59);
$week_end_ts = $week_end->getTimestamp();
$query = " SELECT *
FROM schedules
WHERE ((start BETWEEN :weekstart AND :weekend) OR (due BETWEEN :weekstart AND :weekend))
ORDER BY start
";
try { $stmt = $dbh->prepare($query); $stmt->execute(array(':weekstart' => $week_start_ts, ':weekend' => $week_end_ts)); } catch(PDOException $ex) { echo 'Query failed: ' . $ex->getMessage(); exit; }
如果可能,我需要两者都工作 请帮忙。
答案 0 :(得分:1)
啊,所以你错过了start < :weekstart
和end > :weekend
的情况 - 两者之间都没有,但是这一周与范围重叠。它有一个很好的逻辑。
搜索句点WHERE start < :weekend AND end > :weekstart