Home > Database > Mysql Tutorial > body text

如何通用存储过程来对MySQL分页查询进行操作

WBOY
Release: 2016-06-07 16:11:32
Original
1300 people have browsed it

以下的文章主要描述的是通用存储过程来对MySQL分页查询进行正确的操作,你如果对MySQL分页查询进行正确的操作有兴趣的话你就可以点击以下的文章进行观看了。望你浏览之后能有所收获。 前段时间没有给出SQLServer转到MySQL(和PHP搭配之最佳组合)的通用存储过

以下的文章主要描述的是通用存储过程来对MySQL分页查询进行正确的操作,你如果对MySQL分页查询进行正确的操作有兴趣的话你就可以点击以下的文章进行观看了。望你浏览之后能有所收获。

前段时间没有给出SQLServer转到MySQL(和PHP搭配之最佳组合)的通用存储过程,本着共享的精神,为大家奉献这段MySQL分页查询通用存储过程,假设所用数据库为guestbook:

<ol class="dp-xml">
<li class="alt"><span><span>use guestbook;  </span></span></li>
<li><span>delimiter $$  </span></li>
<li class="alt"><span>drop procedure if exists prc_page_result $$  </span></li>
<li><span>create procedure prc_page_result (  </span></li>
<li class="alt"><span>in currpage int,  </span></li>
<li><span>in columns varchar(500),  </span></li>
<li class="alt"><span>in tablename varchar(500),  </span></li>
<li><span>in sCondition varchar(500),  </span></li>
<li class="alt"><span>in order_field varchar(100),  </span></li>
<li><span>in asc_field int,  </span></li>
<li class="alt"><span>in Prima(最完善的虚拟主机管理系统)ry_field varchar(100),  </span></li>
<li><span>in pagesize int  </span></li>
<li class="alt"><span>)  </span></li>
<li><span>begin  </span></li>
<li class="alt"><span>declare sTemp varchar(1000);  </span></li>
<li><span>declare sSql varchar(4000);  </span></li>
<li class="alt"><span>declare sOrder varchar(1000);  </span></li>
<li><span> </span></li>
<li class="alt">
<span>if </span><span class="attribute">asc_field</span><span> = </span><span class="attribute-value">1</span><span> then  </span>
</li>
<li>
<span>set </span><span class="attribute">sOrder</span><span> = </span><span class="attribute-value">concat</span><span>(' order by ', order_field, ' desc ');  </span>
</li>
<li class="alt">
<span>set </span><span class="attribute">sTemp</span><span> = </span><span class="attribute-value">'<span>;  </span></span>
</li>
<li><span>else  </span></li>
<li class="alt">
<span>set </span><span class="attribute">sOrder</span><span> = </span><span class="attribute-value">concat</span><span>(' order by ', order_field, ' asc ');  </span>
</li>
<li>
<span>set </span><span class="attribute">sTemp</span><span> = </span><span class="attribute-value">'>(select max'</span><span>;  </span>
</li>
<li class="alt"><span>end if;  </span></li>
<li><span> </span></li>
<li class="alt">
<span>if </span><span class="attribute">currpage</span><span> = </span><span class="attribute-value">1</span><span> then  </span>
</li>
<li>
<span>if sCondition </span><span class="tag"><span class="tag">></span><span> '' then  </span></span>
</li>
<li class="alt">
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>('select ', columns, ' from ', tablename, ' where ');  </span>
</li>
<li>
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>(sSql, sCondition, sOrder, ' limit ?');  </span>
</li>
<li class="alt"><span>else  </span></li>
<li>
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>('select ', columns, ' from ', tablename, sOrder, ' limit ?');  </span>
</li>
<li class="alt"><span>end if;  </span></li>
<li><span>else  </span></li>
<li class="alt">
<span>if sCondition </span><span class="tag"><span class="tag">></span><span> '' then  </span></span>
</li>
<li>
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>('select ', columns, ' from ', tablename);  </span>
</li>
<li class="alt">
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>(sSql, ' where ', sCondition, ' and ', Prima(最完善的虚拟主机管理系统)ry_field, sTemp);  </span>
</li>
<li>
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>(sSql, '(', Prima(最完善的虚拟主机管理系统)ry_field, ')', ' from (select ');  </span>
</li>
<li class="alt">
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>(sSql, ' ', Prima(最完善的虚拟主机管理系统)ry_field, ' from ', tablename, sOrder);  </span>
</li>
<li>
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>(sSql, ' limit ', (currpage-1)*pagesize, ') as tabtemp)', sOrder);  </span>
</li>
<li class="alt">
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>(sSql, ' limit ?');  </span>
</li>
<li><span>else  </span></li>
<li class="alt">
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>('select ', columns, ' from ', tablename);  </span>
</li>
<li>
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>(sSql, ' where ', Prima(最完善的虚拟主机管理系统)ry_field, sTemp);  </span>
</li>
<li class="alt">
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>(sSql, '(', Prima(最完善的虚拟主机管理系统)ry_field, ')', ' from (select ');  </span>
</li>
<li>
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>(sSql, ' ', Prima(最完善的虚拟主机管理系统)ry_field, ' from ', tablename, sOrder);  </span>
</li>
<li class="alt">
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>(sSql, ' limit ', (currpage-1)*pagesize, ') as tabtemp)', sOrder);  </span>
</li>
<li>
<span>set </span><span class="attribute">sSql</span><span> = </span><span class="attribute-value">concat</span><span>(sSql, ' limit ?');  </span>
</li>
<li class="alt"><span>end if;  </span></li>
<li><span>end if;  </span></li>
<li class="alt">
<span>set @</span><span class="attribute">iPageSize</span><span> = </span><span class="attribute-value">pagesize</span><span>;  </span>
</li>
<li>
<span>set @</span><span class="attribute">sQuery</span><span> = </span><span class="attribute-value">sSql</span><span>;  </span>
</li>
<li class="alt"><span>prepare stmt from @sQuery;  </span></li>
<li><span>execute stmt using @iPageSize;  </span></li>
<li class="alt"><span>end;  </span></li>
<li><span>$$  </span></li>
<li class="alt"><span>delimiter; </span></li>
</ol>
Copy after login

可以存储为数据库脚本,然后用命令导入:

MySQL(和PHP搭配之最佳组合) -u root -p

调用:

<ol class="dp-xml"><li class="alt"><span><span>call prc_page_result(1, "*", "Tablename", "", "columnname", 1, "PKID", 25); </span></span></li></ol>
Copy after login

以上的相关内容就是对MySQL分页查询通用存储过程的介绍,望你能有所收获。


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