先來(lái)看段mysql查詢(xún)文章回復(fù)語(yǔ)句:
代碼如下:
#查詢(xún)文章回復(fù)
------------------------------
--Procedurestructurefor`sp_select_reply_article`
------------------------------
DROPPROCEDUREIFEXISTS`sp_select_reply_article`;
DELIMITER;;
CREATEDEFINER=`root`@`localhost`PROCEDURE`sp_select_reply_article`(IN`ra_id`int,IN`pagefrom`int,IN`pagesize`int)
BEGIN
#Routinebodygoeshere...
SET@ra_id=ra_id;
SET@pagefrom=pagefrom;
SET@pagesize=pagesize;
SET@ssra=CONCAT('SELECT*FROMgk_articleWHEREid=?LIMIT?,?');
PREPAREsqlqueryFROM@ssra;
EXECUTEsqlqueryUSING@ra_id,@pagefrom,@pagesize;
END
DELIMITER;
#技術(shù)點(diǎn)1:MySql5.1不支持LIMIT參數(shù)(MySql5.5就支持了),如果編寫(xiě)存儲(chǔ)過(guò)程時(shí)使用LIMIT做變量,那是需要用動(dòng)態(tài)SQL來(lái)構(gòu)建的,而這樣做性能肯定沒(méi)有靜態(tài)SQL好。主要代碼如下:
代碼如下:
SET@ssra=CONCAT('SELECT*FROMgk_articleWHEREid=?LIMIT?,?');
PREPAREsqlqueryFROM@ssra;
EXECUTEsqlqueryUSING@ra_id,@pagefrom,@pagesize;
#技術(shù)點(diǎn)2:如果同時(shí)需要返回受影響行數(shù)需要在語(yǔ)句后面添加語(yǔ)句:ROW_COUNT()函數(shù),兩條語(yǔ)句之間需要“;”分隔。
代碼如下:
#更新數(shù)據(jù)
------------------------------
--Procedurestructurefor`sp_update_permission`
------------------------------
DROPPROCEDUREIFEXISTS`sp_update_permission`;
DELIMITER;;
CREATEDEFINER=`root`@`localhost`PROCEDURE`sp_update_permission`(IN`puser_uid`varchar(20),IN`plevel`int,IN`ppower`int)
BEGIN
#Routinebodygoeshere...
SET@puser_uid=puser_uid;
SET@plevel=plevel;
SET@ppower=ppower;
UPDATEgk_permissionSET`level`=@plevel,power=@ppowerWHEREuser_uid=CONVERT(@puser_uidUSINGutf8)COLLATEutf8_unicode_ci;
END
DELIMITER;
#技術(shù)點(diǎn)3:MySQL進(jìn)行字符串比較時(shí)發(fā)生錯(cuò)誤(Illegalmixofcollations(utf8_unicode_ci,IMPLICIT)and(utf8_general_ci,IMPLICIT)foroperation'='),解決方法:將比較等式一邊進(jìn)行字符串轉(zhuǎn)換,如改為“CONVERT(b.fullCodeUSINGutf8)COLLATEutf8_unicode_ci”,主要代碼如下:
代碼如下:
UPDATEgk_permissionSET`level`=@plevel,power=@ppowerWHEREuser_uid=CONVERT(@puser_uidUSINGutf8)COLLATEutf8_unicode_ci;
代碼如下:
#插入數(shù)據(jù)
------------------------------
--Procedurestructurefor`sp_insert_user`
------------------------------
DROPPROCEDUREIFEXISTS`sp_insert_user`;
DELIMITER;;
CREATEDEFINER=`root`@`localhost`PROCEDURE`sp_insert_user`(IN`uid`varchar(20),IN`upw`varchar(32),IN`name`varchar(20),IN`sex`int,IN`phone`varchar(20),IN`u_id`int,IN`s_id`int,IN`j_id`int)
BEGIN
#Routinebodygoeshere...
SET@uid=uid;
SET@upw=upw;
SET@uname=uname;
SET@sex=sex;
SET@phone=phone;
#由于外鍵約束,所以添加的外鍵字段需要在對(duì)應(yīng)外鍵所在表有相應(yīng)數(shù)據(jù)
SET@u_id=u_id;
SET@s_id=s_id;
SET@j_id=j_id;
SET@verifytime=DATE('0000-00-00');
INSERTINTOgk_user(uid,upw,uname,sex,phone,u_id,s_id,j_id,verifytime)
VALUES(@uid,@upw,@uname,@sex,@phone,@u_id,@s_id,@j_id,@verifytime);
#查詢(xún)結(jié)果會(huì)自動(dòng)返回受影響行數(shù)
END
DELIMITER;
代碼如下:
#根據(jù)ID刪除數(shù)據(jù)
------------------------------
--Procedurestructurefor`sp_delete_exchange_by_id`
------------------------------
DROPPROCEDUREIFEXISTS`sp_delete_exchange_by_id`;
DELIMITER;;
CREATEDEFINER=`root`@`localhost`PROCEDURE`sp_delete_exchange_by_id`(IN`eid`int)
BEGIN
#Routinebodygoeshere...
SET@eid=eid;
DELETEFROMgk_exchangeWHEREid=@eid;
END
DELIMITER;
代碼如下:
#通過(guò)賬號(hào)查詢(xún)用戶或者管理員
------------------------------
--Procedurestructurefor`sp_select_user_by_uid`
------------------------------
DROPPROCEDUREIFEXISTS`sp_select_user_by_uid`;
DELIMITER;;
CREATEDEFINER=`root`@`localhost`PROCEDURE`sp_select_user_by_uid`(IN`uid`varchar(20),IN`getAdmin`int)
BEGIN
#Routinebodygoeshere...
SET@uid=uid;
#SET@getadmin=getAdmin;
#查詢(xún)管理員
IF(getAdmin=1)THEN
SELECTus.*,un.`name`,se.`name`,jo.`name`,pe.`level`,pe.powerFROMgk_userASus,gk_unitASun,gk_sectionASse,gk_jobtitleASjo,gk_permissionASpeWHEREus.u_id=un.idANDus.s_id=se.idANDus.j_id=jo.idANDus.uid=pe.user_uidANDus.uid=CONVERT(@uidUSINGutf8)COLLATEutf8_unicode_ci;
ENDIF;
#查詢(xún)用戶
IF(getAdmin=0)THEN
SELECTus.*,un.`name`,se.`name`,jo.`name`FROMgk_userASus,gk_unitASun,gk_sectionASse,gk_jobtitleASjoWHEREus.u_id=un.idANDus.s_id=se.idANDus.j_id=jo.idANDus.uid=CONVERT(@uidUSINGutf8)COLLATEutf8_unicode_ci;
ENDIF;
END
DELIMITER;
#技術(shù)點(diǎn)4:這個(gè)存數(shù)過(guò)程需要用到控制語(yǔ)句(ifelseelseifwhilelooprepeatleaveiterate)。
代碼如下:
IF(getAdmin=1)THEN
#語(yǔ)句…
ENDIF;
#技術(shù)點(diǎn)5:在傳入?yún)?shù)不匹配的情況下報(bào)錯(cuò)(Columncountdoesn'tmatchvaluecountatrow1),這個(gè)就是細(xì)心問(wèn)題了,詳細(xì)檢查參數(shù)吧。
#技術(shù)點(diǎn)6:獲取當(dāng)前時(shí)間的函數(shù):NOW()
#技術(shù)點(diǎn)7:“`”這個(gè)符號(hào)是反單引號(hào),兩個(gè)反單引號(hào)夾起來(lái)的會(huì)被當(dāng)做變量,一般是在定義字段時(shí)遇到關(guān)鍵字沖突的時(shí)候會(huì)用到。
更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄