0

我正在使用 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 列,这是错误还是需要技巧?

异常结果:

4

1 回答 1

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    |
+========+=========+
于 2013-01-09T09:45:39.483 回答