1

我正在尝试从我的数据库中获取有足够床位的酒店(用户指定客人计数参数)。查询应如下所示:

SELECT h.* FROM Hotel AS h
WHERE 
    (SELECT SUM(r.guestCount * r.count) 
    FROM Room AS r 
    WHERE r.hotel_id = h.id) >= $questCount

上面的查询在 where 子句中包含子查询。我已经阅读了学说的 QueryBuilder 文档,但我不知道如何在 QB 中进行子查询。

我现在只有:

$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder()
    ->select('h')
    ->from('AAAHotelsBundle:Hotel', 'h')
    ->where(.........???...........);

任何想法下一步该怎么做?

当然我简化了问题(查询本身要复杂得多)。我使用 Symfony2。

4

2 回答 2

2

在我的情况下,DQL 不是解决方案。我确实需要使用 QueryBuilder。我在 Google Groups 上问过同样的问题,这是解决方案:

$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder() 
    ->select('h') 
    ->from('AAAHotelsBundle:Hotel', 'h') 
    ->join('Room', 'r') 
    ->groupBy('h') 
    ->having('SUM(r.guestCount * r.count) >= :guestCount') 
    ->setParameter("guestCount", $guestCount); 
于 2011-10-20T08:38:23.900 回答
0

我认为这个 DQL 会帮助你

SELECT h, SUM(r.guestCount * r.count) as TSUM FROM Hotel h JOIN h.room r
WHERE TSUM  >= :questCount
于 2011-10-19T06:37:50.880 回答