我有两张表parent
,children
,关系为1:M。我需要一个分页。我使用SELECT和JOIN。对于1位父母,我有一些孩子。如果我尝试使LIMIT 10
此查询总共获得10行。但我需要从表parent
获得所有关系的10行。我该怎么办?很抱歉,我的英文。提前谢谢。
Mysql查询:
SELECT `o`.`id` AS `order_id`, `od`.`id` AS `order_destination_id` FROM `order` AS `o`
LEFT JOIN `order_destination` AS `od` ON o.id = od.order_id LIMIT 5
Zend框架:
$select = $this->select()
->setIntegrityCheck(false)
->from(array('o' => 'order'), array('order_id' => 'id'))
->joinLeft(array('od' => 'order_destination'), 'o.id = od.order_id', array('order_destination_id' => 'id'))
->limit(5);
答案 0 :(得分:1)
我不知道Zend Framework。这是一个简单的SQL解决方案:
SELECT `o`.`id` AS `order_id`, `od`.`id` AS `order_destination_id` FROM `order` AS `o`
LEFT JOIN `order_destination` AS `od` ON o.id = od.order_id
where (select count(*) from order o2 where o2.id > o.id) < 10
答案 1 :(得分:1)
最好使用Zend_Paginator通过一个表进行分页,然后为每个项目请求子项:
$paginator = Zend_Paginator::factory($this->select(), 'DbTableSelect');
$paginator->setItemsCountPerPage(5);
foreach ($paginator as $row) {
$children = $row->getDependentRowset('ChildrenRule');
}