Symfony主义DQL如何在主SELECT的SUM中添加SELECT

问题描述 投票:0回答:1

实际上,我想使用Doctrine来复制此常规SQL查询:

SELECT SUM(b.nb_places * (SELECT pricing FROM param p WHERE t.date BETWEEN p.from_date AND p.to_date)) as gains from booking b INNER JOIN tour t ON b.tour_id = t.id;

根据两个日期之间的价格计算总收益。

我在存储库中编写了此DQL:

    public function allBooking() {
        $query = $this->manager->createQuery(
            'SELECT 
                 SUM(b.nbPlaces * SELECT p.pricing FROM \App\Entity\Param p WHERE t.date BETWEEN p.fromDate AND p.toDate)
        FROM App\Entity\Booking b JOIN b.tour t'
    );
    return $query->getResult();
}

但是运行此查询,我得到了:

[Syntax Error] line 0, col 24: Error: Expected Literal, got 'SELECT' (500 Internal Server Error)

如何使用DQL或QueryBuilder实现此查询?

寻求帮助

symfony doctrine dql
1个回答
0
投票

您忘记放置

SELECT p.pricing FROM \App\Entity\Param p WHERE t.date BETWEEN p.fromDate AND p.toDate

放入其他括号。

它看起来应该像这样:

$query = $this->manager->createQuery(
    'SELECT SUM(b.nbPlaces * (SELECT p.pricing FROM \App\Entity\Param p WHERE t.date BETWEEN p.fromDate AND p.toDate))
    FROM App\Entity\Booking b JOIN b.tour t'
);
© www.soinside.com 2019 - 2024. All rights reserved.