我正在尝试在我的sql中使用相同的参数,但它只识别第一个。看我的代码:
$stmt = $dbh->prepare("SELECT
(SELECT SUM(oq.value)
FROM operations_quotas AS oq
JOIN operations AS o ON oq.operation = o.id
WHERE o.user = :user AND o.type = 1 AND oq.status = 1
) AS total_incomes_open,
(SELECT SUM(oq.value)
FROM operations_quotas AS oq
JOIN operations AS o ON oq.operation = o.id
WHERE o.user = :user AND o.type = 1 AND oq.status = 2
) AS total_incomes_wroteoff");
$stmt->bindParam(":user", $this->getId());
$stmt->execute();
可能吗?
答案 0 :(得分:5)
不可能重复使用这样的参数。你必须制作独特的参数:
$stmt = $dbh->prepare("SELECT
(SELECT SUM(oq.value)
FROM operations_quotas AS oq
JOIN operations AS o ON oq.operation = o.id
WHERE o.user = :user_a AND o.type = 1 AND oq.status = 1
) AS total_incomes_open,
(SELECT COUNT(oq.id)
FROM operations_quotas AS oq
JOIN operations AS o ON oq.operation = o.id
WHERE o.user = :user_b AND o.type = 1 AND oq.status = 2
) AS total_incomes_wroteoff");
$stmt->bindParam(":user_a", $this->getId());
$stmt->bindParam(":user_b", $this->getId());
$stmt->execute();
来自Manual:
在调用PDOStatement :: execute()时,必须为要传递给语句的每个值包含唯一的参数标记。您不能在预准备语句中两次使用同名的命名参数标记。您不能将多个值绑定到单个命名参数,例如,SQL语句的IN()子句。