我试图用case创建一个搜索查询,但遇到了错误“invalid parameter number”。有人能帮我解决这个问题吗?我该怎么解决?
$query = 'SELECT *,
CASE WHEN o.title LIKE :keyword THEN 1
WHEN c.body LIKE :keyword THEN 2
ELSE 99 END AS priority
FROM orders AS o INNER JOIN
comment_relations AS cr ON o.id = cr.target_id INNER JOIN
comments AS c ON cr.comment_id = c.id
WHERE cr.type = 2
ORDER BY priority';
$stmt = $db->prepare($query);
$stmt->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
table看起来像这样
ORDER:
ID = 95
title = first order
COMMENT RELATIONS:
id = 1241
comment_id = 500
target_id = 95
type = 2
COMMENT:
id = 500
body = this is the first comment
1条答案
按热度按时间uttx8gqw1#
我猜这个问题与两次使用同一个参数有关。如果有2个参数,则必须指定2个参数。
你应该试着写两个不同的参数,即使它们有相同的值,看看会发生什么。
同样的问题在这里也有报道