You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony自定义查询用JSON_TABLE报MariaDB语法错误求助

Symfony自定义仓库查询的MariaDB语法错误排查

我在Symfony中编写了一个用于计算价格的自定义仓库查询,代码如下:

$sql = " SELECT
           SUM(sub.totalPriceWT) AS totalPrice,
           c.label
       FROM (
           SELECT
               v.id,
               (
                   SELECT SUM(priceWT)
                   FROM JSON_TABLE(
                       JSON_UNQUOTE(JSON_EXTRACT(v.dac, '$.bdc.service.operator.entries[*].priceWT')),
                       '$[*]' COLUMNS(priceWT DECIMAL(10, 2) PATH '$')
                   ) der
               ) AS totalPriceWT
           FROM valid v
           JOIN sheet s ON v.sheet_id = s.id
           JOIN step st ON s.step_id  = st.id
           WHERE JSON_UNQUOTE(JSON_EXTRACT(v.dac, '$.type')) = 'bdc'
               AND (
                   (YEAR(s.sent_date) = :currentYear AND CAST(JSON_EXTRACT(v.dac, '$.anticipated') AS SIGNED) = :anticipated2)
                   OR
                   (YEAR(s.sent_date) = :previousYear AND CAST(JSON_EXTRACT(v.dac, '$.anticipated') AS SIGNED) = :anticipated1)
               )
               AND s.perimeter_id = :perimeter
               AND JSON_UNQUOTE(JSON_EXTRACT(v.dac, '$.bdc.service.operator.entries')) IS NOT NULL
               AND st.slug != 'rejetee'
       ) AS sub
       JOIN valid v2 ON sub.id = v2.id
       JOIN sheet s2 ON v2.sheet_id = s2.id
       JOIN cost_center c ON s2.cost_center_id = c.id
       GROUP BY c.id
       ";

$conn = $this->manager->getConnection();
$stmt = $conn->prepare($sql);
$stmt->bindValue('perimeter', $perimeter->getId());
$stmt->bindValue('currentYear', $year);
$stmt->bindValue('previousYear', $year - 1);
$stmt->bindValue('anticipated1', 1);
$stmt->bindValue('anticipated2', 0);
$resultSet = $stmt->executeQuery();
$resultBDC = $resultSet->fetchAllAssociative();

执行该查询时出现如下错误:

An exception occurred while executing a query: 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 '( JSON_UNQUOTE(JSON_EXTRACT(v.dac, '$.bdc.serv...' at line 9

原本以为使用的是MySQL,但错误提示指向MariaDB,后确认实际运行环境为MariaDB。执行php bin/console debug:container --env-vars和php bin/console debug:config doctrine dbal命令查看,输出显示为MySQL/pdo_mysql,无法定位问题所在,请求帮助排查。

内容的提问来源于stack exchange,提问作者Laura Leroy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 11:38:12