https://stackoverflow.com/a/12772507/1507546
我想通过学说执行此查询,但是我在下面遇到错误
[学说 dbal driver pdoexception] SQLSTATE [42000]:语法错误或访问违规:1064您的SQL语法中有错误;检查与您的MariadB服务器版本相对应的手册,以获取合适的语法 '@counter:= 0'在第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);