https://stackoverflow.com/a/12772507/1507546
我想通过学说执行此查询,但我收到以下错误
[学说\ DBAL \驱动\ 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);
答案 0 :(得分:1)
大多数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);