对于带有排序顺序的SELECT查询,我准备这个语句:
$direction = 'DESC';
$project_id = 1;
$query = $this->connection->prepare('SELECT text FROM marker WHERE project_id = :project_id ORDER BY time :direction');
$query->bindValue(':project_id', $project_id, PDO::PARAM_INT);
$query->bindValue(':order', $direction, PDO::PARAM_STR);
$success = $query->execute();
但它失败了。所以我准备这个声明:
$query = $this->connection->prepare('SELECT text FROM marker WHERE project_id = :project_id ORDER BY time '.$direction.'');
$query->bindValue(':project_id', $project_id, PDO::PARAM_INT);
$success = $query->execute();
它有效。
可以不绑定ORDER属性吗? 如何正确绑定ORDER属性?
答案 0 :(得分:1)
您可以使用case
声明表达您想要的内容。这是一个相对简单的表达式:
order by (case when :order = 'DESC' then time end) DESC,
time ASC