数据库
首页 > 数据库> > mysql-Doctrine 2无法识别FROM子句上的SELECT

mysql-Doctrine 2无法识别FROM子句上的SELECT

作者:互联网

我有一个要在Doctrine2中使用的SQL查询.

我的查询进入dabatase,执行GROUP BY用户状态并计算每个状态的用户数.

然后,我试图将一个表联接到COUNT个所有用户,并为每个州赚取一个百分比.

return $this->getEntityManager()
            ->createQuery("
                SELECT COUNT(u.id) as total, 
                    (100*(COUNT( u.id ) /  total_users.total)) as percent
                FROM UserBundle:User u,
                    (SELECT COUNT(*) as total
                     FROM UserBundle:User) as total_users
                LEFT JOIN u.idUserEstado ue
                GROUP BY u.idUserEstado")
            ->getResult();

问题是,当我运行Doctrine2查询时,出现异常:

[Semantical Error] line 0, col 397 near 
'(SELECT COUNT(': Error: Class '(' is not defined. 

Doctrine无法识别FROM子句上的SELECT.

解决方法:

from子句中的select不受教义2处理

教义的jira上有一个已关闭(但不接受)的功能请求:http://www.doctrine-project.org/jira/browse/DDC-2793

DQL is about querying objects. Supporting subselects in the FROM
clause means that the DQL parser is not able to build the result set
mapping anymore (as the fields returned by the subquery may not match
the object anymore). This is why it cannot be supported (supporting it
only for the case you run the query without the hydration is a no-go
IMO as it would mean that the query parsing needs to be dependant of
the execution mode).

标签:doctrine-orm,mysql
来源: https://codeday.me/bug/20191201/2078781.html