按条件查询数据
执行语句:call pGiftGetList(2,2,7,'id','desc');
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `pGiftGetList`(pstatus int, pageno int, pagesize int,orderby varchar(20), orderrule varchar(4)) BEGIN declare num int ; declare gpagesize int ; declare gpageno int ; set @gstatus = pstatus;-- 条件 set gpagesize = pagesize;-- 第几页 set gpageno = pageno; -- 第几项 set @gorderby = orderby; -- 排序 set @gorderrule= orderrule; -- 顺序/倒叙 set @sql_text = ' select * from carcare.T_CARCARE_GIFT where 1=1 '; -- 判断条件是否为空 if @gstatus is not null then set @sql_text := concat(@sql_text,' and status = ' ,@gstatus ); SELECT count(*) into @total from carcare.T_CARCARE_GIFT where status = @gstatus; else SELECT count(*) into @total from carcare.T_CARCARE_GIFT; END if; -- 拼接排序语句 set @sql_text := concat(@sql_text, ' order by ',@gorderby ); set @total = @total; set num = (gpageno-1)*gpagesize; -- 判断传过来的页数是否超过数据总数. if @total >= gpageno*gpagesize then set @sql_text := concat(@sql_text, ' limit ',num,',',gpagesize ); else if @total%gpagesize = 0 then set num = ((@total/gpagesize)-1)*gpagesize; else set num = floor(@total/gpagesize)*gpagesize; end if; set @sql_text := concat(@sql_text, ' limit ',num,',',gpagesize ); end if; prepare stmt from @sql_text; -- 预处理需要执行的动态SQL,其中stmt是一个变量 EXECUTE stmt; -- 执行SQL语句 deallocate prepare stmt; END CREATE DEFINER=`root`@`%`
@%是自己有的 不要也可以, 可以改成这样:CREATE PROCEDURE `pGiftGetList`(pstatus int) set @gstatus = pstatus;-- 条件 @变量
不加@ 需要 declare gstatus varchar();这样先声明一下
SELECT count(*) into @total from 中into @total是,就是这个sql执行的结果结果就是@total
添加数据
insert into carcare.T_CARCARE_GIFT (gategory,title ,imgpath,status , stock , giftintro , costscore, changecount,cdt ) values
(1,'提现到支付宝','TXnormal.png',2,100,'提现200',5000,1500,now()); insert into carcare.T_CARCARE_GIFT (gategory,title ,imgpath,status , stock , giftintro , costscore, changecount,cdt ) values (2,'提现到支付宝','TXnormal.png',2,100,'提现200',4000,3800,now()); .....
|
|
来自: mybelievenow > 《数据库》