从一个大的mysql查询计数行(zend)

时间:2012-03-15 21:55:00

标签: mysql zend-framework zend-db-select

我在zend中开发并且有一个相当大的mysql查询。查询工作正常,我得到了我期望的列表。我这样做是使用Select-> Where ....下面是查询。

SELECT DISTINCT `d`.* FROM `deliverable` AS `d` INNER JOIN `groups` AS `g1` ON d.id = g1.deliverable_id INNER JOIN `groupmembers` AS `gm1` ON g1.id = gm1.group_id LEFT JOIN `connection` AS `c` ON d.id = c.downstreamnode_id LEFT JOIN `deliverable` AS `d1` ON c.upstreamnode_id = d1.id INNER JOIN `deliverable` AS `d2` ON CASE WHEN d1.id IS NULL THEN d.id ELSE d1.id END = d2.id INNER JOIN `groups` AS `g` ON d2.id = g.deliverable_id INNER JOIN `groupmembers` AS `gm` ON g.id = gm.group_id WHERE (g1.group_type = 100) AND (gm1.member_id = 1) AND (c.downstreamnode_id IS NULL OR d.restrict_access = 1) AND (g.group_type = 100 OR g.group_type = 110) AND (gm.member_id = 1) AND (d.deliverable_type = 110 OR d.deliverable_type = 100) GROUP BY CASE WHEN c.downstreamnode_id IS NULL THEN d.id ELSE c.downstreamnode_id END

唯一的问题是,当我尝试计算mysql查询中的行时,我只返回1。下面是查询

SELECT DISTINCT count(*) AS `rowCount` FROM `deliverable` AS `d` INNER JOIN `groups` AS `g1` ON d.id = g1.deliverable_id INNER JOIN `groupmembers` AS `gm1` ON g1.id = gm1.group_id LEFT JOIN `connection` AS `c` ON d.id = c.downstreamnode_id LEFT JOIN `deliverable` AS `d1` ON c.upstreamnode_id = d1.id INNER JOIN `deliverable` AS `d2` ON CASE WHEN d1.id IS NULL THEN d.id ELSE d1.id END = d2.id INNER JOIN `groups` AS `g` ON d2.id = g.deliverable_id INNER JOIN `groupmembers` AS `gm` ON g.id = gm.group_id WHERE (g1.group_type = 100) AND (gm1.member_id = 1) AND (c.downstreamnode_id IS NULL OR d.restrict_access = 1) AND (g.group_type = 100 OR g.group_type = 110) AND (gm.member_id = 1) AND (d.deliverable_type = 110 OR d.deliverable_type = 100) GROUP BY CASE WHEN c.downstreamnode_id IS NULL THEN d.id ELSE c.downstreamnode_id END

我通过使用生成第一个查询的相同“select”生成此内容,但我重置了列并添加了计数。

$this->getAdapter()->setFetchMode(Zend_Db::FETCH_ASSOC);
    $select
        ->reset( Zend_Db_Select::COLUMNS)
        ->columns(array('count('.$column.') as rowCount'));
    $rowCount = $this->getAdapter()->fetchOne($select);

这个方法适用于我所有其他查询只有这个我遇到麻烦。我怀疑它有什么可以做我在那里'CASE',但它很奇怪,因为我在第一个查询得到正确的行。有任何想法吗。感谢。

以下是我成功运作的两个查询。

SELECT DISTINCT `po`.* FROM `post` AS `po` INNER JOIN `postinfo` AS `p` ON po.postinfo_id = p.id WHERE (p.creator_id = 1) ORDER BY `p`.`date_created` DESC

SELECT DISTINCT count(*) AS `rowCount` FROM `post` AS `po` INNER JOIN `postinfo` AS `p` ON po.postinfo_id = p.id WHERE (p.creator_id = 1) ORDER BY `p`.`date_created` DESC

在这一个中,我在第一个查询中返回了4行,并为第二个查询返回了“int 4”。有谁知道它为什么不能用于大查询?

3 个答案:

答案 0 :(得分:0)

移动DISTINCT

SELECT COUNT(DISTINCT `po`.*) AS `rowCount` ...

答案 1 :(得分:0)

好的想通了它是GROUP BY导致只返回1个结果。感谢Interrobang为您提供帮助我确信使用DISTINCT不正确将导致我将来头疼。

答案 2 :(得分:0)

尝试在查询中使用SQL_CALC_FOUND_ROWS? http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

使用SQL_CALC_FOUND_ROWS是特定于mysql的,但即使您的初始查询包含限制,它也非常适合获取完整记录计数。获得计数后,不要在后续查询中包含SQL_CALC_FOUND_ROWS以获取额外记录,因为这会对查询造成额外负担。

您的初步查询是:

SELECT SQL_CALC_FOUND_ROWS  DISTINCT `d`.* FROM `deliverable` AS `d` INNER JOIN `groups` ...

执行初始查询后,您必须执行后续调用才能通过执行SELECT FOUND_ROWS()来获取计数。

如果你进行一些搜索,你会找到一个扩展Zend_Db_Select以包含此能力的人。