娴呰皥瀛樺偍杩囩▼
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了娴呰皥瀛樺偍杩囩▼相关的知识,希望对你有一定的参考价值。
鏍囩锛?/p>
銆€銆€瀛樺偍杩囩▼绠€鍗曟潵璇达紝灏辨槸涓轰互鍚庣殑浣跨敤鑰屼繚瀛樼殑涓€鏉℃垨澶氭潯MySQL璇彞鐨勯泦鍚堛€傚彲灏嗗叾瑙嗕负鎵逛欢锛岃櫧鐒跺畠浠殑浣滅敤涓嶄粎闄愪簬鎵瑰鐞嗐€?/code>
鍦ㄦ垜鐪嬫潵锛?瀛樺偍杩囩▼灏辨槸鏈変笟鍔¢€昏緫鍜屾祦绋嬬殑闆嗗悎锛?鍙互鍦ㄥ瓨鍌ㄨ繃绋嬩腑鍒涘缓琛紝鏇存柊鏁版嵁锛?鍒犻櫎绛夌瓑銆?/p>
銆€銆€涓轰粈涔堣浣跨敤瀛樺偍杩囩▼?
-
銆€銆€閫氳繃鎶婂鐞嗗皝瑁呭湪瀹规槗浣跨敤鐨勫崟鍏冧腑锛岀畝鍖栧鏉傜殑鎿嶄綔锛堟濡傚墠闈緥瀛愭墍杩帮級銆?/p>
-
銆€銆€鐢变簬涓嶈姹傚弽澶嶅缓绔嬩竴绯诲垪澶勭悊姝ラ锛岃繖淇濊瘉浜嗘暟鎹殑瀹屾暣鎬с€傚鏋滄墍鏈夊紑鍙戜汉鍛樺拰搴旂敤绋嬪簭閮戒娇鐢ㄥ悓涓€锛堣瘯楠屽拰娴嬭瘯锛夊瓨鍌ㄨ繃绋嬶紝鍒欐墍浣跨敤鐨勪唬鐮侀兘鏄浉鍚岀殑銆傝繖涓€鐐圭殑寤朵几灏辨槸闃叉閿欒銆傞渶 瑕佹墽琛岀殑姝ラ瓒婂锛屽嚭閿欑殑鍙兘鎬у氨瓒婂ぇ銆傞槻姝㈤敊璇繚璇佷簡鏁版嵁鐨勪竴鑷存€с€?/p>
-
銆€銆€绠€鍖栧鍙樺姩鐨勭鐞嗐€傚鏋滆〃鍚嶃€佸垪鍚嶆垨涓氬姟閫昏緫锛堟垨鍒殑鍐呭锛夋湁鍙樺寲锛屽彧闇€瑕佹洿鏀瑰瓨鍌ㄨ繃绋嬬殑浠g爜銆備娇鐢ㄥ畠鐨勪汉鍛樼敋鑷充笉闇€瑕佺煡閬撹繖浜涘彉鍖栥€?/p>
-
銆€銆€涓€涓畝鍗曠殑瀛樺偍杩囩▼
create procedure porcedureName () begin select name from user; end;
銆€銆€瀛樺偍杩囩▼鐢╟reate procedure 鍒涘缓锛?涓氬姟閫昏緫鍜宻ql鍐欏湪begin鍜宔nd涔嬮棿銆俶ysql涓彲鐢╟all porcedureName ();鏉ヨ皟鐢ㄨ繃绋嬨€?/p>
-- 璋冪敤杩囩▼ call porcedureName ();
銆€銆€璇ュ瓨鍌ㄨ繃绋嬫病鏈夊弬鏁帮紝 鍙槸鍦ㄨ皟鐢ㄧ殑鏃跺€欐煡璇簡鐢ㄦ埛琛ㄧ殑鐢ㄦ埛鍚嶈€屽凡銆?/p>
銆€銆€鍒犻櫎瀛樺偍杩囩▼
DROP PROCEDURE IF EXISTS porcedureName; -- 娌℃湁鎷彿()
銆€銆€浣跨敤鍙傛暟鐨勫瓨鍌ㄨ繃绋?/strong>
create procedure procedureName( out min decimal(8,2), out avg decimal(8,2), out max decimal(8,2) ) BEGIN select MIN(price) INTO min from order; select AVG(price) into avg from order; select MAX(price) into max from order; END;
銆€銆€姝よ繃绋嬫帴鍙椾笁涓弬鏁帮紝 鍒嗗埆鐢ㄤ簬鑾峰彇璁㈠崟琛ㄧ殑鏈€灏忋€佸钩鍧囥€佹渶澶т环鏍笺€傛瘡涓弬鏁板繀椤诲叿鏈夋寚瀹氱殑绫诲瀷锛岃繖閲屼娇鐢ㄥ崄杩涘埗鍊硷紙decimal(8,2)锛夛紝 鍏抽敭瀛桹UT鎸囧嚭鐩稿簲鐨勫弬鏁扮敤鏉ヤ粠瀛樺偍杩囩▼浼犲嚭涓€涓€硷紙杩斿洖缁欒皟鐢ㄨ€咃級
銆€銆€MySQL鏀寔IN锛堜紶閫掔粰瀛樺偍杩囩▼锛夈€丱UT锛堜粠瀛樺偍杩囩▼浼犲嚭锛屽杩欓噷鎵€鐢級鍜孖NOUT锛堝瀛樺偍杩囩▼浼犲叆鍜屼紶鍑猴級绫诲瀷鐨勫弬鏁般€傚瓨鍌ㄨ繃绋嬬殑浠g爜浣嶄簬BEGIN鍜孍ND璇彞鍐咃紝濡傚墠鎵€瑙侊紝瀹冧滑鏄竴绯诲垪SELECT璇彞锛岀敤鏉ユ绱㈠€硷紝鐒跺悗淇濆瓨鍒扮浉搴旂殑鍙橀噺锛堥€氳繃鎸囧畾INTO鍏抽敭瀛楋級
銆€銆€涓鸿皟鐢ㄦ淇敼杩囩殑瀛樺偍杩囩▼锛屽繀椤绘寚瀹?涓彉閲忓悕锛屽涓嬫墍绀猴細(鎵€鏈塎ySQL鍙橀噺閮藉繀椤讳互@寮€濮嬨€?
-- 鐢变簬杩囩▼鎸囧畾涓変釜鍙傛暟锛?鏁呰皟鐢ㄥ繀椤昏鍙傛暟鍖归厤 call procedureName(@min, @avg, @max);
銆€銆€璇ヨ皟鐢ㄥ苟娌℃湁浠讳綍杈撳嚭锛?鍙槸鎶婅皟鐢ㄧ殑缁撴灉璧嬬粰浜嗚皟鐢ㄦ椂浼犲叆鐨勫彉閲忥紙@min, @avg, @max锛夈€傜劧鍚庡嵆鍙皟鐢ㄦ樉绀鸿鍙橀噺鐨勫€笺€?/p>
select @min, @avg, @max;
銆€銆€浣跨敤 in 鍙傛暟, 杈撳叆涓€涓敤鎴穒d锛?杩斿洖璇ョ敤鎴锋墍鏈夎鍗曠殑鎬讳环鏍笺€?/p>
create procedure getTotalById ( in userId int, out total decimal(8,2) ) BEGIN select SUM(r.price) from order r where r.u_id = userId into total; END;
銆€銆€璋冪敤瀛樺偍杩囩▼
call getTotalById(1, @total); select @total;
銆€銆€缁撴灉灏嗚繑鍥炶鐢ㄦ埛鎵€鏈夎鍗曠殑鍚堣浠锋牸銆?/p>
銆€銆€澶嶆潅涓€鐐圭殑杩囩▼锛?鏍规嵁鐢ㄦ埛id鑾峰彇璇ョ敤鎴风殑鎵€鏈夎鍗曚环鏍硷紝 骞跺姩鎬佺殑閫夋嫨鏄惁鍔犵◣銆備唬鐮佽璁″涓?/p>
create procedure getTotalByUser2( in userId int, in falg boolean, -- 鏄惁鍔犵◣鏍囪 out total decimal(8,2) ) begin DECLARE tmptotal DECIMAL(8,2); DECLARE taxrate int DEFAULT 6;-- 榛樿鐨勫姞绋庣殑鍒╃巼 select SUM(r.price) from order r where r.u_id = userId into tmptotal; if taxable then select tmptotal + (tmptotal/1000*taxrate) into tmptotal; end if; select tmptotal into total; END;
銆€銆€璇ヨ繃绋嬩紶鍏ヤ笁涓弬鏁帮紝 鐢ㄦ埛id锛?鏄惁鍔犵◣浠ュ強杩斿洖鐨勬€讳环鏍硷紝鍦ㄨ繃绋嬪唴閮紝 瀹氫箟涓や釜灞€閮ㄥ彉閲弔mptotal鍜宼axrate锛屾妸鏌ヨ鍑烘潵鐨勭粨鏋滆祴缁欎复鏃跺彉閲忥紝 鍦ㄥ垽鏂槸鍚﹀姞绋庛€傛渶鍚庢妸灞€閮ㄥ彉閲忕殑鍊艰祴缁欒緭鍑哄弬鏁般€?/p>
call getTotalByUser2(1, false, @total); -- 涓嶅姞绋?/span> call getTotalByUser2(1, true, @total); -- 鍔犵◣ select @total;
以上是关于娴呰皥瀛樺偍杩囩▼的主要内容,如果未能解决你的问题,请参考以下文章
娴呰皥 Mac 榛戣嫻鏋?Hackintosh 瀹夎浣跨敤