使用分组依据时的错误原则查询

时间:2013-01-09 09:23:25

标签: php postgresql doctrine symfony-1.4

我正在使用Symfony Project 1.4和Postgresql

我在使用

的学说中获得了错误查询
$q = Doctrine_Query::create()
 ->select("count(sex) as total, sex")
 ->from('Biodata')
 ->groupBy('sex')           
 ->execute();

调试时总会出现这样的显示错误:

SQLSTATE[42803]: Grouping error: 7 ERROR: column "e.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT e.id AS e__id, e.sex AS e__sex, COUNT(e.sex) AS e__0 ...
^. Failing Query: "SELECT e.id AS e__id, e.sex AS e__sex, COUNT(e.sex) AS e__0 FROM biodata e GROUP BY e.sex"

如何修复它并隐藏e.id列,这是错误还是需要一招?

例外结果:

1 个答案:

答案 0 :(得分:1)

Doctrine会自动将主键添加到选择列表中。使用MSSQL我们遇到了同样的错误。

尝试添加不含水合物的结果:

$q = Doctrine_Query::create()
 ->setHydrationMode(Doctrine_Core::HYDRATE_NONE)
 ->select("count(sex) as total, sex")
 ->from('Biodata')
 ->groupBy('sex')
 ->execute();

你不会有列名,但我想是这样的:

+========+=========+
|   0    |    1    |
+========+=========+
|  183   |    1    |
+--------+---------+
|   54   |    2    |
+========+=========+