gpt4 book ai didi

php - 如何在 Doctrine 中使用mysql变量

转载 作者:行者123 更新时间:2023-11-29 10:47:45 26 4
gpt4 key购买 nike

https://stackoverflow.com/a/12772507/1507546

我想通过 Doctrine 执行此查询,但收到以下错误

[Doctrine\DBAL\Driver\PDOException] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '@counter := 0' at line 1

这是我的代码

$sql = <<<S
SET @counter = 0;
Select sub.orderid,sub.value,(@counter := @counter +1) as counter
FROM
(
select orderid,
round(sum(unitprice * quantity),2) as value
from order_details
group by orderid
) sub
order by 2 desc
limit 10
S;

stmt = $this->_em->getConnection()->prepare($sql);

$stmt->execute();

return $stmt->fetchAll(AbstractQuery::HYDRATE_ARRAY);

最佳答案

大多数 sql API 不允许在没有额外配置的情况下使用多个语句。您需要将它们作为单独的语句传递:

$this->_em->getConnection()->exec("SET @counter = 0"); // May need tweaking, I'm not familiar with Doctrine
$sql = <<<S
Select sub.orderid,sub.value,(@counter := @counter +1) as counter
FROM
(
select orderid,
round(sum(unitprice * quantity),2) as value
from order_details
group by orderid
) sub
order by 2 desc
limit 10
S;

stmt = $this->_em->getConnection()->prepare($sql);

关于php - 如何在 Doctrine 中使用mysql变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44267560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com