【发布时间】:2010-06-19 04:02:19
【问题描述】:
我正在尝试使用 Zend 框架进行分组。这是我的代码:
$table = new TableClass();
$select = $table->select();
$select->from ("table", array("date", "column1" => "sum(column1)"));
$select->group ( array ("date") );
$results = $table->fetchAll ($select);
$result = $results[0];
$date = $result->date;
$column1 = $result->column1;
TableClass 扩展了“Zend_Db_Table_Abstract”。
我可以通过查看 mysql 查询日志来查看查询。查询格式正确 - column1 在查询中命名,如果我在 mysql 工作台中运行查询,结果看起来正确。
我无法访问“column1”中的数据 - 我总是遇到此异常:
未捕获的异常 'Zend_Db_Table_Row_Exception' 带有消息'指定列“column1”不在行中'
但是,我可以毫无问题地访问日期列。
我试过了:
通过数组索引访问列: $结果[0] 但是你得到一个异常(不能按索引访问列)。
不使用列别名: $select->from ("table", array("date", "sum(column1)")); $column1 = $result["sum(column1)"]; 但你得到一个例外(没有这样的列“sum(column1)”)。
抛出 Zend_Db_Expr: "column1" => new Zend_Db_Expr ("sum(column1)" ) 但这无济于事。
我看到的其他一些示例建议使用不带聚合函数的列名,即。 “column1”而不是“sum(column1)”,但在我看来这不是答案 - 查询中没有任何聚合函数,所以 mysql 不知道如何处理它。
任何帮助表示赞赏。
【问题讨论】: