我需要在日期列为以下时选择行:1)仅大于现在,2)仅少于现在,3)任何,基于单个变量的单个查询。
$finished = false (1) / true (2) / null (3);
if ($finished === false) { // Greater then now
$date = $now;
} else if ($finished === null) { // Any date
$date = '1970-01-01 00:00:00';
} else if ($finished === true) { // Less then now
???
}
SELECT * FROM table WHERE date > :date
如何修改查询以选择日期少于现在?我试图将大于然后签名(>)放入$ date变量,但PDO返回错误。是否可以在单个查询中完成?
抱歉英语。
更新
单个查询,单个变量。如果我想搜索完成的行设置$ finished = true。只搜索未完成的行设置$ finished = false等。这是我的工作示例:
$now = date('Y-m-d H:i:s');
if ($finished === null) { // Search for any rows.
$after = $now;
$before = $now;
} else if ($finished === true) { // Search for finished rows (date <= now).
$after = null;
$before = $now;
} else if ($finished === false) { // Search for not finished rows (date > now).
$after = $now;
$before = null;
}
$query = $this->db->prepare("SELECT * FROM table WHERE (date > :after OR date <= :before)");
$query->execute(array(
':after' => $after,
':before' => $before
));