Mysql 带左联接的SQL查询中的限制
I mam SQL Query to me读取类别,我们想做分页类别也使用SQL Query to me其语句:Mysql 带左联接的SQL查询中的限制,mysql,sql,pagination,left-join,limit,Mysql,Sql,Pagination,Left Join,Limit,I mam SQL Query to me读取类别,我们想做分页类别也使用SQL Query to me其语句: public function fetchChildrenNodess($start,$end){ $q_offset = (int)$start; $q_limit = (int)($end - $start); $query= "select k1.id as id from ".Config::tableKategorie." k1 left
public function fetchChildrenNodess($start,$end){
$q_offset = (int)$start;
$q_limit = (int)($end - $start);
$query= "select k1.id as id from ".Config::tableKategorie." k1 left join ".Config::tableKategorie." k2 on k2.nlft<k1.nlft and k2.nrgt>k1.nrgt and k2.nlft>".$this->L." and k2.nrgt<".$this->R." where k1.nlft>".$this->L." and k1.nrgt<".$this->R." and k2.id is null order by k1.nlft ";
$i=0; $ret=array();
// $hash=$this->hash().'_part_'.$start.'_'.$end;
$Q=mysql_query($query."LIMIT 0, 8 ",CommerceDB::$DB);
while($R=mysql_fetch_assoc($Q)){
$ret[]=new Kategoria($R['id']);
};
return $ret;
}
所以我只列出了9个子类别,然后转到下一个子类别。子类别也包括9个类别。我需要得到我的清单只包括最多8个结果。谢谢你在这个解决方案中,你的函数应该只有一个参数,你可以增加next或者减少prev。我假设您的查询和其他代码工作正常 页面参数的顺序为1-N。如果目标是将记录集限制为8行,则不要让用户指定结束值
public function fetchChildrenNodess($page){
$range_const = 8; // # of rows to show; constant value
if( $page > 0 ) // what if page is 0 or less ???
$start = ($range_const * ($page-1)); // start limit
else
$start = 0;
$end = $range_const - $start; // end limit
$query= "select k1.id as id from ".Config::tableKategorie." k1 left join ".Config::tableKategorie." k2 on k2.nlft<k1.nlft and k2.nrgt>k1.nrgt and k2.nlft>".$this->L." and k2.nrgt<".$this->R." where k1.nlft>".$this->L." and k1.nrgt<".$this->R." and k2.id is null order by k1.nlft ";
$query = $query . ' LIMIT ' . $start . ',' . $end . ';'
$i=0; $ret=array();
$Q = mysql_query($query,CommerceDB::$DB);
while($R=mysql_fetch_assoc($Q)){
$ret[]=new Kategoria($R['id']);
}
}
测试用例
public function fetchChildrenNodess($page){
$range_const = 8; // # of rows to show; constant value
if( $page > 0 ) // what if page is 0 or less ???
$start = ($range_const * ($page-1)); // start limit
else
$start = 0;
$end = $range_const - $start; // end limit
$query= "select k1.id as id from ".Config::tableKategorie." k1 left join ".Config::tableKategorie." k2 on k2.nlft<k1.nlft and k2.nrgt>k1.nrgt and k2.nlft>".$this->L." and k2.nrgt<".$this->R." where k1.nlft>".$this->L." and k1.nrgt<".$this->R." and k2.id is null order by k1.nlft ";
$query = $query . ' LIMIT ' . $start . ',' . $end . ';'
$i=0; $ret=array();
$Q = mysql_query($query,CommerceDB::$DB);
while($R=mysql_fetch_assoc($Q)){
$ret[]=new Kategoria($R['id']);
}
}
+----------+--------+------+
| Page Num | $start | $end |
+----------+---------------+
| 1 | 0 | 8 |
| 2 | 8 | 8 |
| 3 | 16 | 8 |
| 4 | 24 | 8 |
| | | |
| 0 | 0 | 8 |
| -1 | 0 | 8 |
+----------+--------+------+