å符串æ串(String)æ¯ç±é¶ä¸ªæå¤ä¸ªå符ç»æçæéåºåãä¸è¬è®°ä¸º s='a1a2•••an'(n>=0)ãå®æ¯ç¼ç¨è¯è¨ä¸è¡¨ç¤ºææ¬çæ°æ®ç±»åã
é常以串çæ´ä½ä½ä¸ºæä½å¯¹è±¡ï¼å¦ï¼å¨ä¸²ä¸æ¥æ¾æ个å串ãæ±åä¸ä¸ªå串ãå¨ä¸²çæ个ä½ç½®ä¸æå
¥ä¸ä¸ªå串以åå é¤ä¸ä¸ªå串çã两个å符串ç¸ççå
è¦æ¡ä»¶æ¯ï¼é¿ 度ç¸çï¼å¹¶ä¸å个对åºä½ç½®ä¸çå符é½ç¸çã设pãqæ¯ä¸¤ä¸ªä¸²ï¼æ±qå¨pä¸é¦æ¬¡åºç°çä½ç½®çè¿ç®å«å模å¼å¹é
ã串ç两ç§æåºæ¬çåå¨æ¹å¼æ¯é¡ºåºåå¨æ¹å¼åé¾ æ¥åå¨æ¹å¼ã
ä¸é¢æ们æ¥ççMySQLä¸çå符串å½æ°
åå¦ç»æçé¿åº¦å¤§äº max_allowed_packet ç³»ç»åéçæ大å¼æ¶ï¼å符串å¼å½æ°çè¿åå¼ä¸ºNULLã
对äºå¨å符串ä½ç½®æä½çå½æ°ï¼ç¬¬ä¸ä¸ªä½ç½®çç¼å·ä¸º 1ã
â ASCII(str)
è¿åå¼ä¸ºå符串str çæå·¦å符çæ°å¼ãåå¦str为空å符串ï¼åè¿åå¼ä¸º 0 ãåå¦str 为NULLï¼åè¿åå¼ä¸º NULLã ASCII()ç¨äºå¸¦æä» 0å°255çæ°å¼çå符ã
mysql>ãSELECTãASCII('2');
->ã50
mysql>ãSELECTãASCII(2);
->ã50
mysql>ãSELECTãASCII('dx');
->ã100
è§ ORD()å½æ°ã
â BIN(N)
è¿åå¼ä¸ºNçäºè¿å¶å¼çå符串表示ï¼å
¶ä¸ã N 为ä¸ä¸ªlonglong (BIGINT) æ°åãè¿çåäº CONV(N,10,2)ãåå¦N 为NULLï¼åè¿åå¼ä¸º NULLã
mysql>ãSELECTãBIN(12);
->ã'1100'
â BIT_LENGTH(str)
è¿åå¼ä¸ºäºè¿å¶çå符串str é¿åº¦ã
mysql>ãSELECTãBIT_LENGTH('text');
->ã32
â CHAR(N,... [USING charset])
CHAR()å°æ¯ä¸ªåæ°Nç解为ä¸ä¸ªæ´æ°ï¼å
¶è¿åå¼ä¸ºä¸ä¸ªå
å«è¿äºæ´æ°ç代ç å¼æç»åºçå符çå符串ãNULLå¼è¢«çç¥ã
mysql>ãSELECTãCHAR(77,121,83,81,'76');
->ã'MySQL'
mysql>ãSELECTãCHAR(77,77.3,'77.3');
->ã'MMM'
å¤§äº 255çCHAR()åæ°è¢«è½¬æ¢ä¸ºå¤ç»æå符ã ä¾å¦ï¼CHAR(256) ç¸å½äº CHAR(1,0), èCHAR(256*256) åç¸å½äº CHAR(1,0,0)ï¼
mysql>ãSELECTãHEX(CHAR(1,0)),ãHEX(CHAR(256));
+----------------+----------------+
|ãHEX(CHAR(1,0))ã|ãHEX(CHAR(256))ã|
+----------------+----------------+
|ã0100ããããããããããã|ã0100ããããããããããã|
+----------------+----------------+
mysql>ãSELECTãHEX(CHAR(1,0,0)),ãHEX(CHAR(256*256));
+------------------+--------------------+
|ãHEX(CHAR(1,0,0))ã|ãHEX(CHAR(256*256))ã|
+------------------+--------------------+
|ã010000ããããããããããã|ã010000ããããããããããããã|
+------------------+--------------------+
CHAR()çè¿åå¼ä¸ºä¸ä¸ªäºè¿å¶å符串ãå¯éæ©ä½¿ç¨USINGè¯å¥äº§çä¸ä¸ªç»åºçå符éä¸çå符串ï¼
mysql>ãSELECTãCHARSET(CHAR(0x65)),ãCHARSET(CHAR(0x65ãUSINGãutf8));
mysql>ãSELECTãCHARSET(CHAR(0x65)),ãCHARSET(CHAR(0x65ãUSINGãutf8));
+---------------------+--------------------------------+
|ãCHARSET(CHAR(0x65))ã|ãCHARSET(CHAR(0x65ãUSINGãutf8))ã|
+---------------------+--------------------------------+
|ãbinaryãããããããããããããã|ãutf8ããããããããããããããããããããããããããã|
+---------------------+--------------------------------+
å¦æ USINGå·²ç»äº§çï¼èç»æå符串ä¸ç¬¦åç»åºçå符éï¼åä¼ååºè¦åãåæ ·ï¼å¦æä¸¥æ ¼çSQL模å¼è¢«æ¿æ´»ï¼åCHAR()çç»æä¼æ为 NULLã
â CHAR_LENGTH(str)
è¿åå¼ä¸ºå符串str çé¿åº¦ï¼é¿åº¦çåä½ä¸ºå符ãä¸ä¸ªå¤åèå符ç®ä½ä¸ä¸ªåå符ã对äºä¸ä¸ªå
å«äºä¸ªäºåèå符é, LENGTH()è¿åå¼ä¸º 10, èCHAR_LENGTH()çè¿åå¼ä¸º5ã
â CHARACTER_LENGTH(str)
CHARACTER_LENGTH()æ¯CHAR_LENGTH()çåä¹è¯ã
â COMPRESS(string_to_compress)
å缩ä¸ä¸ªå符串ãè¿ä¸ªå½æ°è¦æ± MySQLå·²ç»ç¨ä¸ä¸ªè¯¸å¦zlibçå缩åºå缩è¿ããã å¦åï¼è¿åå¼å§ç»æ¯NULLãUNCOMPRESS() å¯å°å缩è¿çå符串è¿è¡è§£å缩ã
mysql>ãSELECTãLENGTH(COMPRESS(REPEAT('a',1000)));
->ã21
mysql>ãSELECTãLENGTH(COMPRESS(''));
->ã0
mysql>ãSELECTãLENGTH(COMPRESS('a'));
->ã13
mysql>ãSELECTãLENGTH(COMPRESS(REPEAT('a',16)));
->ã15
å缩åçå符串çå
容æç
§ä»¥ä¸æ¹å¼åå¨ï¼
空å符串æç
§ç©ºå符串åå¨ã
é空å符串æªå缩å符串çååèé¿åº¦è¿è¡åå¨(é¦å
为ä½åè),åé¢æ¯å缩å符串ãå¦æåç¬¦ä¸²ä»¥ç©ºæ ¼ç»å°¾ï¼å°±ä¼å¨åå ä¸ä¸ª"."å·,以é²æ¢å½ç»æå¼æ¯åå¨ å¨CHARæVARCHARç±»åçå段åæ¶,åºç°èªå¨æç»å°¾ç©ºæ ¼å»æçç°è±¡ã(ä¸æ¨èä½¿ç¨ CHAR æVARCHAR æ¥åå¨å缩å符串ãæ好使ç¨ä¸ä¸ª BLOB å代æ¿ï¼ã
â CONCAT(str1,str2,...)
è¿åç»æ为è¿æ¥åæ°äº§ççå符串ãå¦æä»»ä½ä¸ä¸ªåæ°ä¸ºNULL ï¼åè¿åå¼ä¸º NULLãæ许æä¸ä¸ªæå¤ä¸ªåæ°ãå¦æææåæ°å为éäºè¿å¶å符串ï¼åç»æ为éäºè¿å¶å符串ãå¦æèªåéä¸å«æä»»ä¸äºè¿å¶å符串ï¼åç»æ为ä¸ä¸ªäºè¿å¶å符 串ãä¸ä¸ªæ°ååæ°è¢«è½¬å为ä¸ä¹ç¸ççäºè¿å¶åç¬¦ä¸²æ ¼å¼ï¼è¥è¦é¿å
è¿ç§æ
åµï¼å¯ä½¿ç¨æ¾å¼ç±»å cast, ä¾å¦ï¼ SELECT CONCAT(CAST(int_col AS CHAR), char_col)
mysql>ãSELECTãCONCAT('My',ã'S',ã'QL');
->ã'MySQL'
mysql>ãSELECTãCONCAT('My',ãNULL,ã'QL');
->ãNULL
mysql>ãSELECTãCONCAT(14.3);->ã'14.3'
â CONCAT_WS(separator,str1,str2,...)
CONCAT_WS() 代表 CONCAT With Separator ï¼æ¯CONCAT()çç¹æ®å½¢å¼ããã第ä¸ä¸ªåæ°æ¯å
¶å®åæ°çåé符ãåé符çä½ç½®æ¾å¨è¦è¿æ¥ç两个å符串ä¹é´ãåé符å¯ä»¥æ¯ä¸ä¸ªå符串ï¼ä¹å¯ä»¥æ¯å
¶å®å æ°ãå¦æåé符为 NULLï¼åç»æ为 NULLãå½æ°ä¼å¿½ç¥ä»»ä½åé符åæ°åç NULL å¼ã
mysql>ãSELECTãCONCAT_WS(',','Firstãname','Secondãname','LastãName');
->ã'Firstãname,Secondãname,LastãName'
mysql>ãSELECTãCONCAT_WS(',','Firstãname',NULL,'LastãName');
->ã'Firstãname,LastãName'
CONCAT_WS()ä¸ä¼å¿½ç¥ä»»ä½ç©ºå符串ã (ç¶èä¼å¿½ç¥ææç NULLï¼ã
â CONV(N,from_base,to_base)
ä¸åæ°åºé´è½¬æ¢æ°åãè¿åå¼ä¸ºæ°åçNå符串表示ï¼ç±from_baseåºè½¬å为 to_base åºãå¦æä»»æä¸ä¸ªåæ°ä¸ºNULLï¼åè¿åå¼ä¸º NULLãèªåé N 被ç解为ä¸ä¸ªæ´æ°ï¼ä½æ¯å¯ä»¥è¢«æå®ä¸ºä¸ä¸ªæ´æ°æå符串ãæå°åºæ°ä¸º 2 ï¼èæ大åºæ°å为 36ã If to_base æ¯ä¸ä¸ªè´æ°ï¼å N 被çä½ä¸ä¸ªå¸¦ç¬¦å·æ°ãå¦åï¼ N 被çä½æ 符å·æ°ã CONV() çè¿è¡ç²¾ç¡®åº¦ä¸º 64æ¯ç¹ã
mysql>ãSELECTãCONV('a',16,2);
->ã'1010'
mysql>ãSELECTãCONV('6E',18,8);
->ã'172'
mysql>ãSELECTãCONV(-17,10,-18);
->ã'-H'
mysql>ãSELECTãCONV(10+'10'+'10'+0xa,10,10);
->ã'40'
â ELT(N,str1,str2,str3,...)
è¥N = 1ï¼åè¿åå¼ä¸ºã str1 ï¼è¥N = 2ï¼åè¿åå¼ä¸º str2 ï¼ä»¥æ¤ç±»æ¨ãè¥N å°äº1æ大äºåæ°çæ°ç®ï¼åè¿åå¼ä¸º NULL ã ELT() æ¯ã FIELD()çè¡¥æ°ã
mysql>ãSELECTãELT(1,ã'ej',ã'Heja',ã'hej',ã'foo');
->ã'ej'
mysql>ãSELECTãELT(4,ã'ej',ã'Heja',ã'hej',ã'foo');
->ã'foo'
â EXPORT_SET(bits,on,off[,separator[,number_of_bits]])
è¿åå¼ä¸ºä¸ä¸ªå符串ï¼å
¶ä¸å¯¹äºbitså¼ä¸çæ¯ä¸ªä½ç»ï¼å¯ä»¥å¾å°ä¸ä¸ª on å符串ï¼è对äºæ¯ä¸ªæ¸
é¶æ¯ç¹ä½ï¼å¯ä»¥å¾å°ä¸ä¸ªoff å符串ãbits ä¸çæ¯ç¹å¼æç
§ä»å³å°å·¦ç顺åºæ¥åæ£éª (ç±ä½ä½æ¯ç¹å°é«ä½æ¯ç¹)ãå符串被åéå符串åå¼(é»è®¤ä¸ºéå·â,â)ï¼æç
§ä»å·¦å°å³ç顺åºè¢«æ·»å å°ç»æä¸ãnumber_of_bits ä¼ç»åºè¢«æ£éªçäºè¿å¶ä½æ° (é»è®¤ä¸º 64)ã
mysql>ãSELECTãEXPORT_SET(5,'Y','N',',',4);
->ã'Y,N,Y,N'
mysql>ãSELECTãEXPORT_SET(6,'1','0',',',10);
->ã'0,1,1,0,0,0,0,0,0,0'
â FIELD(str,str1,str2,str3,...)
è¿åå¼ä¸ºstr1, str2, str3,â¦â¦å表ä¸çstr ææ°ãå¨æ¾ä¸å°str çæ
åµä¸ï¼è¿åå¼ä¸º 0 ã
å¦æææ对äºFIELD() çåæ°å为å符串ï¼åææåæ°åæç
§å符串è¿è¡æ¯è¾ãå¦æææçåæ°å为æ°åï¼åæç
§æ°åè¿è¡æ¯è¾ãå¦åï¼åæ°æç
§ååè¿è¡æ¯è¾ã
å¦æstr 为NULLï¼åè¿åå¼ä¸º0 ï¼åå æ¯NULLä¸è½åä»»ä½å¼è¿è¡åçæ¯è¾ãFIELD() æ¯ELT()çè¡¥æ°ã
mysql>ãSELECTãFIELD('ej',ã'Hej',ã'ej',ã'Heja',ã'hej',ã'foo');
->ã2
mysql>ãSELECTãFIELD('fo',ã'Hej',ã'ej',ã'Heja',ã'hej',ã'foo');
->ã0
â FIND_IN_SET(str,strlist)
åå¦å符串str å¨ç±N åé¾ç»æçå符串å表strlist ä¸ï¼ åè¿åå¼çèå´å¨ 1 å° N ä¹é´ãä¸ä¸ªå符串å表就æ¯ä¸ä¸ªç±ä¸äºè¢«â,â符å·åå¼çèªé¾ç»æçå符串ãå¦æ第ä¸ä¸ªåæ°æ¯ä¸ä¸ªå¸¸æ°å符串ï¼è第äºä¸ªæ¯type SETåï¼åãã FIND_IN_SET() å½æ°è¢«ä¼åï¼ä½¿ç¨æ¯ç¹è®¡ç®ãå¦æsträ¸å¨strlist æstrlist 为空å符串ï¼åè¿åå¼ä¸º 0 ãå¦ä»»æä¸ä¸ªåæ°ä¸ºNULLï¼åè¿åå¼ä¸º NULLã è¿ä¸ªå½æ°å¨ç¬¬ä¸ä¸ªåæ°å
å«ä¸ä¸ªéå·(â,â)æ¶å°æ æ³æ£å¸¸è¿è¡ã
mysql>ãSELECTãFIND_IN_SET('b','a,b,c,d');
->ã2
â FORMAT(X,D)
å°number Xè®¾ç½®ä¸ºæ ¼å¼ '#,###,###.##', 以åèäºå
¥çæ¹å¼ä¿çå°å°æ°ç¹åDä½, èè¿åç»æ为ä¸ä¸ªå符串ã
â HEX(N_or_S)
å¦æN_OR_S æ¯ä¸ä¸ªæ°åï¼åè¿åä¸ä¸ª åå
è¿å¶å¼ N çå符串表示ï¼å¨è¿éï¼ãã N æ¯ä¸ä¸ªlonglong (BIGINT)æ°ãè¿ç¸å½äº CONV(N,10,16)ã
å¦æN_OR_S æ¯ä¸ä¸ªå符串ï¼åè¿åå¼ä¸ºä¸ä¸ªN_OR_Sçåå
è¿å¶åç¬¦ä¸²è¡¨ç¤ºï¼ å
¶ä¸æ¯ä¸ªN_OR_S éçæ¯ä¸ªå符被转å为两个åå
è¿å¶æ°åã
mysql>ãSELECTãHEX(255);
->ã'FF'
mysql>ãSELECTã0x616263;
->ã'abc'
mysql>ãSELECTãHEX('abc');
->ã616263
â INSERT(str,pos,len,newstr)
è¿åå符串 str, å
¶åå符串起å§äº pos ä½ç½®åé¿æ被å符串 newstrå代çlen å符ãã å¦æpos è¶
è¿å符串é¿åº¦ï¼åè¿åå¼ä¸ºåå§å符串ãåå¦lençé¿åº¦å¤§äºå
¶å®å符串çé¿åº¦ï¼åä»ä½ç½®poså¼å§æ¿æ¢ãè¥ä»»ä½ä¸ä¸ªåæ°ä¸ºnullï¼åè¿åå¼ä¸º NULLã
mysql>ãSELECTãINSERT('Quadratic',ã3,ã4,ã'What');
->ã'QuWhattic'
mysql>ãSELECTãINSERT('Quadratic',ã-1,ã4,ã'What');
->ã'Quadratic'
mysql>ãSELECTãINSERT('Quadratic',ã3,ã100,ã'What');
->ã'QuWhat'
è¿ä¸ªå½æ°æ¯æå¤åèåå
ã
â INSTR(str,substr)
è¿åå符串 str ä¸åå符串ç第ä¸ä¸ªåºç°ä½ç½®ãè¿åLOCATE()çååæ°å½¢å¼ç¸åï¼é¤éåæ°ç顺åºè¢«é¢ åã
mysql>ãSELECTãINSTR('foobarbar',ã'bar');
->ã4
mysql>ãSELECTãINSTR('xbar',ã'foobar');
->ã0
è¿ä¸ªå½æ°æ¯æå¤åèåå
ï¼å¹¶ä¸åªæå½è³å°æä¸ä¸ªåæ°æ¯äºè¿å¶å符串æ¶åºå大å°åã
â LCASE(str)
LCASE() æ¯ LOWER()çåä¹è¯ãâ LEFT(str,len)
è¿åä»å符串str å¼å§çlen æå·¦å符ã
mysql>ãSELECTãLEFT('foobarbar',ã5);
->ã'fooba'
â LENGTH(str)
è¿åå¼ä¸ºå符串str çé¿åº¦ï¼åä½ä¸ºåèãä¸ä¸ªå¤åèå符ç®ä½å¤åèãè¿æå³ç 对äºä¸ä¸ªå
å«5个2åèå符çåç¬¦ä¸²ï¼ LENGTH() çè¿åå¼ä¸º 10, è CHAR_LENGTH()çè¿åå¼å为5ã
mysql>ãSELECTãLENGTH('text');
->ã4
â LOAD_FILE(file_name)
读åæ件并å°è¿ä¸æ件æç
§å符串çæ ¼å¼è¿åã æ件çä½ç½®å¿
é¡»å¨æå¡å¨ä¸,ä½ å¿
须为æ件å¶å®è·¯å¾å
¨åï¼èä¸ä½ è¿å¿
é¡»æ¥æFILE ç¹è®¸æãæ件å¿
é¡»å¯è¯»åï¼æ件容éå¿
é¡»å°äº max_allowed_packetåèã
è¥æ件ä¸åå¨ï¼æå ä¸æ»¡è¶³ä¸è¿°æ¡ä»¶èä¸è½è¢«è¯»åï¼ åå½æ°è¿åå¼ä¸º NULLã
mysql>ãUPDATEãtbl_name
SETãblob_column=LOAD_FILE('/tmp/picture')
WHEREãid=1;
â LOCATE(substr,str) , LOCATE(substr,str,pos)
第ä¸ä¸ªè¯æ³è¿åå符串 strä¸åå符串substrç第ä¸ä¸ªåºç°ä½ç½®ã第äºä¸ªè¯æ³è¿åå符串 strä¸åå符串substrç第ä¸ä¸ªåºç°ä½ç½®, èµ·å§ä½ç½®å¨posãå¦è¥substr ä¸å¨strä¸ï¼åè¿åå¼ä¸º0ã
mysql>ãSELECTãLOCATE('bar',ã'foobarbar');
->ã4
mysql>ãSELECTãLOCATE('xbar',ã'foobar');
->ã0
mysql>ãSELECTãLOCATE('bar',ã'foobarbar',5);
->ã7
è¿ä¸ªå½æ°æ¯æå¤åèåå
ï¼å¹¶ä¸åªæå½è³å°æä¸ä¸ªåæ°æ¯äºè¿å¶å符串æ¶åºå大å°åã
温馨提示:内容为网友见解,仅供参考