mysql - 分组前的 Zend 框架顺序

标签 mysql sorting zend-framework group-by

有没有人举例说明如何在 zend 分页器适配器中使用 leftjoin 在组前排序?

new Zend_Paginator_Adapter_DbSelect($this->db->select()
->from(array( 'a' => $this->prefix.'contest' ), array('id'))
->joinLeft(array( 'b' => $this->prefix.'logo_to_contest'),'a.id=b.contest_id', array('img'))
->group('a.id')
->order('a.end','a.date_start DESC','b.id RAND()')
)

最佳答案

来自 mysql 手册

In general, clauses used must be given in exactly the order shown in the syntax description. For example, a HAVING clause must come after any GROUP BY clause and before any ORDER BY clause. The exception is that the INTO clause can appear either as shown in the syntax description or immediately following the select_expr list.

并且在 syntax description group 在 order 之前出现,所以它与 zend 无关 mysql 要求先组后序。

然而,为了解决这个问题并在排序后分组,您可以使用带有顺序的子查询进行选择,然后在新的选择上分组,例如:

$subselect = $db->select()
->from(array( 'a' => $this->prefix.'contest' ), array('id'))
->joinLeft(array( 'b' => $this->prefix.'logo_to_contest'),'a.id=b.contest_id', array())
->order('a.end','a.date_start DESC','b.id RAND()');

$select = $db->select()->from(a(array( 'a' => $this->prefix.'contest' ), array('id'))
->joinLeft(array( 'b' => $this->prefix.'logo_to_contest'),'a.id=b.contest_id', array('img'))
->where("a.id in ($subselect)")
->group('a.id');

关于mysql - 分组前的 Zend 框架顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10363886/

相关文章:

javascript - 使用 jQuery 对列表进行数字排序,然后按字母顺序排序?

c++ - 根据特定数据对字符串进行排序的最佳方法是什么

php - postgresql 与lastinsertid 和zend 框架的序列问题

PHP zend left join 转换查询中的日期

mysql - 我正在尝试通过 jdbc 代码使用 mysql 5.5 进行简单的参数查询

mysql join 子句相当于 where 子句,反之亦然

Python:中位数为三的快速排序

php - 使用 Zend_Mail 添加多个收件人

php - SQL中的计算,如何控制它

php - 似乎无法通过 id 编辑/修改我的 php 表