Doctrine查询语言中的子查询

时间:2015-05-18 10:39:43

标签: mysql doctrine-orm

我有一个SQL查询,我试图将其转换为DQL。似乎DQL不喜欢在FROM子句中使用子查询。你能指导我在riqht方向吗?

SQL:

select count(x.remote_addr), ipc.country
from
(
select distinct(remote_addr) from update_feature_requests ufr
where ufr.request_datetime BETWEEN '2015-05-14' AND '2015-05-15'
)
as x
join ip_geolocation_cache ipc ON ipc.ip_address = x.remote_addr
group by ipc.country;

DQL:

$dql = "select
            count(x.remoteAddr), ipc.country
            from
            (
              select distinct(remoteAddr) from " . UpdateFeatureRequest::class . " ufr
              where ufr.requestDatetime BETWEEN '2015-05-14' AND '2015-05-15'
            )
            as x
            join " . IpGeolocationCache::class . " ipc ON ipc.ipAddress = x.remoteAddr
            group by ipc.country";
  

Doctrine \ ORM \ Query \ QueryException HELP [Semantical Error]行   0,col 82 near'(':错误:Class'('未定义。

1 个答案:

答案 0 :(得分:1)

Doctrine仅以有限的方式(如列,其中,存在等)支持子查询,并且不幸的派生表(from中的子查询)不是其中之一。您必须重新构建整个查询以使其成为dql。

等效查询可能是(sql版本):

select count(distinct ufr.remote_addr), ipc.country
from update_feature_requests ufr
join ip_geolocation_cache ipc ON ipc.ip_address = ufr.remote_addr
where ufr.request_datetime BETWEEN '2015-05-14' AND '2015-05-15'
group by ipc.country;

(它应该计算每个国家的唯一地址,这是我阅读原始查询的方式)

其他可能性是使用native query