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//Page display records Number of items
$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 the 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); //Get the maximum 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 the TOP size of subquery 2
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;
}
}
?>
The above introduces the continuous impact of select statement php mssql paging SQL statement optimization, including the content of select statement. I hope it will be helpful to friends who are interested in PHP tutorials.