PHP mssql paging SQL statement optimization with continuous impact_PHP tutorial

WBOY
Release: 2016-07-21 15:47:17
Original
1062 people have browsed it

Copy code The code is as follows:

/**
* @Filename :page.sql.class.php
* @CreatTime :2009-01-06
* @Descrition: This class is a SQL statement processing class.
* @UpdateTime-1 :null
* @Version :jswweb1.0.0
* @Author :fkedwgwy
* @Dome :
$sql//SQL statement
$allcount/ /Total number of records
$pagesize//Number of records displayed on the page
$page//Current page
$sqlc= new sqlpage($sql,$allcount,$pagesize,$page);
$sql=$sqlc->getsql();
Optimized statement:
SELECT * FROM (SELECT TOP 10 * FROM (SELECT TOP 270 Lsh,Ztm,Dyzrsm,Dyzzfs,Cbsm,Cbny,Ssh, Fbsl,jcsl from ts_gcb where Ssh like 'C%' order by Lsh asc) AS inner_tbl ORDER BY Lsh DESC) AS outer_tbl ORDER BY Lsh asc
*/
class sqlpage{
function sqlpage($sql,$allcount,$pagesize,$page){
$this->sql= $sql;//Query name
$this->allcount= intval($allcount);//Total number of records
$this->pagesize= intval($pagesize);//Page size (display number of records)
$this->page= intval($page );//Current page
$this->getpage();
$this->gettop();
}
function getpage(){ //Get the current page
$this->allpage=ceil( $this->allcount/$this->pagesize);//Go to the largest integer of the current decimal
if ($this->page=="" or $this ->page>$this->allpage or $this->page<0 or $this->page==0){
$this->page2=1;
}else{
$this->page2=intval($this->page);//Convert page number to number
}
}
function gettop(){ //Get subquery 2 TOP size
if ($this->page2<$this->allpage){
$this->top2=$this->pagesize;
}else{
$this ->top2=$this->allcount-$this->pagesize*($this->allpage-1);
}
}
/* function getsql(){// Get the SQL statement
$this->s=preg_replace("/select/i","",$this->sql);
$this->top1=$this->pagesize* $this->page2;
$this->sql1="SELECT TOP $this->top1 $this->s";
if (strpos($this->sql,"asc ")){//Ascending order
$this->sql_e="select * from ( select TOP $this->top2 * FROM ( $this->sql1 ) as aSysTable ORDER BY $this->order DESC ) as bSysTable ORDER BY $this->order ASC";
}else
//$this->sql_e="select * from ( select TOP $this->top2 * FROM ( $this ->sql1 ) as aSysTable ORDER BY $this->order DESC ) as bSysTable ORDER BY $this->order ASC";
if (strpos($this->sql,"desc")){ //Descending order
$this->sql_e="select * from ( select TOP $this->top2 * FROM ( $this->sql1 ) as aSysTable ORDER BY $this->order asc ) as bSysTable ORDER BY $this->order desc";
}else{//Does not handle sorting
$this->sql_e="select * from ( select TOP $this->top2 * FROM ( $this->sql1 ) as aSysTable ORDER BY $this->order DESC ) as bSysTable ORDER BY $this->order ASC";
}
// echo $this->sql_e;
return $this->sql_e;
}*/
function getsql()
{
$sql=$this->sql;
$this->top1= $this->pagesize*$this->page2;
$orderby = stristr($sql, 'ORDER BY');
if ($orderby !== false) {
$sort = (stripos($orderby, ' desc') !== false) ? 'desc' : 'asc';
$order = str_ireplace('ORDER BY', '', $orderby);
$order = trim(preg_replace('/bASCb|bDESCb/i', '', $order));
}
$sql = preg_replace('/^SELECTs/i', 'SELECT TOP ' . ($this- >top1) . ' ', $sql);
$sql = 'SELECT * FROM (SELECT TOP ' . $this->top2 . ' * FROM (' . $sql . ') AS inner_tbl';
if ($orderby !== false) {
$sql .= ' ORDER BY ' . $order . ' ';
$sql .= (stripos($sort, 'asc') !== false) ? 'DESC' : 'ASC';
}
$sql .= ') AS outer_tbl';
if ($orderby !== false) {
$sql .= ' ORDER BY ' . $order . ' ' . $sort;
}
echo $sql;
return $sql;
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319975.htmlTechArticleCopy the code as follows: ?php /** * @Filename :page.sql.class.php * @CreatTime :2009-01-06 * @Descrition: This class is a SQL statement processing class. * @UpdateTime-1 :null * @Version :jswweb1...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!