MySql 甯哥敤璇硶鎬荤粨

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySql 甯哥敤璇硶鎬荤粨相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e7%bc%96%e7%a0%81' title='缂栫爜'>缂栫爜   鎿嶄綔   sum   鍛戒护   into   闂撮殧   鎵归噺   浠g爜   not   

1銆佷粈涔堟槸MySQL锛?/p>

銆€銆€MySQL鏄竴绉嶅紑婧愪唬鐮佺殑鍏崇郴鍨嬫暟鎹簱绠$悊绯荤粺锛屽苟鍥犱负鍣ㄩ€熷害銆佸彲闈犳€у拰閫傜敤鎬у鍙椾腑灏忓瀷浼佷笟鐨勯潚鐫愩€?/p>

銆€銆€娉ㄦ剰锛氭暟鎹簱鍛戒护閮芥槸浠ュ垎鍙凤紱缁撳熬鐨勶紝琛ㄧず缁撴潫銆?/p>

銆€銆€Linux绯荤粺涓嬶紝鎬庝箞鐧诲綍MySQL锛?/p>

銆€銆€銆€銆€mysql -u root (鐢ㄦ埛鍚? -p;

銆€銆€銆€銆€杈撳叆瀵嗙爜锛屽嵆鍙櫥褰?/p>

銆€銆€銆€銆€鏄剧ず褰撳墠鐨勬暟鎹簱锛歴how databases;

銆€銆€銆€銆€浣跨敤鏁版嵁搴擄細use 鏁版嵁搴撳悕绉帮紱

銆€銆€銆€銆€鏌ョ湅鏁版嵁琛細show tables锛?/p>

銆€銆€銆€銆€鏄剧ず琛ㄧ殑鍩烘湰灞炴€э細describe  琛ㄥ悕锛?/p>

2銆佸父鐢ㄧ殑瀛楁绫诲瀷鏈夊摢浜涳紵

銆€銆€1锛夋暣褰紙鏁存暟锛夛細

銆€銆€銆€銆€int锛氬彇鍊艰寖鍥达細-2147483648--2147483647

銆€銆€銆€銆€tinyint锛氬彇鍊艰寖鍥达細-128--127

銆€銆€2锛夊瓧绗︿覆锛?/p>

銆€銆€銆€銆€varchar(n)锛氬彲鍙橀暱搴?/p>

銆€銆€銆€銆€char(n)锛氫笉鍙彉闀垮害锛岀浉瀵逛簬varchar鏉ヨ锛宑har鍗犵敤绌洪棿锛屼絾瀹冧篃鏄湁浼樼偣鐨勶紝灏辨槸瀛樺彇閫熷害瑕佷紭浜巚archar銆?/p>

銆€銆€3锛夋棩鏈熸椂闂达細

銆€銆€銆€銆€date锛歽yyy-mm-dd    銆€渚嬪锛?018-10-01

銆€銆€銆€銆€time锛歨h:mm:ss    渚嬪锛?0锛?0锛?5

銆€銆€銆€銆€datetime锛歽yyy-mm-dd hh:mm:ss

銆€銆€4)娴偣鍨嬶細锛坢:浠h〃鎬讳綅鏁帮紝n锛氫唬琛ㄥ皬鏁扮殑浣嶆暟锛?/p>

銆€銆€銆€銆€float锛坢,n锛変緥濡傦細float锛?锛?锛?灏卞彲浠ュ瓨鍌細123.22銆?45.98

銆€銆€銆€銆€double锛坢,n锛夊拰float鐨勫尯鍒槸绮惧害杈冮珮锛屽嵆瀛樺偍鐨勭簿纭害杈冮珮

銆€銆€銆€銆€decimal锛坢,n锛変笁绉嶇被鍨嬩腑锛岀簿搴︽渶楂樸€?/p>

3銆佸浣曞垱寤鸿〃锛?/p>

銆€  (娉ㄦ剰锛歁ySQL涓紝鍐欏畬璇彞鍚庤鍔犲垎鍙凤紱锛岃〃绀虹粨鏉燂紝琛ㄥ悕鍜屽瓧娈靛悕鏈€濂界敤瀛楁瘝鎴栧瓧姣嶅姞鏁板瓧鏉ユ爣璇嗭紝鏈€濂借兘鏄鍚嶇煡鎰忕殑鍗曡瘝)

       鍦烘櫙锛氬垱寤轰竴涓〃锛宨d涓轰富閿紝鍚嶅瓧鐨勭被鍨嬩负varchar,骞朵笖涓嶈兘涓虹┖锛岀紪鐮佹槸锛歶tf8

銆€銆€璇硶锛?create  table  琛ㄥ悕 锛坕d int primary key锛宯ame varchar(10) not null锛屽瓧娈靛悕3锛岀被鍨嬶紝...锛塂EFAULT CHARSET=utf8;

4銆佸浣曟壒閲忔彃鍏ユ暟鎹紵

銆€銆€娉ㄦ剰锛氬瓧娈靛悕鍜屽€兼槸涓€ 涓€瀵瑰簲鐨勩€?/p>

銆€銆€insert   into  琛ㄥ悕 锛堝瓧娈靛悕1锛屽瓧娈靛悕2锛?  values  锛堝€?锛屽€?锛夛紱

5銆佸浣曟洿鏂版暟鎹紵

銆€銆€1锛夋棤鏉′欢鏇存柊

銆€銆€update  琛ㄥ悕  set  瀛楁1=鍊?锛屽瓧娈?=鍊?锛?..锛屽瓧娈礜=鍊糔锛?/p>

銆€銆€2锛夋潯浠舵洿鏂?/p>

銆€銆€update  琛ㄥ悕  set   瀛楁1=鍊?锛?..锛屽瓧娈礜=鍊糔  where  鏉′欢A  and(or) 鏉′欢B and(or) ...锛?/p>

6銆佸浣曞垹闄わ紵锛堟厧鐢級

銆€銆€1锛夋潯浠跺垹闄?/p>

銆€銆€delete from 琛ㄥ悕 where 鏉′欢A and(or) 鏉′欢2,...;

銆€銆€2)鏃犳潯浠跺垹闄?/p>

銆€銆€delete from 琛ㄥ悕                       ------------ 鍒犻櫎鐨勬槸琛ㄧ殑鏁版嵁

銆€銆€drop table  琛ㄥ悕                        -------------鍒犻櫎鏁翠釜琛紝鍖呮嫭琛ㄧ殑缁撴瀯

銆€銆€drop  database   鏁版嵁搴撳悕        ------------鍒犻櫎鏁版嵁搴?/p>

7銆佸浣曟煡璇紵

銆€銆€1锛夋煡璇㈢壒瀹氬瓧娈?/p>

銆€銆€select 瀛楁1锛屽瓧娈?锛?..锛屽瓧娈礜  from  琛ㄥ悕锛?/p>

銆€銆€2锛夋煡瀵绘墍鏈夊瓧娈?/p>

銆€銆€select * from 琛ㄥ悕

銆€銆€3锛夋潯浠舵煡璇?/p>

銆€銆€select  ...  from  琛ㄥ悕  where  鏉′欢A and(or) 鏉′欢B and(or) ...锛?/p>

銆€銆€4锛夊琛ㄦ煡璇紙娉ㄦ剰锛氭煡璇㈢殑瀛楁鍚嶈甯﹁〃鍚嶅姞涓€涓偣锛屾煡璇㈡墠鑳藉璇嗗埆骞舵煡鎵撅級

銆€銆€select  A.瀛楁鍚嶏紝B.瀛楁鍚?nbsp; from 琛ˋ锛岃〃B锛?..锛岃〃N   where  鍏宠仈鏉′欢  and  杩囨护鏉′欢锛?/p>

8銆佸浣曞鏌ヨ鍒扮殑缁撴灉杩涜鎺掑簭锛?------asc鍗囧簭锛堥粯璁わ紝鍙互涓嶅啓锛夛紝desc闄嶅簭

銆€銆€select   瀛楁鍚?  from  琛ㄥ悕  where  鏉′欢  order  by  瀛楁X  asc(desc);

 

9銆佹煡璇㈣鍙ョ粨鏋勶紝鎴栬€呰鍏堝啓浠€涔堬紝鍐嶅啓浠€涔堬紵

銆€銆€select   瀛楁鍚?                         -------------鏌ヨ浠€涔堝瓧娈?/p>

銆€銆€into   鏂拌〃鍚?                             ------------鏌ヨ鐨勬椂鍊欏垱寤轰竴涓柊鐨勮〃

銆€銆€from  琛ㄥ悕                                  ------------鏉ヨ嚜浠€涔堣〃

銆€銆€where        鏉′欢璇彞                  ---------鏉′欢

銆€銆€group by                                    ----------鎸変粈涔堝瓧娈靛垎缁?/p>

銆€銆€having                                       -----------杩囨护鏉′欢

銆€銆€order by                                    -------------鎺掑簭

銆€銆€limit m,n                                   -----------鍒嗛〉(绱㈠紩浠巑寮€濮嬶紝绗竴鏉$储寮曚负0锛岀2鏉$储寮曚负1锛沶琛ㄧず姣忛〉鏄剧ず鐨勬暟鎹潯鏁?

 

10銆佹€庝箞鍒嗙粍锛?---group by

銆€銆€娉ㄦ剰锛歡roup by 蹇呴』鍜岃仛鍚堝嚱鏁扮粨鍚堜娇鐢ㄣ€傚父瑙佽仛鍚堝嚱鏁帮細sum(), count(), avg(), min(), max()绛夈€?/p>

銆€銆€銆€銆€銆€group by鍚庤竟鐨勫垎缁勫瓧娈佃涓巗elect 鍚庤竟鐨勫瓧娈典繚鎸佷竴鑷淬€?/p>

銆€銆€銆€銆€銆€濡傛灉杩囨护鍒嗙粍锛屽垯浣跨敤having+杩囨护鏉′欢鏉ュ畬鎴愩€?/p>

銆€銆€鍦烘櫙锛氭煡璇㈡瘡涓勾绾х殑鎬讳汉鏁板拰鐝骇鏁伴噺

銆€銆€璇硶锛歴elect 骞寸骇锛宻um(浜烘暟) from 琛?group by 骞寸骇锛?/p>

銆€    having 鐩稿綋浜巜here璇彞锛屼絾having鍙互鍖呭惈鑱氬悎鍑芥暟锛屼絾where涓嶅彲浠ャ€?/p>

銆€    having甯稿父涓巊roup by 涓€璧蜂娇鐢ㄣ€?/p>

銆€銆€渚嬪锛歴elect 瀛﹀彿锛宻um(鎴愮哗) from 鎴愮哗琛?group by 瀛﹀彿 having sum锛堟垚缁╋級>600锛?/p>

11銆佹€庝箞鍒嗛〉锛?/p>

銆€銆€select 瀛楁鍚?from 琛ㄥ悕 limit m,n;

12銆乥etween鐨勭敤娉曪紵

銆€銆€select 瀛楁鍚?from 琛ㄥ悕 where 瀛楁鍚?between 鍊? and 鍊?锛?/p>

13銆乮n   not in鐨勭敤娉曪紵------鏌ヨ鐨勭粨鏋滄槸闆嗗悎锛宨n鏄湪浠€涔堥泦鍚堝唴锛宯ot in鍒欑浉鍙嶃€?/p>

銆€銆€select 瀛楁鍚?nbsp; from 琛ㄥ悕 where  鏉′欢瀛楁   in锛堝€?锛屽€?锛?..锛屽€糔锛夛紱

14銆佹ā绯婃煡璇紵

select ... from ... where ... like 鈥?xx鈥?锛?----%鐢ㄦ潵鍖归厤浠绘剰闀垮害鐨勫瓧绗︿覆锛屽彲浠ユ斁浣犳兂鏀剧殑浣嶇疆锛堝墠锛屼腑锛屽悗锛夛紝鏀惧湪鍓嶉潰锛岃鏄庡墠闈㈠彲浠ユ槸浠绘剰瀛楃涓诧紝浣嗙粨灏炬槸xx

select ... from ... where ... like 鈥榑x鈥?   -----_涓€涓笅婊戠嚎鍗犱竴涓綅锛屽彲浠ュ尮閰嶄换鎰忓瓧绗︿覆锛屼絾鍙兘鍖归厤涓€涓紝鍙互鐢ㄦ潵鍖归厤韬唤璇佸彿锛岀數璇濆彿鐮佺瓑銆?/p>

15銆乧ount 锛堬級鐨勭敤娉曪紵------------缁熻

銆€銆€select  count锛?锛塮rom  琛ㄥ悕  where  鏉′欢锛?/p>

16銆佸幓閲嶏紵--------distinct

銆€銆€select distinct 瀛楁鍚?from  琛ㄥ悕锛?/p>

17銆佹暟鍊肩被鍨嬪嚱鏁扮殑鐢ㄦ硶锛?/p>

銆€銆€min() 锛氭渶灏忓€?    max() 锛氭渶澶у€?     avg()锛氬钩鍧囧€?     sum():姹傚拰

銆€銆€璇硶锛?select   min(瀛楁鍚?  from  琛ㄥ悕锛?/p>

 18銆佹棩鏈熺被鍨嬪嚱鏁帮紵

銆€銆€sysdate() :绯荤粺鏃堕棿鍑芥暟 锛?yyyy-mm-dd hh:mm:ss銆?     璇硶锛歴elect sysdate();

銆€銆€curdate() :绯荤粺鏃堕棿鍑芥暟 锛寉yyy-mm-dd銆?   璇硶锛歴elect curdate();

銆€銆€year():鑾峰彇骞翠唤  锛屽锛?018銆?   璇硶锛歴elect year锛坈urdate()锛?    ------------鑾峰彇绯荤粺褰撳墠骞翠唤

銆€銆€month():鑾峰彇鏈堜唤銆?     璇硶锛歴elect month锛坈urdate()锛夛紱  -----------鑾峰彇绯荤粺褰撳墠鏈堜唤

銆€銆€day()锛氳幏鍙栧綋鏈堢鍑犲ぉ銆?nbsp; 璇硶锛歴elect day(curdate());     ------------鑾峰彇绯荤粺褰撳墠鍑犳棩

銆€銆€day_add():澧炲姞涓€涓椂闂撮棿闅斻€?nbsp; 璇硶锛歞ay_add(curdate(),interval 5 day); -----------鍦ㄧ郴缁熷綋鍓嶆棩鏈熶笂鍔?澶?/p>

銆€銆€day_sub():鍑忓幓涓€涓椂闂撮棿闅斻€?nbsp; 璇硶锛歞ay_sub(curdate(),interval 5 day);------------鍦ㄧ郴缁熷綋鍓嶆棩鏈熷噺鍘?澶?/p>

19銆佸瓧绗︿覆鍑芥暟锛?/p>

銆€銆€concat():澶氫釜瀛楁鏁版嵁鎷兼帴銆傝娉曪細select concat(瀛楁1,瀛楁2) from 琛ㄥ悕锛?/p>

銆€銆€substr():鎴彇鏁版嵁鍑芥暟銆俿tr--瑕佹搷浣滅殑瀛楃涓诧紝pos--寮€濮嬫埅鍙栫殑浣嶇疆锛宭en--鎴彇鐨勯暱搴︼紝濡傛湭鎸囧畾锛屽垯鎴彇鍒版渶鍚庝竴浣嶃€?/p>

銆€銆€銆€銆€1锛塻elect substr(str,pos)锛?/p>

銆€銆€銆€銆€2锛塻elect substr(str,pos,len) 锛?nbsp; ---select substr(id,1,2) from student;   浠巗tudent琛ㄤ腑鏌ヨid鐨勫墠涓や綅銆?/p>

銆€銆€length():鑾峰彇涓€涓€肩殑闀垮害锛屼竴鑸敤鍦ㄥ瓧绗︿覆绫诲瀷瀛楁涓婄殑鎯呭喌杈冨銆?/p>

銆€銆€銆€銆€1锛塻elect length锛堚€榟ello鈥橈級;       ----------寰楀埌鐨勭粨鏋滀负5

銆€銆€銆€銆€2锛塻elect length锛坕d锛塮rom student锛?              -----鑾峰彇瀛﹀彿鐨勯暱搴?/p>

20銆佸唴杩炴帴锛?/p>

銆€銆€鍐呰繛鎺ョ殑鐗圭偣锛氬彧杩斿洖婊¤冻鍏宠仈鐨勬潯浠讹紝涓や釜琛ㄩ兘瀛樺湪鐨勬暟鎹€?/p>

銆€銆€select 琛?.瀛楁鍚嶏紝琛?.瀛楁鍚?nbsp; from 琛?锛岃〃2 where 琛?.瀛楁=琛?.瀛楁锛?/p>

銆€銆€鎴栬€咃細

銆€銆€select 琛?.瀛楁鍚嶏紝琛?.瀛楁鍚?from 琛? inner join 琛? on 琛?.瀛楁=琛?.瀛楁锛?/p>

21銆佸閮ㄨ繛鎺ワ紵

銆€銆€娉細鍏堝啓宸﹁〃锛屽悗鍐欏彸琛紝鍖归厤涓嶅埌锛岃嚜鍔ㄨˉnull銆?/p>

銆€銆€1銆佸乏澶栬繛鎺ワ細宸﹁竟鐨勮〃涓嶅姞闄愬埗锛歭eft outer join  鍖归厤涓嶅埌锛屽彸琛ㄤ负null

銆€銆€2銆佸彸澶栬繛鎺ワ細鍙宠竟鐨勮〃涓嶅姞闄愬埗锛歳ight outer join 鍖归厤涓嶅埌锛屽乏琛ㄤ负null

銆€銆€3銆佸叏澶栬繛鎺ワ細鎵€鏈変袱涓〃涓殑琛岄兘浼氬寘鍚湪闆嗗悎涓紝濡傛灉鍖归厤涓嶄笂锛屽湪鍚勮嚜鐨勫搴旂殑浣嶇疆鏄剧ずnull銆?full outer join

銆€銆€鍦烘櫙锛氭樉绀鸿〃1鎵€鏈夌殑琛岋紝涓庡彸琛ㄥ尮閰嶏紝濡傛灉宸﹁〃鐨勮娌℃湁鍖归厤鍒帮紝瀵瑰簲鍙宠〃鐨勮涓簄ull銆?/p>

銆€銆€璇硶锛歴elect 琛?.瀛楁鍚嶏紝琛?.瀛楁鍚?from 琛? left outer join 琛? on 琛?.瀛楁=琛?.瀛楁锛?/p>

22銆佸湪鏌ヨ鐨勫熀纭€涓婂垱寤烘柊琛紵

銆€銆€select 瀛楁鍚?into 鏂拌〃鍚?from 琛ㄥ悕锛?/p>

23銆亀here 瀛楀彞鍙互鐢ㄧ殑鏉′欢锛?/p>

銆€銆€姣旇緝杩愮畻绗︼細=銆?lt;銆?gt;銆?gt;=绛?/p>

銆€銆€閫昏緫杩愮畻绗︼細and銆乷r銆乶ot

銆€銆€鑼冨洿杩愮畻绗︼細between ... and ...銆乶ot between ...and...

銆€銆€鍒楄〃杩愮畻绗︼細in銆乶ot in

銆€銆€瀛楃閫氶厤绗︼細like銆乶otlike 

 

 

銆€銆€

 

以上是关于MySql 甯哥敤璇硶鎬荤粨的主要内容,如果未能解决你的问题,请参考以下文章

abap 甯哥敤璇硶

灏嗘暟鎹粠鍓嶅彴浼犲埌鍚庡彴鏂规硶鎬荤粨

mysql甯哥敤鍩虹鎸囦护澶у叏

MySQL 甯哥敤鍛戒护鍜屽熀纭€璇硶

Linux甯哥敤鍛戒护

涓€瀛﹀氨浼氱殑git鍛戒护