鐩綍
鍦SQL 璇彞涓紝琛ㄨ揪寮忓彲鐢ㄤ簬涓浜涜濡SELECT璇彞鐨ORDER BY 鎴HAVING瀛愬彞銆SELECT銆 DELETE鎴 UPDATE璇彞鐨WHERE 瀛愬彞鎴 SET璇彞涔嬬被鐨勫湴鏂广備娇鐢ㄦ枃鏈笺column鍊笺NULL鍊笺佸嚱鏁般 鎿嶄綔绗︽潵涔﹀啓琛ㄨ揪寮忋 鏈珷鍙欒堪浜嗗彲鐢ㄤ簬涔﹀啓MySQL琛ㄨ揪寮忕殑鍑芥暟鍜屾搷浣滅銆
闄ら潪鍦ㄦ枃妗g紪鍒朵腑瀵逛竴涓嚱鏁版垨鎿嶄綔绗﹀彟鏈夋寚瀹氱殑鎯呭喌澶栵紝涓涓寘鍚NULL 鐨勮〃杈惧紡閫氬父浜х敓涓涓NULL 鍊笺
娉ㄩ噴锛 鍦ㄩ粯璁ょ姸鎬佷笅, 鍦ㄥ嚱鏁板拰绱ч殢鍏跺悗鐨勬嫭鍙蜂箣闂翠笉寰楀瓨鍦ㄧ┖鏍笺傝繖鑳藉府鍔 MySQL 鍒嗘瀽绋嬪簭鍖哄垎涓浜涘悓鍑芥暟鍚嶇浉鍚岀殑鍑芥暟璋冪敤浠ュ強琛ㄦ垨鍒椼備笉杩囷紝鍑芥暟鑷彉閲忓懆鍥村厑璁告湁绌烘牸鍑虹幇銆
鍙互閫氳繃閫夋嫨--sql-mode=IGNORE_SPACE鏉ユ墦寮MySQL鏈嶅姟鍣ㄧ殑鏂规硶浣挎湇鍔″櫒鎺ュ彈鍑芥暟鍚嶅悗鐨勭┖鏍笺 涓汉瀹㈡埛绔▼搴鍙氳繃閫夋嫨mysql_real_connect()鐨CLIENT_IGNORE_SPACE 瀹炵幇杩欎竴鐘舵併傚湪浠ヤ笂涓ょ鎯呭喌涓紝 鎵鏈夌殑鍑芥暟鍚嶉兘鎴愪负淇濈暀瀛椼傝鍙傝5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊.
涓鸿妭鐪佹椂闂达紝鏈珷涓澶у鏁颁緥瀛愪娇鐢ㄧ畝鍐欏舰寮忓睍绀轰簡 mysql 绋嬪簭鐨勮緭鍑虹粨鏋溿 瀵逛簬浠ヤ笅鏍煎紡鐨勪妇渚嬪睍绀猴細
mysql> SELECT MOD(29,9);
+-----------+
| mod(29,9) |
+-----------+
| 2 |
+-----------+
1 rows in set (0.00 绉)
浣跨敤濡備笅鏍煎紡杩涜浠f浛锛
mysql> SELECT MOD(29,9);
-> 2
浠ヤ笅鍒楄〃鏄剧ず浜嗘搷浣滅浼樺厛绾х殑鐢变綆鍒伴珮鐨勯『搴忋傛帓鍒楀湪鍚屼竴琛岀殑鎿嶄綔绗﹀叿鏈夌浉鍚岀殑浼樺厛绾с
:=
||, OR, XOR
&&, AND
NOT
BETWEEN, CASE, WHEN, THEN, ELSE
=, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
|
&
<<, >>
-, +
*, /, DIV, %, MOD
^
- (涓鍏冨噺鍙), ~ (涓鍏冩瘮鐗瑰弽杞)
!
BINARY, COLLATE
娉ㄩ噴锛鍋囧 HIGH_NOT_PRECEDENCE SQL 妯″紡琚縺娲伙紝鍒 NOT 鐨勪紭鍏堢骇鍚 the ! 鎿嶄綔绗︾浉鍚屻傝鍙傝5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊銆
姣旇緝杩愮畻浜х敓鐨勭粨鏋滀负1(TRUE)銆0 (FALSE)鎴 NULL銆傝繖浜涜繍绠楀彲鐢ㄤ簬鏁板瓧鍜屽瓧绗︿覆銆傛牴鎹渶瑕侊紝瀛楃涓插彲鑷姩杞崲涓烘暟瀛楋紝鑰屾暟瀛椾篃鍙嚜鍔ㄨ浆鎹负瀛楃涓层
鏈珷涓殑涓浜涘嚱鏁 (濡LEAST()鍜GREATEST()) 鐨勬墍寰楀间笉鍖呮嫭 1 (TRUE)銆 0 (FALSE)鍜 NULL銆傜劧鑰岋紝鍏舵墍寰楀间箖鏄熀浜庢寜鐓т笅杩拌鍒欒繍琛岀殑姣旇緝杩愮畻锛
MySQL鎸夌収浠ヤ笅瑙勫垯杩涜鏁板兼瘮杈冿細
鍦ㄩ粯璁ょ姸鎬佷笅锛屽瓧绗︿覆姣旇緝涓嶅尯鍒嗗ぇ灏忓啓锛屽苟浣跨敤鐜版湁瀛楃闆(榛樿涓cp1252 Latin1锛屽悓鏃跺鑻辫涔熼傚悎)銆
涓轰簡杩涜姣旇緝锛屽彲浣跨敤CAST()鍑芥暟灏嗘煇涓艰浆涓哄彟澶栦竴绉嶇被鍨嬨 浣跨敤CONVERT()灏嗗瓧绗︿覆鍊艰浆涓轰笉鍚岀殑瀛楃闆嗐傝鍙傝12.8鑺傦紝鈥淐ast鍑芥暟鍜屾搷浣滅鈥銆
浠ヤ笅渚嬪瓙璇存槑浜嗘瘮杈冭繍绠椾腑灏嗗瓧绗︿覆杞负鏁板瓧鐨勮繃绋嬶細
mysql> SELECT 1 > '6x';
-> 0
mysql> SELECT 7 > '6x';
-> 1
mysql> SELECT 0 > 'x6';
-> 0
mysql> SELECT 0 = 'x6';
-> 1
娉ㄦ剰锛屽湪灏嗕竴涓瓧绗︿覆鍒楀悓涓涓暟瀛楄繘琛屾瘮杈冩椂锛 MySQL 涓嶈兘浣跨敤鍒椾腑鐨勭储寮曡繘琛屽揩閫熸煡鎵俱傚亣濡str_col 鏄竴涓紪鍏ョ储寮曠殑瀛楃涓插垪锛屽垯鍦ㄤ互涓嬭鍙ヤ腑锛岀储寮曚笉鑳芥墽琛屾煡鎵惧姛鑳斤細
SELECT * FROM tbl_name WHERE str_col=1;
鍏跺師鍥犳槸璁稿涓嶅悓鐨勫瓧绗︿覆閮藉彲琚浆鎹负鏁板 1: '1'銆 ' 1'銆 '1a'銆 鈥︹
绛変簬锛
mysql> SELECT 1 = 0;
-> 0
mysql> SELECT '0' = 0;
-> 1
mysql> SELECT '0.0' = 0;
-> 1
mysql> SELECT '0.01' = 0;
-> 0
mysql> SELECT '.01' = 0.01;
-> 1
NULL-safe equal.杩欎釜鎿嶄綔绗﹀拰=鎿嶄綔绗︽墽琛岀浉鍚岀殑姣旇緝鎿嶄綔锛屼笉杩囧湪涓や釜鎿嶄綔鐮佸潎涓NULL鏃讹紝鍏舵墍寰楀间负1鑰屼笉涓NULL锛岃屽綋涓涓搷浣滅爜涓NULL鏃讹紝鍏舵墍寰楀间负0鑰屼笉涓NULL銆
mysql> SELECT 1 <=> 1, NULL <=> NULL, 1 <=> NULL;
-> 1, 1, 0
mysql> SELECT 1 = 1, NULL = NULL, 1 = NULL;
-> 1, NULL, NULL
涓嶇瓑浜庯細
mysql> SELECT '.01' <> '0.01';
-> 1
mysql> SELECT .01 <> '0.01';
-> 0
mysql> SELECT 'zapp' <> 'zappp';
-> 1
灏忎簬鎴栫瓑浜庯細
mysql> SELECT 0.1 <= 2;
-> 1
灏忎簬锛
mysql> SELECT 2 < 2;
-> 0
澶т簬鎴栫瓑浜庯細
mysql> SELECT 2 >= 2;
-> 1
澶т簬锛
mysql> SELECT 2 > 2;
-> 0
鏍规嵁涓涓竷灏斿兼潵妫楠屼竴涓硷紝鍦ㄨ繖閲岋紝甯冨皵鍊煎彲浠ユ槸TRUE銆FALSE鎴UNKNOWN銆
mysql> SELECT 1 IS TRUE, 0 IS FALSE, NULL IS UNKNOWN;
-> 1, 1, 1
mysql> SELECT 1 IS NOT UNKNOWN, 0 IS NOT UNKNOWN, NULL IS NOT UNKNOWN;
-> 1, 1, 0
妫楠屼竴涓兼槸鍚︿负 NULL銆
mysql> SELECT 1 IS NULL, 0 IS NULL, NULL IS NULL;
-> 0, 0, 1
mysql> SELECT 1 IS NOT NULL, 0 IS NOT NULL, NULL IS NOT NULL;
-> 1, 1, 0
涓轰簡鑳藉椤哄埄鐨勪娇鐢ODBC 绋嬪簭宸ヤ綔锛屽湪浣跨敤IS NULL鏃讹紝MySQL鏀寔涓涓嬮澶栫壒鎬э細
o SELECT * FROM tbl_name WHERE auto_col IS NULL
褰撹缃SQL_AUTO_IS_NULL=0鏃讹紝杩欓」鎿嶄綔鏃犳硶杩愯銆傝鍙傝13.5.3鑺傦紝鈥淪ET璇硶鈥銆
o SELECT * FROM tbl_name WHERE date_column IS NULL
杩愯杩欎竴姝ラ渶瑕佷娇鐢ㄤ竴浜 ODBC 搴旂敤杞欢锛屽洜涓 ODBC鏈韩涓嶆敮鎸 涓涓 '0000-00-00'鐨勬椂闂村笺
鍋囧expr澶т簬鎴栫瓑浜 min 涓expr 灏忎簬鎴栫瓑浜max, 鍒BETWEEN 鐨勮繑鍥炲间负1,鎴栨槸0銆傝嫢鎵鏈鍙傛暟閮芥槸鍚屼竴绫诲瀷锛屽垯涓婅堪鍏崇郴鐩稿綋浜庤〃杈惧紡 (min <= expr AND expr <= max)銆傚叾瀹冪被鍨嬬殑杞崲鏍规嵁鏈珷寮绡囨墍杩拌寰嬭繘琛岋紝涓旈傜敤浜3绉鍙傛暟涓换鎰忎竴绉嶃
mysql> SELECT 1 BETWEEN 2 AND 3;
-> 0
mysql> SELECT 'b' BETWEEN 'a' AND 'c';
-> 1
mysql> SELECT 2 BETWEEN 2 AND '3';
-> 1
mysql> SELECT 2 BETWEEN 2 AND 'x-3';
-> 0
杩欑浉褰撲簬NOT(expr BETWEEN min AND max)銆
路 COALESCE(value,...)
杩斿洖鍊间负鍒楄〃褰撲腑鐨勭涓涓潪 NULL鍊硷紝鍦ㄦ病鏈夐潪NULL 鍊煎緱鎯呭喌涓嬭繑鍥炲间负 NULL 銆
mysql> SELECT COALESCE(NULL,1);
-> 1
mysql> SELECT COALESCE(NULL,NULL,NULL);
-> NULL
路 GREATEST(value1,value2,...)
褰撴湁2鎴栧涓鍙傛暟鏃讹紝杩斿洖鍊间负鏈澶(鏈澶у肩殑)鍙傛暟銆傛瘮杈鍙傛暟鎵渚濇嵁鐨勮寰嬪悓LEAST()鐩稿悓銆
mysql> SELECT GREATEST(2,0);
-> 2
mysql> SELECT GREATEST(34.0,3.0,5.0,767.0);
-> 767.0
mysql> SELECT GREATEST('B','A','C');
-> 'C'
鍦ㄦ病鏈夎嚜鍙橀噺涓NULL鐨勬儏鍐典笅锛GREATEST()鐨勮繑鍥炲间负NULL銆
路 expr IN (value,...)
mysql> SELECT 2 IN (0,3,5,'wefwf');
-> 0
mysql> SELECT 'wefwf' IN (0,3,5,'wefwf');
-> 1
IN 鍒楄〃涓墍鍒楀肩殑涓暟浠呭彈闄愪簬 max_allowed_packet 鍊笺
涓轰簡鍚SQL 鏍囧噯鐩镐竴鑷达紝鍦ㄥ乏渚ц〃杈惧紡涓NULL鐨勬儏鍐典笅锛屾垨鏄〃涓壘涓嶅埌鍖归厤椤规垨鏄〃涓竴涓〃杈惧紡涓NULL 鐨勬儏鍐典笅锛IN鐨勮繑鍥炲煎潎涓NULL銆
IN() 璇瀯涔熷彲鐢ㄤ功鍐欐煇浜涚被鍨嬬殑瀛愭煡璇€傝鍙傝13.2.8.3鑺傦紝鈥滀娇鐢ˋNY銆両N鍜孲OME杩涜瀛愭煡璇⑩銆
路 expr NOT IN (value,...)
杩欎笌NOT (expr IN (value,...))鐩稿悓銆
濡expr 涓NULL锛岄偅涔ISNULL() 鐨勮繑鍥炲间负 1锛屽惁鍒欒繑鍥炲间负 0銆
mysql> SELECT ISNULL(1+1);
-> 0
mysql> SELECT ISNULL(1/0);
-> 1
浣跨敤= 鐨NULL 鍊煎姣旈氬父鏄敊璇殑銆
ISNULL() 鍑芥暟鍚 IS NULL姣旇緝鎿嶄綔绗﹀叿鏈変竴浜涚浉鍚岀殑鐗规с傝鍙傝鏈夊叧IS NULL 鐨勮鏄庛
路 INTERVAL(N,N1,N2,N3,...)
鍋囧N < N1锛鍒欒繑鍥炲间负0锛涘亣濡N < N2 绛夌瓑锛屽垯杩斿洖鍊间负1锛涘亣濡N 涓NULL锛屽垯杩斿洖鍊间负 -1 銆傛墍鏈夌殑鍙傛暟鍧囨寜鐓ф暣鏁板鐞嗐備负浜嗚繖涓嚱鏁扮殑姝g‘杩愯锛屽繀椤绘弧瓒 N1 < N2 < N3 < 鈥︹< Nn 銆傚叾鍘熷洜鏄娇鐢ㄤ簡浜屽垎鏌ユ壘(鏋佸揩閫)銆
mysql> SELECT INTERVAL(23, 1, 15, 17, 30, 44, 200);
-> 3
mysql> SELECT INTERVAL(10, 1, 10, 100, 1000);
-> 2
mysql> SELECT INTERVAL(22, 23, 30, 44, 200);
-> 0
路 LEAST(value1,value2,...)
鍦ㄦ湁涓や釜鎴栧涓鍙傛暟鐨勬儏鍐典笅锛 杩斿洖鍊间负鏈灏 (鏈灏忓) 鍙傛暟銆傜敤涓涓嬭鍒欏皢鑷彉閲忚繘琛屽姣旓細
鍋囧浠绘剰涓涓嚜鍙橀噺涓NULL锛屽垯 LEAST()鐨勮繑鍥炲间负NULL 銆
mysql> SELECT LEAST(2,0);
-> 0
mysql> SELECT LEAST(34.0,3.0,5.0,767.0);
-> 3.0
mysql> SELECT LEAST('B','A','C');
-> 'A'
娉ㄦ剰锛屼笂闈㈢殑杞崲瑙勫垯鍦ㄤ竴浜涜竟鐣屾儏褰腑浼氫骇鐢熶竴浜涘鐗圭殑缁撴灉锛
mysql> SELECT CAST(LEAST(3600, 9223372036854775808.0) as SIGNED);
-> -9223372036854775808
鍙戠敓杩欑鎯呭喌鐨勫師鍥犳槸MySQL鍦ㄦ暣鏁拌澧冧腑璇诲彇9223372036854775808.0銆傛暣鏁拌〃绀烘硶涓嶅埄浜庝繚瀛樻暟鍊硷紝鍥犳瀹冨寘鎷竴涓甫绗﹀彿鏁存暟銆
鍦SQL涓紝鎵鏈夐昏緫 鎿嶄綔绗︾殑姹傚兼墍寰楃粨鏋滃潎涓 TRUE銆FALSE鎴 NULL (UNKNOWN)銆傚湪 MySQL涓紝瀹冧滑浣撶幇涓 1 (TRUE)銆 0 (FALSE)鍜 NULL銆傚叾澶у鏁伴兘涓庝笉鍚岀殑鏁版嵁搴SQL閫氱敤锛岀劧鑰屼竴浜涙湇鍔″櫒瀵TRUE鐨勮繑鍥炲煎彲鑳芥槸浠绘剰涓涓潪闆跺笺
閫昏緫 NOT銆傚綋鎿嶄綔鏁颁负0 鏃讹紝鎵寰楀间负 1 锛涘綋鎿嶄綔鏁颁负闈為浂鍊兼椂锛屾墍寰楀间负 0 锛岃屽綋鎿嶄綔鏁颁负NOT NULL鏃讹紝鎵寰楃殑杩斿洖鍊间负 NULL銆
mysql> SELECT NOT 10;
-> 0
mysql> SELECT NOT 0;
-> 1
mysql> SELECT NOT NULL;
-> NULL
mysql> SELECT ! (1+1);
-> 0
mysql> SELECT ! 1+1;
-> 1
鏈鍚庝竴涓緥瀛愪骇鐢熺殑缁撴灉涓 1锛屽師鍥犳槸琛ㄨ揪寮忕殑璁$畻鏂瑰紡鍜(!1)+1鐩稿悓銆
閫昏緫AND銆傚綋鎵鏈夋搷浣滄暟鍧囦负闈為浂鍊笺佸苟涓斾笉涓NULL鏃讹紝璁$畻鎵寰楃粨鏋滀负 1 锛屽綋涓涓垨澶氫釜鎿嶄綔鏁颁负0 鏃讹紝鎵寰楃粨鏋滀负 0 锛屽叾浣欐儏鍐佃繑鍥炲间负 NULL 銆
mysql> SELECT 1 && 1;
-> 1
mysql> SELECT 1 && 0;
-> 0
mysql> SELECT 1 && NULL;
-> NULL
mysql> SELECT 0 && NULL;
-> 0
mysql> SELECT NULL && 0;
-> 0
閫昏緫 OR銆傚綋涓や釜鎿嶄綔鏁板潎涓洪潪 NULL鍊兼椂锛屽鏈変换鎰忎竴涓搷浣滄暟涓洪潪闆跺硷紝鍒欑粨鏋滀负1锛屽惁鍒欑粨鏋滀负0銆傚綋鏈変竴涓搷浣滄暟涓NULL鏃讹紝濡傚彟涓涓搷浣滄暟涓洪潪闆跺硷紝鍒欑粨鏋滀负1锛屽惁鍒欑粨鏋滀负 NULL 銆傚亣濡備袱涓搷浣滄暟鍧囦负 NULL锛屽垯鎵寰楃粨鏋滀负 NULL銆
mysql> SELECT 1 || 1;
-> 1
mysql> SELECT 1 || 0;
-> 1
mysql> SELECT 0 || 0;
-> 0
mysql> SELECT 0 || NULL;
-> NULL
mysql> SELECT 1 || NULL;
-> 1
閫昏緫XOR銆傚綋浠绘剰涓涓搷浣滄暟涓 NULL鏃讹紝杩斿洖鍊间负NULL銆傚浜庨潪 NULL 鐨勬搷浣滄暟锛屽亣濡備竴涓鏁版搷浣滄暟涓洪潪闆跺硷紝鍒欒绠楁墍寰楃粨鏋滀负 1 锛屽惁鍒欎负 0 銆
mysql> SELECT 1 XOR 1;
-> 0
mysql> SELECT 1 XOR 0;
-> 1
mysql> SELECT 1 XOR NULL;
-> NULL
mysql> SELECT 1 XOR 1 XOR 1;
-> 1
a XOR b 鐨勮绠楃瓑鍚屼簬 (a AND (NOT b)) OR ((NOT a)鍜 b)銆
鍚屾牱瑙 12.1.1鑺傦紝鈥滄搷浣滅浼樺厛绾р銆
鍦ㄧ涓涓柟妗堢殑杩斿洖缁撴灉涓紝 value=compare-value銆傝岀浜屼釜鏂规鐨勮繑鍥炵粨鏋滄槸绗竴绉嶆儏鍐电殑鐪熷疄缁撴灉銆傚鏋滄病鏈夊尮閰嶇殑缁撴灉鍊硷紝鍒欒繑鍥炵粨鏋滀负ELSE鍚庣殑缁撴灉锛屽鏋滄病鏈ELSE 閮ㄥ垎锛屽垯杩斿洖鍊间负 NULL銆
mysql> SELECT CASE 1 WHEN 1 THEN 'one'
-> WHEN 2 THEN 'two' ELSE 'more' END;
-> 'one'
mysql> SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;
-> 'true'
mysql> SELECT CASE BINARY 'B'
-> WHEN 'a' THEN 1 WHEN 'b' THEN 2 END;
-> NULL
涓涓CASE琛ㄨ揪寮忕殑榛樿杩斿洖鍊肩被鍨嬫槸浠讳綍杩斿洖鍊肩殑鐩稿闆嗗悎绫诲瀷锛屼絾鍏蜂綋鎯呭喌瑙嗗叾鎵鍦ㄨ澧冭屽畾銆傚鏋滅敤鍦ㄥ瓧绗︿覆璇涓紝鍒欒繑鍥炵粨鏋滃懗瀛楃涓层傚鏋滅敤鍦ㄦ暟瀛楄澧冧腑锛屽垯杩斿洖缁撴灉涓哄崄杩涘埗鍊笺佸疄鍊兼垨鏁存暟鍊笺
濡傛灉 expr1 鏄TRUE (expr1 <> 0 and expr1 <> NULL)锛屽垯 IF()鐨勮繑鍥炲间负expr2; 鍚﹀垯杩斿洖鍊煎垯涓 expr3銆IF() 鐨勮繑鍥炲间负鏁板瓧鍊兼垨瀛楃涓插硷紝鍏蜂綋鎯呭喌瑙嗗叾鎵鍦ㄨ澧冭屽畾銆
mysql> SELECT IF(1>2,2,3);
-> 3
mysql> SELECT IF(1<2,'yes ','no');
-> 'yes'
mysql> SELECT IF(STRCMP('test','test1'),'no','yes');
-> 'no'
濡傛灉expr2 鎴expr3涓彧鏈変竴涓槑纭槸 NULL锛屽垯IF() 鍑芥暟鐨勭粨鏋滅被鍨 涓洪潪NULL琛ㄨ揪寮忕殑缁撴灉绫诲瀷銆
expr1 浣滀负涓涓暣鏁板艰繘琛岃绠楋紝灏辨槸璇达紝鍋囧浣犳鍦ㄩ獙璇佹诞鐐瑰兼垨瀛楃涓插硷紝 閭d箞搴旇浣跨敤姣旇緝杩愮畻杩涜妫楠屻
mysql> SELECT IF(0.1,1,0);
-> 0
mysql> SELECT IF(0.1<>0,1,0);
-> 1
鍦ㄦ墍绀虹殑绗竴涓緥瀛愪腑锛IF(0.1)鐨勮繑鍥炲间负0锛屽師鍥犳槸 0.1 琚浆鍖栦负鏁存暟鍊硷紝浠庤屽紩璧蜂竴涓 IF(0)鐨勬楠屻傝繖鎴栬涓嶆槸浣犳兂瑕佺殑鎯呭喌銆傚湪绗簩涓緥瀛愪腑锛屾瘮杈冩楠屼簡鍘熷娴偣鍊硷紝鐩殑鏄负浜嗕簡瑙f槸鍚﹀叾涓洪潪闆跺笺傛瘮杈冪粨鏋滀娇鐢ㄦ暣鏁般
IF() (杩欎竴鐐瑰湪鍏惰鍌ㄥ瓨鍒颁复鏃惰〃鏃跺緢閲嶈 ) 鐨勯粯璁よ繑鍥炲肩被鍨嬫寜鐓т互涓嬫柟寮忚绠楋細
琛ㄨ揪寮 |
杩斿洖鍊 |
expr2 鎴expr3 杩斿洖鍊间负涓涓瓧绗︿覆銆 |
瀛楃涓 |
expr2 鎴expr3 杩斿洖鍊间负涓涓诞鐐瑰笺 |
娴偣 |
expr2 鎴 expr3 杩斿洖鍊间负涓涓暣鏁般 |
鏁存暟 |
鍋囧expr2 鍜expr3 閮芥槸瀛楃涓诧紝涓斿叾涓换浣曚竴涓瓧绗︿覆鍖哄垎澶у皬鍐欙紝鍒欒繑鍥炵粨鏋滄槸鍖哄垎澶у皬鍐欍
鍋囧expr1 涓嶄负 NULL锛屽垯 IFNULL() 鐨勮繑鍥炲间负 expr1; 鍚﹀垯鍏惰繑鍥炲间负 expr2銆IFNULL()鐨勮繑鍥炲兼槸鏁板瓧鎴栨槸瀛楃涓诧紝鍏蜂綋鎯呭喌鍙栧喅浜庡叾鎵浣跨敤鐨勮澧冦
mysql> SELECT IFNULL(1,0);
-> 1
mysql> SELECT IFNULL(NULL,10);
-> 10
mysql> SELECT IFNULL(1/0,10);
-> 10
mysql> SELECT IFNULL(1/0,'yes');
-> 'yes'
IFNULL(expr1,expr2)鐨勯粯璁ょ粨鏋滃间负涓や釜琛ㄨ揪寮忎腑鏇村姞鈥滈氱敤鈥濈殑涓涓紝椤哄簭涓STRING銆 REAL鎴 INTEGER銆傚亣璁句竴涓熀浜庤〃杈惧紡鐨勮〃鐨勬儏鍐碉紝 鎴MySQL蹇呴』鍦ㄥ唴瀛樺偍鍣ㄤ腑鍌ㄥ瓨涓涓复鏃惰〃涓IFNULL()鐨勮繑鍥炲硷細
CREATE TABLE tmp SELECT IFNULL(1,'test') AS test锛
鍦ㄨ繖涓緥瀛愪腑锛屾祴璇曞垪鐨勭被鍨嬩负 CHAR(4)銆
濡傛灉expr1 = expr2 鎴愮珛锛岄偅涔堣繑鍥炲间负NULL锛屽惁鍒欒繑鍥炲间负 expr1銆傝繖鍜CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END鐩稿悓銆
mysql> SELECT NULLIF(1,1);
-> NULL
mysql> SELECT NULLIF(1,2);
-> 1
娉ㄦ剰锛屽鏋滃弬鏁颁笉鐩哥瓑锛屽垯 MySQL 涓ゆ姹傚緱鐨勫间负 expr1 銆
鍋囧缁撴灉鐨勯暱搴﹀ぇ浜 max_allowed_packet 绯荤粺鍙橀噺鐨勬渶澶у兼椂锛屽瓧绗︿覆鍊煎嚱鏁扮殑杩斿洖鍊间负NULL銆傝鍙傝7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥銆
瀵逛簬鍦ㄥ瓧绗︿覆浣嶇疆鎿嶄綔鐨勫嚱鏁帮紝绗竴涓綅缃殑缂栧彿涓 1銆
杩斿洖鍊间负瀛楃涓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()鍑芥暟銆
杩斿洖鍊间负N鐨勪簩杩涘埗鍊肩殑瀛楃涓茶〃绀猴紝鍏朵腑 N 涓轰竴涓longlong (BIGINT) 鏁板瓧銆傝繖绛夊悓浜 CONV(N,10,2)銆傚亣濡N 涓NULL锛屽垯杩斿洖鍊间负 NULL銆
mysql> SELECT BIN(12);
-> '1100'
杩斿洖鍊间负浜岃繘鍒剁殑瀛楃涓str 闀垮害銆
mysql> SELECT BIT_LENGTH('text');
-> 32
CHAR()灏嗘瘡涓弬鏁N鐞嗚В涓轰竴涓暣鏁帮紝鍏惰繑鍥炲间负涓涓寘鍚繖浜涙暣鏁扮殑浠g爜鍊兼墍缁欏嚭鐨勫瓧绗︾殑瀛楃涓层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銆
杩斿洖鍊间负瀛楃涓str 鐨勯暱搴︼紝闀垮害鐨勫崟浣嶄负瀛楃銆備竴涓瀛楄妭瀛楃绠椾綔涓涓崟瀛楃銆瀵逛簬涓涓鍖呭惈浜斾釜浜屽瓧鑺傚瓧绗﹂泦, LENGTH()杩斿洖鍊间负 10, 鑰CHAR_LENGTH()鐨勮繑鍥炲间负5銆
CHARACTER_LENGTH()鏄CHAR_LENGTH()鐨勫悓涔夎瘝銆
鍘嬬缉涓涓瓧绗︿覆銆傝繖涓嚱鏁拌姹 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
鍘嬬缉鍚庣殑瀛楃涓茬殑鍐呭鎸夌収浠ヤ笅鏂瑰紡瀛樺偍锛
杩斿洖缁撴灉涓鸿繛鎺ュ弬鏁颁骇鐢熺殑瀛楃涓层傚鏈変换浣曚竴涓弬鏁颁负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() 浠h〃 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锛銆
涓嶅悓鏁板熀闂磋浆鎹㈡暟瀛椼傝繑鍥炲间负鏁板瓧鐨N瀛楃涓茶〃绀猴紝鐢from_base鍩鸿浆鍖栦负 to_base 鍩恒傚鏈変换鎰忎竴涓弬鏁颁负NULL锛屽垯杩斿洖鍊间负 NULL銆傝嚜鍙橀噺 N 琚悊瑙d负涓涓暣鏁帮紝浣嗘槸鍙互琚寚瀹氫负涓涓暣鏁版垨瀛楃涓层傛渶灏忓熀鏁颁负 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'
鑻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'
杩斿洖鍊间负涓涓瓧绗︿覆锛屽叾涓浜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'
杩斿洖鍊间负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
鍋囧瀛楃涓str 鍦ㄧ敱N 瀛愰摼缁勬垚鐨勫瓧绗︿覆鍒楄〃strlist 涓紝 鍒欒繑鍥炲肩殑鑼冨洿鍦 1 鍒 N 涔嬮棿 銆備竴涓瓧绗︿覆鍒楄〃灏辨槸涓涓敱涓浜涜鈥,鈥欑鍙峰垎寮鐨勮嚜閾剧粍鎴愮殑瀛楃涓层傚鏋滅涓涓弬鏁版槸涓涓父鏁板瓧绗︿覆锛岃岀浜屼釜鏄type SET鍒楋紝鍒 FIND_IN_SET() 鍑芥暟琚紭鍖栵紝浣跨敤姣旂壒璁$畻銆傚鏋str涓嶅湪strlist 鎴strlist 涓虹┖瀛楃涓诧紝鍒欒繑鍥炲间负 0 銆傚浠绘剰涓涓弬鏁颁负NULL锛屽垯杩斿洖鍊间负 NULL銆 杩欎釜鍑芥暟鍦ㄧ涓涓弬鏁板寘鍚竴涓楀彿(鈥,鈥)鏃跺皢鏃犳硶姝e父杩愯銆
mysql> SELECT FIND_IN_SET('b','a,b,c,d');
-> 2
灏number X璁剧疆涓烘牸寮 '#,###,###.##', 浠ュ洓鑸嶄簲鍏ョ殑鏂瑰紡淇濈暀鍒板皬鏁扮偣鍚D浣, 鑰岃繑鍥炵粨鏋滀负涓涓瓧绗︿覆銆傝瑙12.9.4鑺傦紝鈥滃叾浠栧嚱鏁扳.
濡傛灉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
杩斿洖瀛楃涓 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'
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏銆
杩斿洖瀛楃涓 str 涓瓙瀛楃涓茬殑绗竴涓嚭鐜颁綅缃傝繖鍜LOCATE()鐨勫弻鍙傛暟褰㈠紡鐩稿悓锛岄櫎闈炲弬鏁扮殑椤哄簭琚鍊掋
mysql> SELECT INSTR('foobarbar', 'bar');
-> 4
mysql> SELECT INSTR('xbar', 'foobar');
-> 0
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冿紝骞朵笖鍙湁褰撹嚦灏戞湁涓涓弬鏁版槸浜岃繘鍒跺瓧绗︿覆鏃跺尯鍒嗗ぇ灏忓啓銆
LCASE() 鏄 LOWER()鐨勫悓涔夎瘝銆
杩斿洖浠庡瓧绗︿覆str 寮濮嬬殑len 鏈宸﹀瓧绗︺
mysql> SELECT LEFT('foobarbar', 5);
-> 'fooba'
杩斿洖鍊间负瀛楃涓str 鐨勯暱搴︼紝鍗曚綅涓哄瓧鑺傘備竴涓瀛楄妭瀛楃绠椾綔澶氬瓧鑺傘傝繖鎰忓懗鐫 瀵逛簬涓涓寘鍚5涓2瀛楄妭瀛楃鐨勫瓧绗︿覆锛 LENGTH() 鐨勮繑鍥炲间负 10, 鑰 CHAR_LENGTH()鐨勮繑鍥炲煎垯涓5銆
mysql> SELECT LENGTH('text');
-> 4
璇诲彇鏂囦欢骞跺皢杩欎竴鏂囦欢鎸夌収瀛楃涓茬殑鏍煎紡杩斿洖銆 鏂囦欢鐨勪綅缃繀椤诲湪鏈嶅姟鍣ㄤ笂,浣犲繀椤讳负鏂囦欢鍒跺畾璺緞鍏ㄥ悕锛岃屼笖浣犺繕蹇呴』鎷ユ湁FILE 鐗硅鏉冦傛枃浠跺繀椤诲彲璇诲彇锛屾枃浠跺閲忓繀椤诲皬浜 max_allowed_packet瀛楄妭銆
鑻ユ枃浠朵笉瀛樺湪锛屾垨鍥犱笉婊¤冻涓婅堪鏉′欢鑰屼笉鑳借璇诲彇锛 鍒欏嚱鏁拌繑鍥炲间负 NULL銆
mysql> UPDATE tbl_name
SET blob_column=LOAD_FILE('/tmp/picture')
WHERE id=1;
绗竴涓娉曡繑鍥炲瓧绗︿覆 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
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冿紝骞朵笖鍙湁褰撹嚦灏戞湁涓涓弬鏁版槸浜岃繘鍒跺瓧绗︿覆鏃跺尯鍒嗗ぇ灏忓啓銆
杩斿洖瀛楃涓 str 浠ュ強鎵鏈夋牴鎹渶鏂扮殑瀛楃闆嗘槧灏勮〃鍙樹负灏忓啓瀛楁瘝鐨勫瓧绗 (榛樿涓 cp1252 Latin1)銆
mysql> SELECT LOWER('QUADRATICALLY');
-> 'quadratically'
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦
杩斿洖瀛楃涓 str, 鍏跺乏杈圭敱瀛楃涓padstr 濉ˉ鍒len 瀛楃闀垮害銆傚亣濡str 鐨勯暱搴﹀ぇ浜len, 鍒欒繑鍥炲艰缂╃煭鑷 len 瀛楃銆
mysql> SELECT LPAD('hi',4,'??');
-> '??hi'
mysql> SELECT LPAD('hi',1,'??');
-> 'h'
杩斿洖瀛楃涓 str 锛屽叾寮曞绌烘牸瀛楃琚垹闄ゃ
mysql> SELECT LTRIM(' barbar');
-> 'barbar'
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦
杩斿洖涓涓瀹氬 (涓涓寘鍚鈥,鈥欏彿鍒嗗紑鐨勫瓧瀛楃涓茬殑瀛楃涓) 锛岀敱鍦bits 缁勪腑鍏锋湁鐩稿簲鐨勬瘮鐗圭殑瀛楃涓茬粍鎴愩str1 瀵瑰簲姣旂壒 0, str2 瀵瑰簲姣旂壒1,浠ユ绫绘帹銆str1, str2, ...涓殑 NULL鍊间笉浼氳娣诲姞鍒扮粨鏋滀腑銆
mysql> SELECT MAKE_SET(1,'a','b','c');
-> 'a'
mysql> SELECT MAKE_SET(1 | 4,'hello','nice','world');
-> 'hello,world'
mysql> SELECT MAKE_SET(1 | 4,'hello','nice',NULL,'world');
-> 'hello'
mysql> SELECT MAKE_SET(0,'a','b','c');
-> ''
MID(str,pos,len) 鏄 SUBSTRING(str,pos,len)鐨勫悓涔夎瘝銆
杩斿洖涓涓 N鐨勫叓杩涘埗鍊肩殑瀛楃涓茶〃绀猴紝鍏朵腑 N 鏄竴涓longlong (BIGINT)鏁般傝繖绛夊悓浜CONV(N,10,8)銆傝嫢N 涓 NULL 锛屽垯杩斿洖鍊间负NULL銆
mysql> SELECT OCT(12);
-> '14'
OCTET_LENGTH() 鏄 LENGTH()鐨勫悓涔夎瘝銆
鑻ュ瓧绗︿覆str 鐨勬渶宸﹀瓧绗︽槸涓涓瀛楄妭瀛楃锛屽垯杩斿洖璇ュ瓧绗︾殑浠g爜锛 浠g爜鐨勮绠楅氳繃浣跨敤浠ヤ笅鍏紡璁$畻鍏剁粍鎴愬瓧鑺傜殑鏁板艰屽緱鍑:
(1st byte code)
+ (2nd byte code 脳 256)
+ (3rd byte code 脳 2562) ...
鍋囧鏈宸﹀瓧绗︿笉鏄竴涓瀛楄妭瀛楃锛岄偅涔 ORD()鍜屽嚱鏁ASCII()杩斿洖鐩稿悓鐨勫笺
mysql> SELECT ORD('2');
-> 50
POSITION(substr IN str)鏄 LOCATE(substr,str)鍚屼箟璇嶃
寮曡瘉涓涓瓧绗︿覆锛岀敱姝や骇鐢熶竴涓湪SQL璇彞涓彲鐢ㄤ綔瀹屽叏杞箟鏁版嵁鍊肩殑缁撴灉銆 杩斿洖鐨勫瓧绗︿覆鐢卞崟寮曞彿鏍囨敞锛屾瘡渚嬮兘甯︽湁鍗曞紩鍙 (鈥'鈥)銆 鍙嶆枩绾跨鍙 (鈥\鈥)銆 ASCII NUL浠ュ強鍓嶉潰鏈夊弽鏂滅嚎绗﹀彿鐨Control-Z 銆傚鏋滆嚜鍙橀噺鐨勫间负NULL, 鍒欒繑鍥炰笉甯﹀崟寮曞彿鐨勫崟璇 鈥NULL鈥濄
mysql> SELECT QUOTE('Don\'t!');
-> 'Don\'t!'
mysql> SELECT QUOTE(NULL);
-> NULL
杩斿洖涓涓敱閲嶅鐨勫瓧绗︿覆str 缁勬垚鐨勫瓧绗︿覆锛屽瓧绗︿覆str鐨勬暟鐩瓑浜count 銆 鑻 count <= 0,鍒欒繑鍥炰竴涓┖瀛楃涓层傝嫢str 鎴 count 涓 NULL锛屽垯杩斿洖 NULL 銆
mysql> SELECT REPEAT('MySQL', 3);
-> 'MySQLMySQLMySQL'
杩斿洖瀛楃涓str 浠ュ強鎵鏈夎瀛楃涓to_str鏇夸唬鐨勫瓧绗︿覆from_str 銆
mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
-> 'WwWwWw.mysql.com'
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦
杩斿洖瀛楃涓 str 锛岄『搴忓拰瀛楃椤哄簭鐩稿弽銆
mysql> SELECT REVERSE('abc');
-> 'cba'
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦
浠庡瓧绗︿覆str 寮濮嬶紝杩斿洖鏈鍙len 瀛楃銆
mysql> SELECT RIGHT('foobarbar', 4);
-> 'rbar'
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦
杩斿洖瀛楃涓str, 鍏跺彸杈硅瀛楃涓 padstr濉ˉ鑷len 瀛楃闀垮害銆傚亣濡傚瓧绗︿覆str 鐨勯暱搴﹀ぇ浜 len,鍒欒繑鍥炲艰缂╃煭鍒颁笌 len 瀛楃鐩稿悓闀垮害銆
mysql> SELECT RPAD('hi',5,'?');
-> 'hi???'
mysql> SELECT RPAD('hi',1,'?');
-> 'h'
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦
杩斿洖瀛楃涓 str 锛岀粨灏剧┖鏍煎瓧绗﹁鍒犲幓銆
mysql> SELECT RTRIM('barbar ');
-> 'barbar'
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦
浠str杩斿洖涓涓soundex瀛楃涓层 涓や釜鍏锋湁鍑犱箮鍚屾牱鎺㈡祴鐨勫瓧绗︿覆搴旇鍏锋湁鍚屾牱鐨 soundex 瀛楃涓层備竴涓爣鍑嗙殑soundex 瀛楃涓茬殑闀垮害涓4涓瓧绗︼紝鐒惰SOUNDEX() 鍑芥暟浼氳繑鍥炰竴涓汉浠ラ暱搴︾殑瀛楃涓层 鍙娇鐢ㄧ粨鏋滀腑鐨SUBSTRING() 鏉ュ緱鍒颁竴涓爣鍑 soundex 瀛楃涓层傚湪str涓锛浼氬拷鐣ユ墍鏈夋湭鎸夌収瀛楁瘝椤哄簭鎺掑垪鐨勫瓧绗︺ 鎵鏈変笉鍦A-Z鑼冨洿涔嬪唴鐨勫浗闄呭瓧姣嶇鍙疯瑙嗕负鍏冮煶瀛楁瘝銆
mysql> SELECT SOUNDEX('Hello');
-> 'H400'
mysql> SELECT SOUNDEX('Quadratically');
-> 'Q36324'
娉ㄦ剰锛杩欎釜鍑芥暟鎵ц鍘熷鐨Soundex绠楁硶锛岃岄潪鏇村姞娴佽鐨勫姞寮虹増鏈(濡D. Knuth鎵杩)銆傚叾鍖哄埆鍦ㄤ簬鍘熷鐗堟湰棣栧厛浼氬垹鍘诲厓闊筹紝鍏舵鏄噸澶嶏紝鑰屽姞寮虹増鍒欓鍏堝垹鍘婚噸澶嶏紝鑰屽悗鍒犲幓鍏冮煶銆
杩欑浉褰撲簬SOUNDEX(expr1) = SOUNDEX(expr2)銆
杩斿洖涓涓敱N 闂撮殧绗﹀彿缁勬垚鐨勫瓧绗︿覆銆
mysql> SELECT SPACE(6);
-> ' '
涓嶅甫鏈len 鍙傛暟鐨勬牸寮忎粠瀛楃涓str杩斿洖涓涓瓙瀛楃涓诧紝璧峰浜庝綅缃 pos銆傚甫鏈len鍙傛暟鐨勬牸寮忎粠瀛楃涓str杩斿洖涓涓暱搴﹀悓len瀛楃鐩稿悓鐨勫瓙瀛楃涓诧紝璧峰浜庝綅缃 pos銆 浣跨敤 FROM鐨勬牸寮忎负鏍囧噯 SQL 璇硶銆備篃鍙兘瀵pos浣跨敤涓涓礋鍊笺傚亣鑻ヨ繖鏍凤紝鍒欏瓙瀛楃涓茬殑浣嶇疆璧峰浜庡瓧绗︿覆缁撳熬鐨pos 瀛楃锛岃屼笉鏄瓧绗︿覆鐨勫紑澶翠綅缃傚湪浠ヤ笅鏍煎紡鐨勫嚱鏁颁腑鍙互瀵pos 浣跨敤涓涓礋鍊笺
mysql> SELECT SUBSTRING('Quadratically',5);
-> 'ratically'
mysql> SELECT SUBSTRING('foobarbar' FROM 4);
-> 'barbar'
mysql> SELECT SUBSTRING('Quadratically',5,6);
-> 'ratica'
mysql> SELECT SUBSTRING('Sakila', -3);
-> 'ila'
mysql> SELECT SUBSTRING('Sakila', -5, 3);
-> 'aki'
mysql> SELECT SUBSTRING('Sakila' FROM -4 FOR 2);
-> 'ki'
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦
娉ㄦ剰锛屽鏋滃len浣跨敤鐨勬槸涓涓皬浜1鐨勫硷紝鍒欑粨鏋滃缁堜负绌哄瓧绗︿覆銆
SUBSTR()鏄 SUBSTRING()鐨勫悓涔夎瘝銆
鍦ㄥ畾鐣岀 delim 浠ュ強count 鍑虹幇鍓嶏紝浠庡瓧绗︿覆str杩斿洖鑷瓧绗︿覆銆傝嫢count涓烘鍊,鍒欒繑鍥炴渶缁堝畾鐣岀(浠庡乏杈瑰紑濮)宸﹁竟鐨勪竴鍒囧唴瀹广傝嫢count涓鸿礋鍊硷紝鍒欒繑鍥炲畾鐣岀锛堜粠鍙宠竟寮濮嬶級鍙宠竟鐨勪竴鍒囧唴瀹广
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
-> 'www.mysql'
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);
-> 'mysql.com'
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦
杩斿洖瀛楃涓 str 锛 鍏朵腑鎵鏈remstr 鍓嶇紑鍜/鎴栧悗缂閮藉凡琚垹闄ゃ傝嫢鍒嗙被绗BOTH銆LEADIN鎴TRAILING涓病鏈変竴涓槸缁欏畾鐨,鍒欏亣璁句负BOTH 銆 remstr 涓哄彲閫夐」锛屽湪鏈寚瀹氭儏鍐典笅锛屽彲鍒犻櫎绌烘牸銆
mysql> SELECT TRIM(' bar ');
-> 'bar'
mysql> SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx');
-> 'barxxx'
mysql> SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx');
-> 'bar'
mysql> SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');
-> 'barx'
杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦
UCASE()鏄UPPER()鐨勫悓涔夎瘝銆
瀵圭粡COMPRESS()鍑芥暟鍘嬬缉鍚庣殑瀛楃涓茶繘琛岃В鍘嬬缉銆傝嫢鍙傛暟涓哄帇缂╁硷紝鍒欑粨鏋滀负 NULL銆傝繖涓嚱鏁拌姹 MySQL 宸茶璇稿zlib 涔嬬被鐨勫帇缂╁簱缂栬瘧杩囥傚惁鍒, 杩斿洖鍊煎皢濮嬬粓鏄 NULL銆
mysql> SELECT UNCOMPRESS(COMPRESS('any string'));
-> 'any string'
mysql> SELECT UNCOMPRESS('any string');
-> NULL
杩斿洖鍘嬬缉瀛楃涓插帇缂╁墠鐨勯暱搴︺
mysql> SELECT UNCOMPRESSED_LENGTH(COMPRESS(REPEAT('a',30)));
-> 30
鎵ц浠HEX(str)鐨勫弽鍚戞搷浣溿傚氨鏄锛屽畠灏嗗弬鏁颁腑鐨勬瘡涓瀵瑰崄鍏繘鍒舵暟瀛楃悊瑙d负涓涓暟瀛楋紝骞跺皢鍏惰浆鍖栦负璇ユ暟瀛椾唬琛ㄧ殑瀛楃銆傜粨鏋滃瓧绗︿互浜岃繘鍒跺瓧绗︿覆鐨勫舰寮忚繑鍥炪
mysql> SELECT UNHEX('4D7953514C');
-> 'MySQL'
mysql> SELECT 0x4D7953514C;
-> 'MySQL'
mysql> SELECT UNHEX(HEX('string'));
-> 'string'
mysql> SELECT HEX(UNHEX('1267'));
-> '1267'
杩斿洖瀛楃涓str锛 浠ュ強鏍规嵁鏈鏂板瓧绗﹂泦鏄犲皠杞寲涓哄ぇ鍐欏瓧姣嶇殑瀛楃 (榛樿涓cp1252 Latin1).
mysql> SELECT UPPER('Hej');
-> 'HEJ'
璇ュ嚱鏁版敮鎸佸瀛楄妭瀛楀厓銆
鏍规嵁锛MySQL 浼氳嚜鍔ㄥ皢鏁板瓧杞寲涓哄瓧绗︿覆锛屽弽涔嬩害鐒躲
mysql> SELECT 1+'1';
-> 2
mysql> SELECT CONCAT(2,' test');
-> '2 test'
鑻ユ兂瑕佸皢鏁板瓧鏄庣‘鍦拌浆鍖栦负瀛楃涓诧紝鍙娇鐢 CAST()鎴 CONCAT()鍑芥暟锛
mysql> SELECT 38.8, CAST(38.8 AS CHAR);
-> 38.8, '38.8'
mysql> SELECT 38.8, CONCAT(38.8);
-> 38.8, '38.8'
CAST() 姣旇緝鍙彇銆.
鑻ュ凡缁忓涓涓瓧绗︿覆鍑芥暟缁欏畾涓涓簩杩涘埗瀛楃涓蹭綔涓哄弬鏁帮紝 鍒欐墍寰楀埌鐨勭粨鏋滃瓧绗︿覆涔熸槸涓涓簩杩涘埗瀛楃涓层備竴涓浆鍖栦负瀛楃涓茬殑鏁板瓧琚綔涓轰簩杩涘埗瀛楃涓插寰呫傝繖浠呬細瀵规瘮杈冪粨鏋滀骇鐢熷奖鍝嶃
涓鑸岃█, 鑻ュ瓧绗︿覆姣旇緝涓换鎰忎竴涓〃杈惧紡鏄尯鍒嗗ぇ灏忓啓鐨勶紝鍒欐墽琛屾瘮杈冩椂涔熷尯鍒嗗ぇ灏忓啓銆
妯″紡鍖归厤锛屼娇鐢SQL绠鍗曟瑙勮〃杈惧紡姣旇緝銆傝繑鍥1 (TRUE) 鎴 0 (FALSE)銆 鑻 expr 鎴 pat 涓换浣曚竴涓负 NULL,鍒欑粨鏋滀负 NULL銆
妯″紡涓嶉渶瑕佷负鏂囧瓧瀛楃涓层備緥濡傦紝鍙互琚寚瀹氫负涓涓瓧绗︿覆琛ㄨ揪寮忔垨琛ㄥ垪銆
鍦ㄦā寮忎腑鍙互鍚LIKE涓璧蜂娇鐢ㄤ互涓嬩袱绉嶉氶厤绗︼細
瀛楃 |
璇存槑 |
% |
鍖归厤浠讳綍鏁扮洰鐨勫瓧绗︼紝鐢氳嚦鍖呮嫭闆跺瓧绗 |
_ |
鍙兘鍖归厤涓绉嶅瓧绗 |
mysql> SELECT 'David!' LIKE 'David_';
-> 1
mysql> SELECT 'David!' LIKE '%D%v%';
-> 1
鑻ヨ瀵归氶厤绗︾殑鏂囧瓧瀹炰緥杩涜妫楠, 鍙皢杞箟瀛楃鏀惧湪璇ュ瓧绗﹀墠闈€傚鏋滄病鏈夋寚瀹 ESCAPE瀛楃, 鍒欏亣璁句负鈥\鈥欍
瀛楃涓 |
璇存槑 |
|
\% |
鍖归厤涓涓 鈥%鈥欏瓧绗 |
|
\_ |
鍖归厤涓涓 鈥_鈥 瀛楃 |
mysql> SELECT 'David!' LIKE 'David\_';
-> 0
mysql> SELECT 'David_' LIKE 'David\_';
-> 1
瑕佹寚瀹氫竴涓笉鍚岀殑杞箟瀛楃,鍙娇鐢ESCAPE璇彞锛
mysql> SELECT 'David_' LIKE 'David|_' ESCAPE '|';
-> 1
杞箟搴忓垪鍙互涓虹┖锛屼篃鍙互鏄竴涓瓧绗︾殑闀垮害銆 浠 MySQL 5.1.2寮濮, 濡傝嫢 NO_BACKSLASH_ESCAPES SQL妯″紡琚縺娲, 鍒欒搴忓垪涓嶈兘涓虹┖銆
浠ヤ笅涓や釜璇彞涓句緥璇存槑浜嗗瓧绗︿覆姣旇緝涓嶅尯鍒嗗ぇ灏忓啓锛岄櫎闈炲叾涓竴涓搷浣滄暟涓轰簩杩涘埗瀛楃涓诧細
mysql> SELECT 'abc' LIKE 'ABC';
-> 1
mysql> SELECT 'abc' LIKE BINARY 'ABC';
-> 0
鍦MySQL涓, LIKE 鍏佽鍑虹幇鍦ㄦ暟瀛楄〃杈惧紡涓 (杩欐槸鏍囧噯SQL LIKE 鐨勫欢浼革級銆
mysql> SELECT 10 LIKE '1%';
-> 1
娉ㄩ噴锛 鐢变簬 MySQL鍦ㄥ瓧绗︿覆涓娇鐢 C杞箟璇硶(渚嬪, 鐢ㄢ\n鈥欎唬琛ㄤ竴涓崲琛屽瓧绗)锛屽湪LIKE瀛楃涓蹭腑锛屽繀椤诲皢鐢ㄥ埌鐨勨\鈥欏弻鍐欍備緥濡傦紝 鑻ヨ鏌ユ壘 鈥\n鈥, 蹇呴』灏嗗叾鍐欐垚 鈥\\n鈥欍傝岃嫢瑕佹煡鎵 鈥\鈥, 鍒欏繀椤诲皢鍏跺啓鎴 it as 鈥\\\\鈥;鍘熷洜鏄弽鏂滅嚎绗﹀彿浼氳璇硶鍒嗘瀽绋嬪簭鍓ョ涓娆★紝鍦ㄨ繘琛屾ā寮忓尮閰嶆椂锛屽張浼氳鍓ョ涓娆★紝鏈鍚庝細鍓╀笅涓涓弽鏂滅嚎绗﹀彿鎺ュ彈鍖归厤銆
杩欑浉褰撲簬 NOT (expr LIKE pat [ESCAPE 'escape-char'])銆
杩欑浉褰撲簬NOT (expr REGEXP pat)銆
鎵ц瀛楃涓茶〃杈惧紡 expr 鍜屾ā寮pat 鐨勬ā寮忓尮閰嶃傝妯″紡鍙互琚欢浼镐负姝h琛ㄨ揪寮忋傛瑙勮〃杈惧紡鐨勮娉曞湪闄勫綍G锛MySQL姝e垯琛ㄨ揪寮涓湁璇︾粏璁ㄨ銆傝嫢expr 鍖归厤 pat锛鍒欒繑鍥 1; 鍚﹀垯杩斿洖0銆傝嫢 expr 鎴 pat 浠绘剰涓涓负 NULL, 鍒欑粨鏋滀负 NULL銆 RLIKE 鏄REGEXP鐨勫悓涔夎瘝, 浣滅敤鏄负mSQL 鎻愪緵鍏煎鎬с
妯″紡涓嶉渶瑕佷负鏂囧瓧瀛楃涓层備緥濡,鍙互琚寚瀹氫负涓涓瓧绗︿覆琛ㄨ揪寮忔垨琛ㄥ垪銆
娉ㄩ噴锛鐢变簬鍦ㄥ瓧绗︿覆涓紝 MySQL浣跨敤 C 杞箟璇硶 (渚嬪, 鐢ㄢ\n鈥欐潵浠h〃鎹㈣瀛楃 ),鍦REGEXP瀛楃涓蹭腑蹇呴』灏嗙敤鍒扮殑鈥\鈥 鍙屽啓銆
REGEXP 涓嶅尯鍒嗗ぇ灏忓啓, 闄ら潪灏嗗叾鍚屼簩杩涘埗瀛楃涓插悓鏃朵娇鐢ㄣ
mysql> SELECT 'Monty!' REGEXP 'm%y%%';
-> 0
mysql> SELECT 'Monty!' REGEXP '.*';
-> 1
mysql> SELECT 'new*\n*line' REGEXP 'new\\*.\\*line';
-> 1
mysql> SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A';
-> 1 0
mysql> SELECT 'a' REGEXP '^[a-d]';
-> 1
鍦ㄧ‘瀹氬瓧绗︾被鍨嬫椂锛REGEXP 鍜 RLIKE 浣跨敤褰撳墠瀛楃闆 (榛樿涓cp1252 Latin1 )銆 璀﹀憡锛杩欎簺鎿嶄綔绗︿笉鏀寔澶氬瓧鑺傚瓧鍏冦
鑻ユ墍鏈夌殑瀛楃涓插潎鐩稿悓锛屽垯杩斿洖STRCMP()锛岃嫢鏍规嵁褰撳墠鍒嗙被娆″簭锛岀涓涓弬鏁板皬浜庣浜屼釜锛屽垯杩斿洖 -1锛屽叾瀹冩儏鍐佃繑鍥 1 銆
mysql> SELECT STRCMP('text', 'text2');
-> -1
mysql> SELECT STRCMP('text2', 'text');
-> 1
mysql> SELECT STRCMP('text', 'text');
-> 0
鍦ㄦ墽琛屾瘮杈冩椂锛STRCMP() 浣跨敤褰撳墠瀛楃闆嗐傝繖浣垮緱榛樿鐨勬瘮杈冨尯鍒嗗ぇ灏忓啓锛屽綋鎿嶄綔鏁颁腑鐨勪竴涓垨涓や釜閮芥槸浜岃繘鍒跺瓧绗︿覆鏃堕櫎澶栥
鍙娇鐢ㄥ父瑙佺殑绠楁湳鎿嶄綔绗︺傛敞鎰忓氨 -銆 +鍜 *鑰岃█, 鑻ヤ袱涓弬鏁板潎涓烘鏁帮紝鍒欏叾璁$畻缁撴灉鐨勭簿纭害涓 BIGINT (64姣旂壒)锛岃嫢鍏朵腑涓涓弬鏁颁负鏃犵鍙锋暣鏁帮紝 鑰屽叾瀹冨弬鏁颁篃鏄暣鏁, 鍒欑粨鏋滀负鏃犵鍙锋暣鏁般傝鍙傝12.8鑺傦紝鈥淐ast鍑芥暟鍜屾搷浣滅鈥銆
鍔犲彿:
mysql> SELECT 3+5;
-> 8
鍑忓彿:
mysql> SELECT 3-5;
-> -2
涓鍏冨噺鍙枫傛洿鎹㈠弬鏁扮鍙枫
mysql> SELECT - 2;
-> -2
娉ㄦ剰锛氳嫢璇 鎿嶄綔绗﹀悓涓涓BIGINT鍚屾椂浣跨敤锛屽垯杩斿洖鍊间篃鏄竴涓BIGINT銆傝繖鎰忓懗鐫浣犲簲褰撳敖閲忛伩鍏嶅鍙兘浜х敓鈥263鐨勬暣鏁颁娇鐢 鈥撱
涔樺彿:
mysql> SELECT 3*5;
-> 15
mysql> SELECT 18014398509481984*18014398509481984.0;
-> 324518553658426726783156020576256.0
mysql> SELECT 18014398509481984*18014398509481984;
-> 0
鏈鍚庝竴涓〃杈惧紡鐨勭粨鏋滄槸涓嶆纭殑銆傚師鍥犳槸鏁存暟鐩镐箻鐨勭粨鏋滆秴杩囦簡BIGINT 璁$畻鐨 64姣旂壒鑼冨洿銆 (瑙11.2鑺傦紝鈥滄暟鍊肩被鍨嬧.)
闄ゅ彿:
mysql> SELECT 3/5;
-> 0.60
琚浂闄ょ殑缁撴灉涓 NULL锛
mysql> SELECT 102/(1-1);
-> NULL
鍙湁褰撴墽琛岀殑璇涓紝鍏剁粨鏋滆琚浆鍖栦负涓涓暣鏁版椂 锛岄櫎娉曟墠浼氬拰 BIGINT 绠楁硶涓璧蜂娇鐢ㄣ
鏁存暟闄ゆ硶銆 绫讳技浜 FLOOR()锛岀劧鑰屼娇鐢BIGINT 绠楁硶涔熸槸鍙潬鐨勩
mysql> SELECT 5 DIV 2;
-> 2
鑻ュ彂鐢熼敊璇紝鎵鏈夋暟瀛﹀嚱鏁颁細杩斿洖 NULL 銆
杩斿洖X 鐨勭粷瀵瑰笺
mysql> SELECT ABS(2);
-> 2
mysql> SELECT ABS(-32);
-> 32
璇ュ嚱鏁版敮鎸佷娇鐢BIGINT鍊笺
杩斿洖X 鍙嶄綑寮, 鍗, 浣欏鸡鏄X鐨勫笺傝嫢X 涓嶅湪-1鍒 1鐨勮寖鍥翠箣鍐咃紝鍒欒繑鍥 NULL 銆
mysql> SELECT ACOS(1);
-> 0
mysql> SELECT ACOS(1.0001);
-> NULL
mysql> SELECT ACOS(0);
-> 1.5707963267949
杩斿洖X 鐨勫弽姝e鸡锛屽嵆锛屾寮︿负X 鐨勫笺傝嫢X 鑻X 涓嶅湪-1鍒 1鐨勮寖鍥翠箣鍐咃紝鍒欒繑鍥 NULL 銆
mysql> SELECT ASIN(0.2);
-> 0.20135792079033
mysql> SELECT ASIN('foo');
+-------------+
| ASIN('foo') |
+-------------+
| 0 |
+-------------+
1 row in set, 1 warning (0.00 sec)
mysql> SHOW WARNINGS;
+---------+------+-----------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'foo' |
+---------+------+-----------------------------------------+
杩斿洖X 鐨勫弽姝e垏锛屽嵆锛屾鍒囦负X 鐨勫笺
mysql> SELECT ATAN(2);
-> 1.1071487177941
mysql> SELECT ATAN(-2);
-> -1.1071487177941
杩斿洖涓や釜鍙橀噺X 鍙Y鐨勫弽姝e垏銆 瀹冪被浼间簬 Y 鎴 X鐨勫弽姝e垏璁$畻, 闄ら潪涓や釜鍙傛暟鐨勭鍙峰潎鐢ㄤ簬纭畾缁撴灉鎵鍦ㄨ薄闄愩
mysql> SELECT ATAN(-2,2);
-> -0.78539816339745
mysql> SELECT ATAN2(PI(),0);
-> 1.5707963267949
杩斿洖涓嶅皬浜X 鐨勬渶灏忔暣鏁板笺
mysql> SELECT CEILING(1.23);
-> 2
mysql> SELECT CEIL(-1.23);
-> -1
杩欎袱涓嚱鏁扮殑鎰忎箟鐩稿悓銆傛敞鎰忚繑鍥炲间細琚浆鍖栦负涓涓BIGINT銆
杩斿洖X 鐨勪綑寮︼紝鍏朵腑X鍦ㄥ姬搴︿笂宸茬煡銆
mysql> SELECT COS(PI());
-> -1
杩斿洖X 鐨勪綑鍒囥
mysql> SELECT COT(12);
-> -1.5726734063977
mysql> SELECT COT(0);
-> NULL
璁$畻寰幆鍐椾綑鐮佹牎楠鍊煎苟杩斿洖涓涓 32姣旂壒鏃犵鍙峰笺傝嫢鍙傛暟涓NULL 锛屽垯缁撴灉涓 NULL銆傝鍙傛暟搴斾负涓涓瓧绗︿覆锛岃屼笖鍦ㄤ笉鏄瓧绗︿覆鐨勬儏鍐典笅浼氳浣滀负瀛楃涓插鐞嗭紙鑻ユ湁鍙兘锛夈
mysql> SELECT CRC32('MySQL');
-> 3259397556
mysql> SELECT CRC32('mysql');
-> 2501908538
杩斿洖鍙傛暟 X, 璇ュ弬鏁扮敱寮у害琚浆鍖栦负搴︺
mysql> SELECT DEGREES(PI());
-> 180
mysql> SELECT DEGREES(PI() / 2);
-> 90
杩斿洖e鐨X涔樻柟鍚庣殑鍊(鑷劧瀵规暟鐨勫簳)銆
mysql> SELECT EXP(2);
-> 7.3890560989307
mysql> SELECT EXP(-2);
-> 0.13533528323661
mysql> SELECT EXP(0);
-> 1
杩斿洖涓嶅ぇ浜X鐨勬渶澶ф暣鏁板 銆
mysql> SELECT FLOOR(1.23);
-> 1
mysql> SELECT FLOOR(-1.23);
-> -2
娉ㄦ剰锛岃繑鍥炲间細琚浆鍖栦负涓涓 BIGINT銆
灏嗘暟瀛X 鐨勬牸寮忓啓鎴'#,###,###.##'鏍煎紡, 鍗充繚鐣欏皬鏁扮偣鍚 D浣嶏紝鑰岀D浣嶇殑淇濈暀鏂瑰紡涓哄洓鑸嶄簲鍏ワ紝鐒跺悗灏嗙粨鏋滀互瀛楃涓茬殑褰㈠紡杩斿洖銆璇﹁12.9.4鑺傦紝鈥滃叾浠栧嚱鏁扳銆
杩斿洖X 鐨勮嚜鐒跺鏁,鍗, X 鐩稿浜庡熀鏁e 鐨勫鏁般
mysql> SELECT LN(2);
-> 0.69314718055995
mysql> SELECT LN(-2);
-> NULL
杩欎釜鍑芥暟鍚LOG(X)鍏锋湁鐩稿悓鎰忎箟銆
鑻ョ敤涓涓弬鏁拌皟鐢紝杩欎釜鍑芥暟灏变細杩斿洖X 鐨勮嚜鐒跺鏁般
mysql> SELECT LOG(2);
-> 0.69314718055995
mysql> SELECT LOG(-2);
-> NULL
鑻ョ敤涓や釜鍙傛暟杩涜璋冪敤锛岃繖涓嚱鏁颁細杩斿洖X 瀵逛簬浠绘剰鍩烘暟B 鐨勫鏁般
mysql> SELECT LOG(2,65536);
-> 16
mysql> SELECT LOG(10,100);
-> 2
LOG(B,X) 灏辩浉褰撲簬 LOG(X) / LOG(B)銆
杩斿洖X 鐨勫熀鏁颁负2鐨勫鏁般
mysql> SELECT LOG2(65536);
-> 16
mysql> SELECT LOG2(-100);
-> NULL
瀵逛簬鏌ュ嚭瀛樺偍涓涓暟瀛楅渶瑕佸灏戜釜姣旂壒锛LOG2()闈炲父鏈夋晥銆傝繖涓嚱鏁扮浉褰撲簬琛ㄨ揪寮 LOG(X) / LOG(2)銆
杩斿洖X鐨勫熀鏁颁负10鐨勫鏁般
mysql> SELECT LOG10(2);
-> 0.30102999566398
mysql> SELECT LOG10(100);
-> 2
mysql> SELECT LOG10(-100);
-> NULL
LOG10(X)鐩稿綋浜LOG(10,X)銆
妯℃搷浣溿傝繑鍥N 琚 M闄ゅ悗鐨勪綑鏁般
mysql> SELECT MOD(234, 10);
-> 4
mysql> SELECT 253 % 7;
-> 1
mysql> SELECT MOD(29,9);
-> 2
mysql> SELECT 29 MOD 9;
-> 2
杩欎釜鍑芥暟鏀寔浣跨敤BIGINT 鍊笺
MOD() 瀵逛簬甯︽湁灏忔暟閮ㄥ垎鐨勬暟鍊间篃璧蜂綔鐢紝 瀹冭繑鍥為櫎娉曡繍绠楀悗鐨勭簿纭綑鏁帮細
mysql> SELECT MOD(34.5,3);
-> 1.5
杩斿洖 蠔 (pi)鐨勫笺傞粯璁ょ殑鏄剧ず灏忔暟浣嶆暟鏄7浣,鐒惰 MySQL鍐呴儴浼氫娇鐢ㄥ畬鍏ㄥ弻绮惧害鍊笺
mysql> SELECT PI();
-> 3.141593
mysql> SELECT PI()+0.000000000000000000;
-> 3.141592653589793116
杩斿洖X 鐨Y涔樻柟鐨勭粨鏋滃笺
mysql> SELECT POW(2,2);
-> 4
mysql> SELECT POW(2,-2);
-> 0.25
杩斿洖鐢卞害杞寲涓哄姬搴︾殑鍙傛暟 X, (娉ㄦ剰 蠔 寮у害绛変簬180搴︼級銆
mysql> SELECT RADIANS(90);
-> 1.5707963267949
杩斿洖涓涓殢鏈烘诞鐐瑰 v 锛岃寖鍥村湪 0 鍒1 涔嬮棿 (鍗, 鍏惰寖鍥翠负 0 鈮 v 鈮 1.0)銆傝嫢宸叉寚瀹氫竴涓暣鏁板弬鏁 N 锛屽垯瀹冭鐢ㄤ綔绉嶅瓙鍊硷紝鐢ㄦ潵浜х敓閲嶅搴忓垪銆
mysql> SELECT RAND();
-> 0.9233482386203
mysql> SELECT RAND(20);
-> 0.15888261251047
mysql> SELECT RAND(20);
-> 0.15888261251047
mysql> SELECT RAND();
-> 0.63553050033332
mysql> SELECT RAND();
-> 0.70100469486881
mysql> SELECT RAND(20);
-> 0.15888261251047
鑻ヨ鍦i 鈮 R 鈮 j 杩欎釜鑼冨洿寰楀埌涓涓殢鏈烘暣鏁R 锛岄渶瑕佺敤鍒拌〃杈惧紡 FLOOR(i + RAND() * (j 鈥 i + 1))銆備緥濡傦紝 鑻ヨ鍦7 鍒 12 鐨勮寖鍥达紙鍖呮嫭7鍜12锛夊唴寰楀埌涓涓殢鏈烘暣鏁, 鍙娇鐢ㄤ互涓嬭鍙ワ細
SELECT FLOOR(7 + (RAND() * 6));
鍦ORDER BY璇彞涓紝涓嶈兘浣跨敤涓涓甫鏈RAND()鍊肩殑鍒楋紝鍘熷洜鏄 ORDER BY 浼氳绠楀垪鐨勫閲嶆椂闂淬傜劧鑰岋紝鍙寜鐓у涓嬬殑闅忔満椤哄簭妫绱㈡暟鎹锛
mysql> SELECT * FROM tbl_name ORDER BY RAND();
ORDER BY RAND()鍚 LIMIT 鐨勭粨鍚堜粠涓缁勫垪涓夋嫨闅忔満鏍锋湰寰堟湁鐢細
mysql> SELECT * FROM table1, table2 WHERE a=b AND c<d
-> ORDER BY RAND() LIMIT 1000;
娉ㄦ剰锛屽湪WHERE璇彞涓紝WHERE姣忔墽琛屼竴娆★紝 RAND()灏变細琚啀璁$畻涓娆°
RAND()鐨勪綔鐢ㄤ笉鏄綔涓轰竴涓簿纭殑闅忔満鍙戠敓鍣紝鑰屾槸涓绉嶇敤鏉ュ彂鐢熷湪鍚屾牱鐨 MySQL鐗堟湰鐨勫钩鍙颁箣闂寸殑鍙Щ鍔ad hoc闅忔満鏁扮殑蹇熸柟寮忋
杩斿洖鍙傛暟X, 鍏跺兼帴杩戜簬鏈杩戜技鐨勬暣鏁般傚湪鏈変袱涓弬鏁扮殑鎯呭喌涓嬶紝杩斿洖 X 锛屽叾鍊间繚鐣欏埌灏忔暟鐐瑰悗D浣嶏紝鑰岀D浣嶇殑淇濈暀鏂瑰紡涓哄洓鑸嶄簲鍏ャ傝嫢瑕佹帴淇濈暀X鍊煎皬鏁扮偣宸﹁竟鐨D 浣嶏紝鍙皢 D 璁句负璐熷笺
mysql> SELECT ROUND(-1.23);
-> -1
mysql> SELECT ROUND(-1.58);
-> -2
mysql> SELECT ROUND(1.58);
-> 2
mysql> SELECT ROUND(1.298, 1);
-> 1.3
mysql> SELECT ROUND(1.298, 0);
-> 1
mysql> SELECT ROUND(23.298, -1);
-> 20
杩斿洖鍊肩殑绫诲瀷鍚 绗竴涓嚜鍙橀噺鐩稿悓(鍋囪瀹冩槸涓涓暣鏁般佸弻绮惧害鏁版垨灏忔暟)銆傝繖鎰忓懗鐫瀵逛簬涓涓暣鏁板弬鏁,缁撴灉涔熸槸涓涓暣鏁(鏃犲皬鏁伴儴鍒)銆
褰撶涓涓弬鏁版槸鍗佽繘鍒跺父鏁版椂锛屽浜庡噯纭煎弬鏁帮紝ROUND() 浣跨敤绮惧瘑鏁板棰樺簱锛
浠ヤ笅涓句緥璇存槑鑸嶅叆娉曞浜庣簿纭煎拰杩戜技鍊肩殑涓嶅悓涔嬪锛
mysql> SELECT ROUND(2.5), ROUND(25E-1);
+------------+--------------+
| ROUND(2.5) | ROUND(25E-1) |
+------------+--------------+
| 3 | 2 |
+------------+--------------+
杩斿洖鍙傛暟浣滀负-1銆 0鎴1鐨勭鍙凤紝璇ョ鍙峰彇鍐充簬X 鐨勫间负璐熴侀浂鎴栨銆
mysql> SELECT SIGN(-32);
-> -1
mysql> SELECT SIGN(0);
-> 0
mysql> SELECT SIGN(234);
-> 1
杩斿洖X 姝e鸡锛屽叾涓 X 鍦ㄥ姬搴︿腑琚粰瀹氥
mysql> SELECT SIN(PI());
-> 1.2246063538224e-16
mysql> SELECT ROUND(SIN(PI()));
-> 0
杩斿洖闈炶礋鏁X 鐨勪簩娆℃柟鏍广
mysql> SELECT SQRT(4);
-> 2
mysql> SELECT SQRT(20);
-> 4.4721359549996
mysql> SELECT SQRT(-16);
-> NULL
杩斿洖X 鐨勬鍒囷紝鍏朵腑X 鍦ㄥ姬搴︿腑琚粰瀹氥
mysql> SELECT TAN(PI());
-> -1.2246063538224e-16
mysql> SELECT TAN(PI()+1);
-> 1.5574077246549
杩斿洖琚垗鍘昏嚦灏忔暟鐐瑰悗D浣嶇殑鏁板瓧X銆傝嫢D 鐨勫间负 0, 鍒欑粨鏋滀笉甯︽湁灏忔暟鐐规垨涓嶅甫鏈夊皬鏁伴儴鍒嗐鍙互灏D璁句负璐熸暟,鑻ヨ鎴幓(褰掗浂) X灏忔暟鐐瑰乏璧风D浣嶅紑濮嬪悗闈㈡墍鏈変綆浣嶇殑鍊.
mysql> SELECT TRUNCATE(1.223,1);
-> 1.2
mysql> SELECT TRUNCATE(1.999,1);
-> 1.9
mysql> SELECT TRUNCATE(1.999,0);
-> 1
mysql> SELECT TRUNCATE(-1.999,1);
-> -1.9
mysql> SELECT TRUNCATE(122,-2);
-> 100
mysql> SELECT TRUNCATE(10.28*100,0);
-> 1028
鎵鏈夋暟瀛楃殑鑸嶅叆鏂瑰悜閮芥帴杩戜簬闆躲
鏈珷璁鸿堪浜嗕竴浜涘彲鐢ㄤ簬鎿嶄綔鏃堕棿鍊肩殑鍑芥暟銆傚叧浜庢瘡涓椂闂村拰鏃ユ湡绫诲瀷鍏锋湁鐨勫煎煙鍙婃寚瀹氬肩殑鏈夋晥鏍煎紡锛岃鍙傝11.3鑺傦紝鈥滄棩鏈熷拰鏃堕棿绫诲瀷鈥銆
涓嬮潰鐨勪緥瀛愪娇鐢ㄤ簡鏃堕棿鍑芥暟銆備互涓嬭闂夋嫨浜嗘渶杩戠殑 30澶╁唴鎵鏈夊甫鏈date_col 鍊肩殑璁板綍锛
mysql> SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;
娉ㄦ剰锛岃繖涓闂篃鑳介夋嫨灏嗘潵鐨勬棩鏈熻褰曘
鐢ㄤ簬鏃ユ湡鍊肩殑鍑芥暟閫氬父浼氭帴鍙楁椂闂存棩鏈熷艰屽拷鐣ユ椂闂撮儴鍒嗐傝岀敤浜庢椂闂村肩殑鍑芥暟閫氬父鎺ュ彈鏃堕棿鏃ユ湡鍊艰屽拷鐣ユ棩鏈熼儴鍒嗐
杩斿洖鍚勮嚜褰撳墠鏃ユ湡鎴栨椂闂寸殑鍑芥暟鍦ㄦ瘡娆¤闂墽琛屽紑濮嬫椂璁$畻涓娆°傝繖鎰忓懗鐫鍦ㄤ竴涓崟涓璇㈤棶涓紝瀵硅濡NOW() 鐨勫嚱鏁板娆¤闂绘槸浼氬緱鍒板悓鏍风殑缁撴灉(鏈揪鍒版垜浠殑鐩殑锛屽崟涓璇㈤棶涔熷寘鎷瀛樺偍绋嬪簭鎴栬Е鍙戝櫒鍜岃璇ョ▼搴/瑙﹀彂鍣ㄨ皟鐢ㄧ殑鎵鏈夊瓙绋嬪簭鐨勮皟鐢 )銆傝繖椤瑰師鍒欎篃閫傜敤浜 CURDATE()銆 CURTIME()銆 UTC_DATE()銆 UTC_TIME()銆UTC_TIMESTAMP()锛屼互鍙婃墍鏈夊拰瀹冧滑鎰忎箟鐩稿悓鐨勫嚱鏁般
CURRENT_TIMESTAMP()銆 CURRENT_TIME()銆 CURRENT_DATE()浠ュ強FROM_UNIXTIME()鍑芥暟杩斿洖杩炴帴褰撳墠鏃跺尯鍐呯殑鍊硷紝杩欎釜鍊煎彲鐢ㄤ綔time_zone绯荤粺鍙橀噺鐨勫笺傛澶栵紝 UNIX_TIMESTAMP() 鍋囪鍏跺弬鏁颁负涓涓綋鍓嶆椂鍖虹殑鏃堕棿鏃ユ湡鍊笺傝鍙傝5.10.8鑺傦紝鈥淢ySQL鏈嶅姟鍣ㄦ椂鍖烘敮鎸佲銆
浠ヤ笅鍑芥暟鐨勮杩颁腑杩斿洖鍊肩殑鑼冨洿浼氳姹傚畬鍏ㄦ棩鏈熴 鑻ヤ竴涓棩鏈熶负鈥滈浂鈥 鍊硷紝鎴栬呮槸涓涓濡'2001-11-00'涔嬬被鐨勪笉瀹屽叏鏃ユ湡锛 鎻愬彇閮ㄥ垎鏃ユ湡鍊肩殑鍑芥暟鍙兘浼氳繑鍥 0銆 渚嬪锛 DAYOFMONTH('2001-11-00') 浼氳繑鍥0銆
褰撹绗簩涓弬鏁扮殑INTERVAL鏍煎紡婵娲诲悗锛 ADDDATE()灏辨槸DATE_ADD()鐨勫悓涔夎瘝銆傜浉鍏冲嚱鏁SUBDATE() 鍒欐槸DATE_SUB()鐨勫悓涔夎瘝銆傚浜INTERVAL鍙傛暟涓婄殑淇℃伅 锛岃鍙傝鍏充簬DATE_ADD()鐨勮杩般
mysql> SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY);
-> '1998-02-02'
mysql> SELECT ADDDATE('1998-01-02', INTERVAL 31 DAY);
-> '1998-02-02'
鑻 days 鍙傛暟鍙槸鏁存暟鍊硷紝鍒 MySQL 5.1灏嗗叾浣滀负澶╂暟鍊兼坊鍔犺嚦 expr銆
mysql> SELECT ADDDATE('1998-01-02', 31);
-> '1998-02-02'
ADDTIME()灏 expr2娣诲姞鑷expr 鐒跺悗杩斿洖缁撴灉銆 expr 鏄竴涓椂闂存垨鏃堕棿鏃ユ湡琛ㄨ揪寮忥紝鑰expr2 鏄竴涓椂闂磋〃杈惧紡銆
mysql> SELECT ADDTIME('1997-12-31 23:59:59.999999',
-> '1 1:1:1.000002');
-> '1998-01-02 01:01:01.000001'
mysql> SELECT ADDTIME('01:00:00.999999', '02:00:00.999998');
-> '03:00:01.999997'
CONVERT_TZ() 灏嗘椂闂存棩鏈熷dt 浠from_tz 缁欏嚭鐨勬椂鍖鸿浆鍒to_tz缁欏嚭鐨勬椂鍖猴紝鐒跺悗杩斿洖缁撴灉鍊笺傚叧浜庡彲鑳芥寚瀹氱殑鏃跺尯鐨勮缁嗚杩帮紝璇峰弬瑙5.10.8鑺傦紝鈥淢ySQL鏈嶅姟鍣ㄦ椂鍖烘敮鎸佲銆傝嫢鑷彉閲忔棤鏁堬紝鍒欒繖涓嚱鏁颁細杩斿洖 NULL銆
鍦ㄤ粠鑻from_tz 鍒UTC鐨勮浆鍖栬繃绋嬩腑锛岃鍊艰秴鍑 TIMESTAMP 绫诲瀷鐨勮鏀寔鑼冨洿锛岄偅涔堣浆鍖栦笉浼氬彂鐢熴傚叧浜 TIMESTAMP 鑼冨洿鐨勮杩帮紝璇峰弬瑙11.1.2鑺傦紝鈥滄棩鏈熷拰鏃堕棿绫诲瀷姒傝堪鈥銆
mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
-> '2004-01-01 13:00:00'
mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');
-> '2004-01-01 22:00:00'
娉ㄩ噴锛鑻ヨ浣跨敤璇稿 'MET'鎴 'Europe/Moscow'涔嬬被鐨勬寚瀹氭椂闂村尯锛岄鍏堣璁剧疆姝g‘鐨勬椂鍖鸿〃銆傝缁嗚鏄庤5.10.8鑺傦紝鈥淢ySQL鏈嶅姟鍣ㄦ椂鍖烘敮鎸佲銆
灏嗗綋鍓嶆棩鏈熸寜鐓'YYYY-MM-DD' 鎴YYYYMMDD 鏍煎紡鐨勫艰繑鍥烇紝鍏蜂綋鏍煎紡鏍规嵁鍑芥暟鐢ㄥ湪瀛楃涓叉垨鏄暟瀛楄澧冧腑鑰屽畾銆
mysql> SELECT CURDATE();
-> '1997-12-15'
mysql> SELECT CURDATE() + 0;
-> 19971215
CURRENT_DATE鍜CURRENT_DATE()鏄殑鍚屼箟璇.
灏嗗綋鍓嶆椂闂翠互'HH:MM:SS'鎴 HHMMSS 鐨勬牸寮忚繑鍥烇紝 鍏蜂綋鏍煎紡鏍规嵁鍑芥暟鐢ㄥ湪瀛楃涓叉垨鏄暟瀛楄澧冧腑鑰屽畾銆
mysql> SELECT CURTIME();
-> '23:50:26'
mysql> SELECT CURTIME() + 0;
-> 235026
CURRENT_TIME 鍜CURRENT_TIME() 鏄CURTIME()鐨勫悓涔夎瘝銆
CURRENT_TIMESTAMP鍜 CURRENT_TIMESTAMP()鏄NOW()鐨勫悓涔夎瘝銆
鎻愬彇鏃ユ湡鎴栨椂闂存棩鏈熻〃杈惧紡expr涓殑鏃ユ湡閮ㄥ垎銆
mysql> SELECT DATE('2003-12-31 01:02:03');
-> '2003-12-31'
DATEDIFF() 杩斿洖璧峰鏃堕棿 expr鍜岀粨鏉熸椂闂expr2涔嬮棿鐨勫ぉ鏁般Expr鍜expr2 涓烘棩鏈熸垨 date-and-time 琛ㄨ揪寮忋傝绠椾腑鍙敤鍒拌繖浜涘肩殑鏃ユ湡閮ㄥ垎銆
mysql> SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');
-> 1
mysql> SELECT DATEDIFF('1997-11-30 23:59:59','1997-12-31');
-> -31
杩欎簺鍑芥暟鎵ц鏃ユ湡杩愮畻銆 date 鏄竴涓 DATETIME 鎴DATE鍊硷紝鐢ㄦ潵鎸囧畾璧峰鏃堕棿銆 expr 鏄竴涓〃杈惧紡锛岀敤鏉ユ寚瀹氫粠璧峰鏃ユ湡娣诲姞鎴栧噺鍘荤殑鏃堕棿闂撮殧鍊笺 Expr鏄竴涓瓧绗︿覆;瀵逛簬璐熷肩殑鏃堕棿闂撮殧锛屽畠鍙互浠ヤ竴涓 鈥-鈥欏紑澶淬 type 涓哄叧閿瘝锛屽畠鎸囩ず浜嗚〃杈惧紡琚В閲婄殑鏂瑰紡銆
鍏抽敭璇INTERVA鍙 type 鍒嗙被绗﹀潎涓嶅尯鍒嗗ぇ灏忓啓銆
浠ヤ笅琛ㄦ樉绀轰簡type 鍜expr 鍙傛暟鐨勫叧绯伙細
type 鍊 |
棰勬湡鐨 expr 鏍煎紡 |
MICROSECOND |
MICROSECONDS |
SECOND |
SECONDS |
MINUTE |
MINUTES |
HOUR |
HOURS |
DAY |
DAYS |
WEEK |
WEEKS |
MONTH |
MONTHS |
QUARTER |
QUARTERS |
YEAR |
YEARS |
SECOND_MICROSECOND |
'SECONDS.MICROSECONDS' |
MINUTE_MICROSECOND |
'MINUTES.MICROSECONDS' |
MINUTE_SECOND |
'MINUTES:SECONDS' |
HOUR_MICROSECOND |
'HOURS.MICROSECONDS' |
HOUR_SECOND |
'HOURS:MINUTES:SECONDS' |
HOUR_MINUTE |
'HOURS:MINUTES' |
DAY_MICROSECOND |
'DAYS.MICROSECONDS' |
DAY_SECOND |
'DAYS HOURS:MINUTES:SECONDS' |
DAY_MINUTE |
'DAYS HOURS:MINUTES' |
DAY_HOUR |
'DAYS HOURS' |
YEAR_MONTH |
'YEARS-MONTHS' |
MySQL 鍏佽浠讳綍expr 鏍煎紡涓殑鏍囩偣鍒嗛殧绗︺傝〃涓墍鏄剧ず鐨勬槸寤鸿鐨 鍒嗛殧绗︺傝嫢 date 鍙傛暟鏄竴涓 DATE 鍊硷紝鑰屼綘鐨勮绠楀彧浼氬寘鎷 YEAR銆MONTH鍜DAY閮ㄥ垎(鍗, 娌℃湁鏃堕棿閮ㄥ垎), 鍏剁粨鏋滄槸涓涓DATE 鍊笺傚惁鍒欙紝缁撴灉灏嗘槸涓涓 DATETIME鍊笺
鑻ヤ綅浜庡彟涓绔殑琛ㄨ揪寮忔槸涓涓棩鏈熸垨鏃ユ湡鏃堕棿鍊 锛 鍒INTERVAL expr type鍙厑璁稿湪 + 鎿嶄綔绗︾殑涓ょ銆傚浜 鈥撴搷浣滅锛 INTERVAL expr type 鍙厑璁稿湪鍏跺彸绔紝鍘熷洜鏄粠涓涓椂闂撮棿闅斾腑鎻愬彇涓涓棩鏈熸垨鏃ユ湡鏃堕棿鍊兼槸姣棤鎰忎箟鐨勩 (瑙佷笅闈㈢殑渚嬪瓙锛銆
mysql> SELECT '1997-12-31 23:59:59' + INTERVAL 1 SECOND;
-> '1998-01-01 00:00:00'
mysql> SELECT INTERVAL 1 DAY + '1997-12-31';
-> '1998-01-01'
mysql> SELECT '1998-01-01' - INTERVAL 1 SECOND;
-> '1997-12-31 23:59:59'
mysql> SELECT DATE_ADD('1997-12-31 23:59:59',
-> INTERVAL 1 SECOND);
-> '1998-01-01 00:00:00'
mysql> SELECT DATE_ADD('1997-12-31 23:59:59',
-> INTERVAL 1 DAY);
-> '1998-01-01 23:59:59'
mysql> SELECT DATE_ADD('1997-12-31 23:59:59',
-> INTERVAL '1:1' MINUTE_SECOND);
-> '1998-01-01 00:01:00'
mysql> SELECT DATE_SUB('1998-01-01 00:00:00',
-> INTERVAL '1 1:1:1' DAY_SECOND);
-> '1997-12-30 22:58:59'
mysql> SELECT DATE_ADD('1998-01-01 00:00:00',
-> INTERVAL '-1 10' DAY_HOUR);
-> '1997-12-30 14:00:00'
mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
-> '1997-12-02'
mysql> SELECT DATE_ADD('1992-12-31 23:59:59.000002',
-> INTERVAL '1.999999' SECOND_MICROSECOND);
-> '1993-01-01 00:00:01.000001'
鑻ヤ綘鎸囧畾浜嗕竴涓繃浜庣煭鐨勬椂闂撮棿闅斿 (涓嶅寘鎷type 鍏抽敭璇嶆墍棰勬湡鐨勬墍鏈夋椂闂撮棿闅旈儴鍒), MySQL 鍋囧畾浣犲凡缁忕渷鍘讳簡鏃堕棿闂撮殧鍊肩殑鏈宸﹂儴鍒嗐 渚嬪锛屼綘鎸囧畾浜嗕竴绉嶇被鍨嬬殑DAY_SECOND, expr 鐨勫奸鏈熷簲褰撳叿鏈夊ぉ銆 灏忔椂銆佸垎閽熷拰绉掗儴鍒嗐傝嫢浣犳寚瀹氫簡涓涓被浼 '1:10'鐨勫, MySQL 鍋囧畾澶╁拰灏忔椂閮ㄥ垎涓嶅瓨鍦紝閭d箞杩欎釜鍊间唬琛ㄥ垎鍜岀銆傛崲瑷涔, '1:10' DAY_SECOND 琚В閲婁负鐩稿綋浜 '1:10' MINUTE_SECOND銆傝繖鐩稿綋浜 MySQL灏TIME 鍊艰В閲婁负鎵鑰楄垂鐨勬椂闂磋屼笉鏄棩鏃剁殑瑙i噴鏂瑰紡銆
鍋囧浣犲涓涓棩鏈熷兼坊鍔犳垨鍑忓幓涓浜涘惈鏈夋椂闂撮儴鍒嗙殑鍐呭锛屽垯缁撴灉鑷姩杞寲涓轰竴涓棩鏈熸椂闂村硷細
mysql> SELECT DATE_ADD('1999-01-01', INTERVAL 1 DAY);
-> '1999-01-02'
mysql> SELECT DATE_ADD('1999-01-01', INTERVAL 1 HOUR);
-> '1999-01-01 01:00:00'
鍋囧浣犱娇鐢ㄤ簡鏍煎紡涓ラ噸閿欒鐨勬棩鏈,鍒欑粨鏋滀负 NULL銆傚亣濡備綘娣诲姞浜 MONTH銆YEAR_MONTH鎴YEAR 锛岃岀粨鏋滄棩鏈熶腑鏈変竴澶╃殑鏃ユ湡澶т簬娣诲姞鐨勬湀浠界殑鏃ユ湡鏈澶ч檺搴︼紝鍒欒繖涓棩鏈熻嚜鍔ㄨ璋冩暣涓烘坊鍔犳湀浠界殑鏈澶ф棩鏈燂細
mysql> SELECT DATE_ADD('1998-01-30', INTERVAL 1 MONTH);
-> '1998-02-28'
鏍规嵁format 瀛楃涓插畨鎺date 鍊肩殑鏍煎紡銆
浠ヤ笅璇存槑绗﹀彲鐢ㄥ湪 format 瀛楃涓蹭腑锛
璇存槑绗 |
璇存槑 |
%a |
宸ヤ綔鏃ョ殑缂╁啓鍚嶇О (Sun..Sat) |
%b |
鏈堜唤鐨勭缉鍐欏悕绉 (Jan..Dec) |
%c |
鏈堜唤锛屾暟瀛楀舰寮(0..12) |
%D |
甯︽湁鑻辫鍚庣紑鐨勮鏈堟棩鏈 (0th, 1st, 2nd, 3rd, ...) |
%d |
璇ユ湀鏃ユ湡, 鏁板瓧褰㈠紡 (00..31) |
%e |
璇ユ湀鏃ユ湡, 鏁板瓧褰㈠紡(0..31) |
%f |
寰 (000000..999999) |
%H |
灏忔椂(00..23) |
%h |
灏忔椂(01..12) |
%I |
灏忔椂 (01..12) |
%i |
鍒嗛挓,鏁板瓧褰㈠紡 (00..59) |
%j |
涓骞翠腑鐨勫ぉ鏁 (001..366) |
%k |
灏忔椂 (0..23) |
%l |
灏忔椂 (1..12) |
%M |
鏈堜唤鍚嶇О (January..December) |
%m |
鏈堜唤, 鏁板瓧褰㈠紡 (00..12) |
%p |
涓婂崍锛AM锛夋垨涓嬪崍锛 PM锛 |
%r |
鏃堕棿 , 12灏忔椂鍒 (灏忔椂hh:鍒嗛挓mm:绉掓暟ss 鍚庡姞 AM鎴PM) |
%S |
绉 (00..59) |
%s |
绉 (00..59) |
%T |
鏃堕棿 , 24灏忔椂鍒 (灏忔椂hh:鍒嗛挓mm:绉掓暟ss) |
%U |
鍛 (00..53), 鍏朵腑鍛ㄦ棩涓烘瘡鍛ㄧ殑绗竴澶 |
%u |
鍛 (00..53), 鍏朵腑鍛ㄤ竴涓烘瘡鍛ㄧ殑绗竴澶 |
%V |
鍛 (01..53), 鍏朵腑鍛ㄦ棩涓烘瘡鍛ㄧ殑绗竴澶 ; 鍜 %X鍚屾椂浣跨敤 |
%v |
鍛 (01..53), 鍏朵腑鍛ㄤ竴涓烘瘡鍛ㄧ殑绗竴澶 ; 鍜 %x鍚屾椂浣跨敤 |
%W |
宸ヤ綔鏃ュ悕绉 (鍛ㄦ棩..鍛ㄥ叚) |
%w |
涓鍛ㄤ腑鐨勬瘡鏃 (0=鍛ㄦ棩..6=鍛ㄥ叚) |
%X |
璇ュ懆鐨勫勾浠斤紝鍏朵腑鍛ㄦ棩涓烘瘡鍛ㄧ殑绗竴澶, 鏁板瓧褰㈠紡,4浣嶆暟;鍜%V鍚屾椂浣跨敤 |
%x |
璇ュ懆鐨勫勾浠斤紝鍏朵腑鍛ㄤ竴涓烘瘡鍛ㄧ殑绗竴澶, 鏁板瓧褰㈠紡,4浣嶆暟;鍜%v鍚屾椂浣跨敤 |
%Y |
骞翠唤, 鏁板瓧褰㈠紡,4浣嶆暟 |
%y |
骞翠唤, 鏁板瓧褰㈠紡 (2浣嶆暟) |
%% |
鈥%鈥欐枃瀛楀瓧绗 |
鎵鏈夊叾瀹冨瓧绗﹂兘琚鍒跺埌缁撴灉涓紝鏃犻渶浣滃嚭瑙i噴銆
娉ㄦ剰锛 鈥%鈥欏瓧绗﹁姹傚湪鏍煎紡鎸囧畾绗︿箣鍓嶃
鏈堜唤鍜屾棩鏈熻鏄庣鐨勮寖鍥翠粠闆跺紑濮嬶紝鍘熷洜鏄 MySQL鍏佽瀛樺偍璇稿 '2004-00-00'鐨勪笉瀹屽叏鏃ユ湡.
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y');
-> 'Saturday October 1997'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00', '%H:%i:%s');
-> '22:23:00'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00',
'%D %y %a %d %m %b %j');
-> '4th 97 Sat 04 10 Oct 277'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00',
'%H %k %I %r %T %S %w');
-> '22 22 10 10:23:00 PM 22:23:00 00 6'
mysql> SELECT DATE_FORMAT('1999-01-01', '%X %V');
-> '1998 52'
DAY() 鍜DAYOFMONTH()鐨勬剰涔夌浉鍚屻
杩斿洖date 瀵瑰簲鐨勫伐浣滄棩鍚嶇О銆
mysql> SELECT DAYNAME('1998-02-05');
-> '鍛ㄥ洓'
杩斿洖date 瀵瑰簲鐨勮鏈堟棩鏈燂紝鑼冨洿鏄粠 1鍒31銆
mysql> SELECT DAYOFMONTH('1998-02-03');
-> 3
杩斿洖date (1 = 鍛ㄦ棩, 2 = 鍛ㄤ竴, ..., 7 = 鍛ㄥ叚)瀵瑰簲鐨勫伐浣滄棩绱㈠紩銆傝繖浜涚储寮曞肩鍚 ODBC鏍囧噯銆
mysql> SELECT DAYOFWEEK('1998-02-03');
-> 3
杩斿洖date 瀵瑰簲鐨勪竴骞翠腑鐨勫ぉ鏁帮紝鑼冨洿鏄粠 1鍒366銆
mysql> SELECT DAYOFYEAR('1998-02-03');
-> 34
EXTRACT()鍑芥暟鎵浣跨敤鐨勬椂闂撮棿闅旂被鍨嬭鏄庣鍚 DATE_ADD()鎴DATE_SUB()鐨勭浉鍚,浣嗗畠浠庢棩鏈熶腑鎻愬彇鍏堕儴鍒嗭紝鑰屼笉鏄墽琛屾棩鏈熻繍绠椼
mysql> SELECT EXTRACT(YEAR FROM '1999-07-02');
-> 1999
mysql> SELECT EXTRACT(YEAR_MONTH FROM '1999-07-02 01:02:03');
-> 199907
mysql> SELECT EXTRACT(DAY_MINUTE FROM '1999-07-02 01:02:03');
-> 20102
mysql> SELECT EXTRACT(MICROSECOND
-> FROM '2003-01-02 10:30:00.00123');
-> 123
缁欏畾涓涓ぉ鏁 N, 杩斿洖涓涓DATE鍊笺
mysql> SELECT FROM_DAYS(729669);
-> '1997-10-07'
浣跨敤 FROM_DAYS()澶勭悊鍙よ佹棩鏈熸椂锛屽姟蹇呰皑鎱庛備粬涓嶇敤浜庡鐞嗛槼鍘嗗嚭鐜板墠鐨勬棩鏈(1582)銆傝鍙傝12.6鑺傦紝鈥淢ySQL浣跨敤浠涔堟棩鍘嗭紵鈥銆
杩斿洖'YYYY-MM-DD HH:MM:SS'鎴YYYYMMDDHHMMSS 鏍煎紡鍊肩殑unix_timestamp鍙傛暟琛ㄧず锛屽叿浣撴牸寮忓彇鍐充簬璇ュ嚱鏁版槸鍚︾敤鍦ㄥ瓧绗︿覆涓垨鏄暟瀛楄澧冧腑銆
鑻format 宸茬粡缁欏嚭锛屽垯缁撴灉鐨勬牸寮忔槸鏍规嵁format 瀛楃涓茶屽畾銆 format 鍙互鍖呭惈鍚DATE_FORMAT() 鍑芥暟杈撳叆椤瑰垪琛ㄤ腑鐩稿悓鐨勮鏄庣銆
mysql> SELECT FROM_UNIXTIME(875996580);
-> '1997-10-04 22:23:00'
mysql> SELECT FROM_UNIXTIME(875996580) + 0;
-> 19971004222300
mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),
-> '%Y %D %M %h:%i:%s %x');
-> '2003 6th August 06:22:58 2003'
杩斿洖涓涓牸寮忓瓧绗︿覆銆傝繖涓嚱鏁板湪鍚DATE_FORMAT() 鍙STR_TO_DATE()鍑芥暟缁撳悎鏃跺緢鏈夌敤銆
绗竴涓弬鏁扮殑3涓彲鑳藉煎拰绗簩涓弬鏁扮殑5涓彲鑳藉间骇鐢 15 涓彲鑳芥牸寮忓瓧绗︿覆 (瀵逛簬浣跨敤鐨勮鏄庣锛岃鍙傝DATE_FORMAT()鍑芥暟璇存槑琛 )銆
鍑芥暟璋冪敤 |
缁撴灉 |
GET_FORMAT(DATE,'USA') |
'%m.%d.%Y' |
GET_FORMAT(DATE,'JIS') |
'%Y-%m-%d' |
GET_FORMAT(DATE,'ISO') |
'%Y-%m-%d' |
GET_FORMAT(DATE,'EUR') |
'%d.%m.%Y' |
GET_FORMAT(DATE,'INTERNAL') |
'%Y%m%d' |
GET_FORMAT(DATETIME,'USA') |
'%Y-%m-%d-%H.%i.%s' |
GET_FORMAT(DATETIME,'JIS') |
'%Y-%m-%d %H:%i:%s' |
GET_FORMAT(DATETIME,'ISO') |
'%Y-%m-%d %H:%i:%s' |
GET_FORMAT(DATETIME,'EUR') |
'%Y-%m-%d-%H.%i.%s' |
GET_FORMAT(DATETIME,'INTERNAL') |
'%Y%m%d%H%i%s' |
GET_FORMAT(TIME,'USA') |
'%h:%i:%s %p' |
GET_FORMAT(TIME,'JIS') |
'%H:%i:%s' |
GET_FORMAT(TIME,'ISO') |
'%H:%i:%s' |
GET_FORMAT(TIME,'EUR') |
'%H.%i.%S' |
GET_FORMAT(TIME,'INTERNAL') |
'%H%i%s' |
ISO 鏍煎紡涓ISO 9075, 鑰岄潪ISO 8601.
涔熷彲浠ヤ娇鐢TIMESTAMP, 杩欐椂GET_FORMAT()鐨勮繑鍥炲煎拰DATETIME鐩稿悓銆
mysql> SELECT DATE_FORMAT('2003-10-03',GET_FORMAT(DATE,'EUR'));
-> '03.10.2003'
mysql> SELECT STR_TO_DATE('10.31.2003',GET_FORMAT(DATE,'USA'));
-> '2003-10-31'
杩斿洖time 瀵瑰簲鐨勫皬鏃舵暟銆傚浜庢棩鏃跺肩殑杩斿洖鍊艰寖鍥存槸浠 0 鍒 23 銆
mysql> SELECT HOUR('10:05:03');
-> 10
鐒惰, TIME 鍊肩殑鑼冨洿瀹為檯涓婇潪甯稿ぇ, 鎵浠HOUR鍙互杩斿洖澶т簬23鐨勫笺
mysql> SELECT HOUR('272:59:59');
-> 272
鑾峰彇涓涓棩鏈熸垨鏃ユ湡鏃堕棿鍊硷紝杩斿洖璇ユ湀鏈鍚庝竴澶╁搴旂殑鍊笺傝嫢鍙傛暟鏃犳晥锛屽垯杩斿洖NULL銆
mysql> SELECT LAST_DAY('2003-02-05');
-> '2003-02-28'
mysql> SELECT LAST_DAY('2004-02-05');
-> '2004-02-29'
mysql> SELECT LAST_DAY('2004-01-01 01:01:01');
-> '2004-01-31'
mysql> SELECT LAST_DAY('2003-03-32');
-> NULL
LOCALTIME 鍙 LOCALTIME()鍜NOW()鍏锋湁鐩稿悓鎰忎箟銆
LOCALTIMESTAMP鍜LOCALTIMESTAMP()鍜NOW()鍏锋湁鐩稿悓鎰忎箟銆
缁欏嚭骞翠唤鍊煎拰涓骞翠腑鐨勫ぉ鏁鍊硷紝杩斿洖涓涓棩鏈熴dayofyear 蹇呴』澶т簬 0 锛屽惁鍒欑粨鏋滀负 NULL銆
mysql> SELECT MAKEDATE(2001,31), MAKEDATE(2001,32);
-> '2001-01-31', '2001-02-01'
mysql> SELECT MAKEDATE(2001,365), MAKEDATE(2004,365);
-> '2001-12-31', '2004-12-30'
mysql> SELECT MAKEDATE(2001,0);
-> NULL
杩斿洖鐢hour銆 minute鍜second 鍙傛暟璁$畻寰楀嚭鐨勬椂闂村笺
mysql> SELECT MAKETIME(12,15,30);
-> '12:15:30'
浠庢椂闂存垨鏃ユ湡鏃堕棿琛ㄨ揪寮expr杩斿洖寰鍊硷紝鍏舵暟瀛楄寖鍥翠粠 0鍒 999999銆
mysql> SELECT MICROSECOND('12:00:00.123456');
-> 123456
mysql> SELECT MICROSECOND('1997-12-31 23:59:59.000010');
-> 10
杩斿洖 time 瀵瑰簲鐨勫垎閽熸暟,鑼冨洿鏄粠 0 鍒 59銆
mysql> SELECT MINUTE('98-02-03 10:05:03');
-> 5
杩斿洖date 瀵瑰簲鐨勬湀浠斤紝鑼冨洿鏃朵粠 1 鍒 12銆
mysql> SELECT MONTH('1998-02-03');
-> 2
杩斿洖date 瀵瑰簲鏈堜唤鐨勫叏鍚嶃
mysql> SELECT MONTHNAME('1998-02-05');
-> 'February '
杩斿洖褰撳墠鏃ユ湡鍜屾椂闂村硷紝鍏舵牸寮忎负 'YYYY-MM-DD HH:MM:SS' 鎴YYYYMMDDHHMMSS 锛 鍏蜂綋鏍煎紡鍙栧喅浜庤鍑芥暟鏄惁鐢ㄥ湪瀛楃涓蹭腑鎴栨暟瀛楄澧冧腑銆
mysql> SELECT NOW();
-> '1997-12-15 23:50:26'
mysql> SELECT NOW() + 0;
-> 19971215235026
鍦ㄤ竴涓瓨鍌ㄧ▼搴忔垨瑙﹀彂鍣ㄥ唴, NOW() 杩斿洖涓涓父鏁版椂闂达紝璇ュ父鏁版寚绀轰簡璇ョ▼搴忔垨瑙﹀彂璇彞寮濮嬫墽琛岀殑鏃堕棿銆傝繖鍚SYSDATE()鐨勮繍琛屾湁鎵涓嶅悓銆
娣诲姞 N 涓湀鑷冲懆鏈P (鏍煎紡涓YYMM 鎴YYYYMM)锛岃繑鍥炲肩殑鏍煎紡涓 YYYYMM銆傛敞鎰忓懆鏈熷弬鏁 P 涓嶆槸鏃ユ湡鍊笺
mysql> SELECT PERIOD_ADD(9801,2);
-> 199803
杩斿洖鍛ㄦ湡P1鍜 P2 涔嬮棿鐨勬湀浠芥暟銆P1 鍜P2 鐨勬牸寮忓簲璇ヤ负YYMM鎴YYYYMM銆傛敞鎰忓懆鏈熷弬鏁 P1鍜P2 涓嶆槸鏃ユ湡鍊笺
mysql> SELECT PERIOD_DIFF(9802,199703);
-> 11
杩斿洖date 瀵瑰簲鐨勪竴骞翠腑鐨勫搴﹀硷紝鑼冨洿鏄粠 1鍒 4銆
mysql> SELECT QUARTER('98-04-01');
-> 2
杩斿洖time 瀵瑰簲鐨勭鏁, 鑼冨洿鏄粠 0鍒59銆
mysql> SELECT SECOND('10:05:03');
-> 3
杩斿洖琚浆鍖栦负灏忔椂銆 鍒嗛挓鍜岀鏁扮殑seconds鍙傛暟鍊, 鍏舵牸寮忎负 'HH:MM:SS' 鎴HHMMSS锛屽叿浣撴牸寮忔牴鎹鍑芥暟鏄惁鐢ㄥ湪瀛楃涓叉垨鏁板瓧璇涓屽畾銆
mysql> SELECT SEC_TO_TIME(2378);
-> '00:39:38'
mysql> SELECT SEC_TO_TIME(2378) + 0;
-> 3938
杩欐槸DATE_FORMAT() 鍑芥暟鐨勫掕浆銆傚畠鑾峰彇涓涓瓧绗︿覆 str 鍜屼竴涓牸寮忓瓧绗︿覆format銆傝嫢鏍煎紡瀛楃涓插寘鍚棩鏈熷拰鏃堕棿閮ㄥ垎锛屽垯 STR_TO_DATE()杩斿洖涓涓 DATETIME 鍊硷紝 鑻ヨ瀛楃涓插彧鍖呭惈鏃ユ湡閮ㄥ垎鎴栨椂闂撮儴鍒嗭紝鍒欒繑鍥炰竴涓 DATE 鎴TIME鍊笺
str鎵鍖呭惈鐨勬棩鏈熴佹椂闂存垨鏃ユ湡鏃堕棿鍊煎簲璇ュ湪format鎸囩ず鐨勬牸寮忎腑琚粰瀹氥傚浜庡彲鐢ㄥ湪format涓殑璇存槑绗︼紝璇峰弬瑙DATE_FORMAT() 鍑芥暟璇存槑琛ㄣ 鎵鏈夊叾瀹冪殑瀛楃琚愬瓧鑾峰彇锛屽洜姝や笉浼氳瑙i噴銆傝嫢 str 鍖呭惈涓涓潪娉曟棩鏈熴佹椂闂存垨鏃ユ湡鏃堕棿鍊硷紝鍒 STR_TO_DATE()杩斿洖NULL銆傚悓鏃讹紝涓涓潪娉曞间細寮曡捣璀﹀憡銆
瀵规棩鏈熷奸儴鍒嗙殑鑼冨洿妫鏌ュ湪11.3.1鑺傦紝鈥淒ATETIME銆丏ATE鍜孴IMESTAMP绫诲瀷鈥鏈夎缁嗚鏄庛傚叾鎰忎箟鏄,渚嬪, 鍙鍏蜂綋鏃ユ湡閮ㄥ垎鐨勮寖鍥存椂浠 1鍒 31涔嬮棿锛屽垯鍏佽涓涓棩鏈熶腑鐨勫叿浣撴棩鏈熼儴鍒嗗ぇ浜庝竴涓湀涓ぉ鏁板笺傚苟涓旓紝鍏佽鈥滈浂鈥濇棩鏈熸垨甯︽湁0鍊奸儴鍒嗙殑鏃ユ湡銆
mysql> SELECT STR_TO_DATE('00/00/0000', '%m/%d/%Y');
-> '0000-00-00'
mysql> SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');
-> '2004-04-31'
褰撹绗簩涓弬鏁扮殑 INTERVAL鍨嬪紡璋冪敤鏃, SUBDATE()鍜DATE_SUB()鐨勬剰涔夌浉鍚屻傚浜庢湁鍏INTERVAL鍙傛暟鐨勪俊鎭紝 瑙佹湁鍏 DATE_ADD()鐨勮璁恒
mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
-> '1997-12-02'
mysql> SELECT SUBDATE('1998-01-02', INTERVAL 31 DAY);
-> '1997-12-02'
绗簩涓舰寮忓厑璁稿days浣跨敤鏁存暟鍊笺傚湪杩欎簺鎯呭喌涓嬶紝瀹冭绠椾綔鐢辨棩鏈熸垨鏃ユ湡鏃堕棿琛ㄨ揪寮 expr涓彁鍙栫殑澶╂暟銆
mysql> SELECT SUBDATE('1998-01-02 12:00:00', 31);
-> '1997-12-02 12:00:00'
娉ㄦ剰涓嶈兘浣跨敤鏍煎紡 "%X%V" 鏉ュ皢涓涓 year-week 瀛楃涓茶浆鍖栦负涓涓棩鏈燂紝鍘熷洜鏄綋涓涓槦鏈熻法瓒婁竴涓湀浠界晫闄愭椂锛屼竴涓勾鍜屾槦鏈熺殑缁勫悎涓嶈兘鏍囩ず涓涓敮涓鐨勫勾鍜屾湀浠姐傝嫢瑕佸皢year-week杞寲涓轰竴涓棩鏈燂紝鍒欎篃搴旀寚瀹氬叿浣撳伐浣滄棩锛
mysql> select str_to_date('200442 Monday', '%X%V %W');
-> 2004-10-18
SUBTIME()浠expr 涓彁鍙expr2 锛岀劧鍚庤繑鍥炵粨鏋溿expr 鏄竴涓椂闂存垨鏃ユ湡鏃堕棿琛ㄨ揪寮忥紝鑰xpr2 鏄竴涓椂闂磋〃杈惧紡銆
mysql> SELECT SUBTIME('1997-12-31 23:59:59.999999','1 1:1:1.000002');
-> '1997-12-30 22:58:58.999997'
mysql> SELECT SUBTIME('01:00:00.999999', '02:00:00.999998');
-> '-00:59:59.999999'
杩斿洖褰撳墠鏃ユ湡鍜屾椂闂村硷紝鏍煎紡涓'YYYY-MM-DD HH:MM:SS' 鎴YYYYMMDDHHMMSS锛 鍏蜂綋鏍煎紡鏍规嵁鍑芥暟鏄惁鐢ㄥ湪瀛楃涓叉垨鏁板瓧璇鑰屽畾銆
鍦ㄤ竴涓瓨鍌ㄧ▼搴忔垨瑙﹀彂鍣ㄤ腑, SYSDATE()杩斿洖鍏舵墽琛岀殑鏃堕棿, 鑰岄潪瀛樺偍鎴愰兘鎴栬Е鍙戣鍙ュ紑濮嬫墽琛岀殑鏃堕棿銆傝繖涓NOW()鐨勮繍浣滄湁鎵涓嶅悓銆
鎻愬彇涓涓椂闂存垨鏃ユ湡鏃堕棿琛ㄨ揪寮忕殑鏃堕棿閮ㄥ垎锛屽苟灏嗗叾浠ュ瓧绗︿覆褰㈠紡杩斿洖銆
mysql> SELECT TIME('2003-12-31 01:02:03');
-> '01:02:03'
mysql> SELECT TIME('2003-12-31 01:02:03.000123');
-> '01:02:03.000123'
TIMEDIFF() 杩斿洖璧峰鏃堕棿 expr 鍜岀粨鏉熸椂闂expr2 涔嬮棿鐨勬椂闂淬 expr 鍜expr2 涓烘椂闂存垨 date-and-time 琛ㄨ揪寮,涓や釜鐨勭被鍨嬪繀椤讳竴鏍枫
mysql> SELECT TIMEDIFF('2000:01:01 00:00:00',
-> '2000:01:01 00:00:00.000001');
-> '-00:00:00.000001'
mysql> SELECT TIMEDIFF('1997-12-31 23:59:59.000001',
-> '1997-12-30 01:01:01.000002');
-> '46:58:57.999999'
瀵逛簬涓涓崟鍙傛暟,璇ュ嚱鏁板皢鏃ユ湡鎴栨棩鏈熸椂闂磋〃杈惧紡 expr 浣滀负鏃ユ湡鏃堕棿鍊艰繑鍥.瀵逛簬涓や釜鍙傛暟, 瀹冨皢鏃堕棿琛ㄨ揪寮 expr2 娣诲姞鍒版棩鏈熸垨鏃ユ湡鏃堕棿琛ㄨ揪寮 expr 涓紝灏theresult浣滀负鏃ユ湡鏃堕棿鍊艰繑鍥炪
mysql> SELECT TIMESTAMP('2003-12-31');
-> '2003-12-31 00:00:00'
mysql> SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');
-> '2004-01-01 00:00:00'
灏嗘暣鍨嬭〃杈惧紡int_expr 娣诲姞鍒版棩鏈熸垨鏃ユ湡鏃堕棿琛ㄨ揪寮 datetime_expr涓 int_expr 鐨勫崟浣嶈鏃堕棿闂撮殧鍙傛暟缁欏畾锛岃鍙傛暟蹇呴』鏄互涓嬪肩殑鍏朵腑涓涓細 FRAC_SECOND銆SECOND銆 MINUTE銆 HOUR銆 DAY銆 WEEK銆 MONTH銆 QUARTER鎴 YEAR銆
鍙娇鐢ㄦ墍鏄剧ず鐨勫叧閿瘝鎸囧畾Interval鍊硷紝鎴栦娇鐢SQL_TSI_鍓嶇紑銆備緥濡, DAY鎴SQL_TSI_DAY 閮芥槸姝g‘鐨勩
mysql> SELECT TIMESTAMPADD(MINUTE,1,'2003-01-02');
-> '2003-01-02 00:01:00'
mysql> SELECT TIMESTAMPADD(WEEK,1,'2003-01-02');
-> '2003-01-09'
杩斿洖鏃ユ湡鎴栨棩鏈熸椂闂磋〃杈惧紡datetime_expr1 鍜datetime_expr2the 涔嬮棿鐨勬暣鏁板樊銆傚叾缁撴灉鐨勫崟浣嶇敱interval 鍙傛暟缁欏嚭銆interval 鐨勬硶瀹氬煎悓TIMESTAMPADD()鍑芥暟璇存槑涓墍鍒楀嚭鐨勭浉鍚屻
mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
-> 3
mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');
-> -1
鍏朵娇鐢ㄥ拰 DATE_FORMAT()鍑芥暟鐩稿悓, 鐒惰format 瀛楃涓插彲鑳戒粎浼氬寘鍚鐞嗗皬鏃躲佸垎閽熷拰绉掔殑鏍煎紡璇存槑绗︺傚叾瀹冭鏄庣浜х敓涓涓NULL鍊兼垨0銆
鑻time value鍖呭惈涓涓ぇ浜23鐨勫皬鏃堕儴鍒嗭紝鍒 %H 鍜 %k 灏忔椂鏍煎紡璇存槑绗︿細浜х敓涓涓ぇ浜0..23鐨勯氬父鑼冨洿鐨勫笺傚彟涓涓皬鏃舵牸寮忚鏄庣浜х敓灏忔椂鍊兼ā鏁12銆
mysql> SELECT TIME_FORMAT('100:00:00', '%H %k %h %I %l');
-> '100 100 04 04 4'
杩斿洖宸茶浆鍖栦负绉掔殑time鍙傛暟銆
mysql> SELECT TIME_TO_SEC('22:23:00');
-> 80580
mysql> SELECT TIME_TO_SEC('00:39:38');
-> 2378
缁欏畾涓涓棩鏈date, 杩斿洖涓涓ぉ鏁 (浠庡勾浠0寮濮嬬殑澶╂暟 )銆
mysql> SELECT TO_DAYS(950501);
-> 728779
mysql> SELECT TO_DAYS('1997-10-07');
-> 729669
TO_DAYS() 涓嶇敤浜庨槼鍘嗗嚭鐜(1582)鍓嶇殑鍊硷紝鍘熷洜鏄綋鏃ュ巻鏀瑰彉鏃讹紝閬楀け鐨勬棩鏈熶笉浼氳鑰冭檻鍦ㄥ唴銆傝鍙傝12.6鑺傦紝鈥淢ySQL浣跨敤浠涔堟棩鍘嗭紵鈥銆
璇疯浣忥紝 MySQL浣跨敤11.3鑺傦紝鈥滄棩鏈熷拰鏃堕棿绫诲瀷鈥涓殑瑙勫垯灏嗘棩鏈熶腑鐨勪簩浣嶆暟骞翠唤鍊艰浆鍖栦负鍥涗綅銆備緥濡傦紝 '1997-10-07'鍜 '97-10-07' 琚涓哄悓鏍风殑鏃ユ湡:
mysql> SELECT TO_DAYS('1997-10-07'), TO_DAYS('97-10-07');
-> 729669, 729669
瀵逛簬1582 骞翠箣鍓嶇殑鏃ユ湡(鎴栬鍦ㄥ叾瀹冨湴鍖轰负涓嬩竴骞 ), 璇ュ嚱鏁扮殑缁撴灉瀹炰笉鍙潬鐨勩傝瑙12.6鑺傦紝鈥淢ySQL浣跨敤浠涔堟棩鍘嗭紵鈥 銆
鑻ユ棤鍙傛暟璋冪敤锛屽垯杩斿洖涓涓Unix timestamp ('1970-01-01 00:00:00' GMT 涔嬪悗鐨勭鏁) 浣滀负鏃犵鍙锋暣鏁般傝嫢鐢date 鏉ヨ皟鐢UNIX_TIMESTAMP()锛屽畠浼氬皢鍙傛暟鍊间互'1970-01-01 00:00:00' GMT鍚庣殑绉掓暟鐨勫舰寮忚繑鍥炪date 鍙互鏄竴涓DATE 瀛楃涓层佷竴涓 DATETIME瀛楃涓层佷竴涓 TIMESTAMP鎴栦竴涓綋鍦版椂闂寸殑YYMMDD 鎴YYYMMDD鏍煎紡鐨勬暟瀛椼
mysql> SELECT UNIX_TIMESTAMP();
-> 882226357
mysql> SELECT UNIX_TIMESTAMP('1997-10-04 22:23:00');
-> 875996580
褰 UNIX_TIMESTAMP琚敤鍦 TIMESTAMP鍒楁椂, 鍑芥暟鐩存帴杩斿洖鍐呴儴鏃舵埑鍊硷紝 鑰屼笉杩涜浠讳綍闅愬惈鐨 鈥string-to-Unix-timestamp鈥濊浆鍖栥傚亣濡備綘鍚UNIX_TIMESTAMP()浼犻掍竴涓孩鍑烘棩鏈燂紝瀹冧細杩斿洖 0,浣嗚娉ㄦ剰鍙湁鍩烘湰鑼冨洿妫鏌ヤ細琚饱琛 (骞翠唤浠1970 鍒 2037锛 鏈堜唤浠01鍒12,鏃ユ湡浠 01 鍒31)銆
鍋囧浣犳兂瑕佸噺鍘 UNIX_TIMESTAMP() 鍒, 浣犳垨璁稿笇鏈涘垹鍘诲甫绗﹀彿鏁存暟鐨勭粨鏋溿傝鍙傝12.8鑺傦紝鈥淐ast鍑芥暟鍜屾搷浣滅鈥銆
杩斿洖褰撳墠 UTC鏃ユ湡鍊硷紝鍏舵牸寮忎负 'YYYY-MM-DD' 鎴 YYYYMMDD锛屽叿浣撴牸寮忓彇鍐充簬鍑芥暟鏄惁鐢ㄥ湪瀛楃涓叉垨鏁板瓧璇涓
mysql> SELECT UTC_DATE(), UTC_DATE() + 0;
-> '2003-08-14', 20030814
杩斿洖褰撳墠 UTC 鍊硷紝鍏舵牸寮忎负 'HH:MM:SS' 鎴HHMMSS锛屽叿浣撴牸寮忔牴鎹鍑芥暟鏄惁鐢ㄥ湪瀛楃涓叉垨鏁板瓧璇鑰屽畾銆
mysql> SELECT UTC_TIME(), UTC_TIME() + 0;
-> '18:07:53', 180753
杩斿洖褰撳墠UTC鏃ユ湡鍙婃椂闂村硷紝鏍煎紡涓 'YYYY-MM-DD HH:MM:SS' 鎴YYYYMMDDHHMMSS锛屽叿浣撴牸寮忔牴鎹鍑芥暟鏄惁鐢ㄥ湪瀛楃涓叉垨鏁板瓧璇鑰屽畾銆
mysql> SELECT UTC_TIMESTAMP(), UTC_TIMESTAMP() + 0;
-> '2003-08-14 18:08:04', 20030814180804
璇ュ嚱鏁拌繑鍥date 瀵瑰簲鐨勬槦鏈熸暟銆WEEK() 鐨勫弻鍙傛暟褰㈠紡鍏佽浣犳寚瀹氳鏄熸湡鏄惁璧峰浜庡懆鏃ユ垨鍛ㄤ竴锛 浠ュ強杩斿洖鍊肩殑鑼冨洿鏄惁涓轰粠0 鍒53 鎴栦粠1 鍒53銆傝嫢 mode鍙傛暟琚渷鐣ワ紝鍒欎娇鐢default_week_format绯荤粺鑷彉閲忕殑鍊笺傝鍙傝5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥銆
浠ヤ笅琛ㄨ鏄庝簡mode 鍙傛暟鐨勫伐浣滆繃绋嬶細d
|
绗竴澶 |
|
|
Mode |
宸ヤ綔鏃 |
鑼冨洿 |
Week 1 涓虹涓鍛 ... |
0 |
鍛ㄦ棩 |
0-53 |
鏈勾搴︿腑鏈変竴涓懆鏃 |
1 |
鍛ㄤ竴 |
0-53 |
鏈勾搴︿腑鏈3澶╀互涓 |
2 |
鍛ㄦ棩 |
1-53 |
鏈勾搴︿腑鏈変竴涓懆鏃 |
3 |
鍛ㄤ竴 |
1-53 |
鏈勾搴︿腑鏈3澶╀互涓 |
4 |
鍛ㄦ棩 |
0-53 |
鏈勾搴︿腑鏈3澶╀互涓 |
5 |
鍛ㄤ竴 |
0-53 |
鏈勾搴︿腑鏈変竴涓懆涓 |
6 |
鍛ㄦ棩 |
1-53 |
鏈勾搴︿腑鏈3澶╀互涓 |
7 |
鍛ㄤ竴 |
1-53 |
鏈勾搴︿腑鏈変竴涓懆涓 |
mysql> SELECT WEEK('1998-02-20');
-> 7
mysql> SELECT WEEK('1998-02-20',0);
-> 7
mysql> SELECT WEEK('1998-02-20',1);
-> 8
mysql> SELECT WEEK('1998-12-31',1);
-> 53
娉ㄦ剰锛屽亣濡傛湁涓涓棩鏈熶綅浜庡墠涓骞寸殑鏈鍚庝竴鍛紝 鑻ヤ綘涓嶄娇鐢2銆3銆6鎴7浣滀负mode 鍙傛暟閫夋嫨锛屽垯MySQL杩斿洖 0锛
mysql> SELECT YEAR('2000-01-01'), WEEK('2000-01-01',0);
-> 2000, 0
鏈変汉鎴栬浼氭彁鍑烘剰瑙侊紝璁や负 MySQL 瀵逛簬WEEK() 鍑芥暟搴旇杩斿洖 52 锛屽師鍥犳槸缁欏畾鐨勬棩鏈熷疄闄呬笂鍙戠敓鍦1999骞寸殑绗52鍛ㄣ傛垜浠喅瀹氳繑鍥0浣滀负浠f浛鐨勫師鍥犳槸鎴戜滑甯屾湜璇ュ嚱鏁拌兘杩斿洖鈥滅粰瀹氬勾浠界殑鏄熸湡鏁扳濄傝繖浣垮緱WEEK() 鍑芥暟鍦ㄥ悓鍏跺畠浠庢棩鏈熶腑鎶藉彇鏃ユ湡閮ㄥ垎鐨勫嚱鏁扮粨鍚堟椂鐨勪娇鐢ㄦ洿鍔犲彲闈犮
鍋囧浣犳洿甯屾湜鎵璁$畻鐨勫叧浜庡勾浠界殑缁撴灉鍖呮嫭缁欏畾鏃ユ湡鎵鍦ㄥ懆鐨勭涓澶╋紝鍒欏簲浣跨敤 0銆2銆5鎴 7 浣滀负mode鍙傛暟閫夋嫨銆
mysql> SELECT WEEK('2000-01-01',2);
-> 52
浣滀负閫夋嫨锛屽彲浣跨敤 YEARWEEK()鍑芥暟:
mysql> SELECT YEARWEEK('2000-01-01');
-> 199952
mysql> SELECT MID(YEARWEEK('2000-01-01'),5,2);
-> '52'
杩斿洖date (0 = 鍛ㄤ竴, 1 = 鍛ㄤ簩, ... 6 = 鍛ㄦ棩)瀵瑰簲鐨勫伐浣滄棩绱㈠紩 weekday index for
mysql> SELECT WEEKDAY('1998-02-03 22:23:00');
-> 1
mysql> SELECT WEEKDAY('1997-11-05');
-> 2
灏嗚鏃ユ湡鐨勯槼鍘嗗懆浠ユ暟瀛楀舰寮忚繑鍥烇紝鑼冨洿鏄粠1鍒53銆傚畠鏄竴涓吋瀹瑰害鍑芥暟锛岀浉褰撲簬WEEK(date,3)銆
mysql> SELECT WEEKOFYEAR('1998-02-20');
-> 8
杩斿洖date 瀵瑰簲鐨勫勾浠,鑼冨洿鏄粠1000鍒9999銆
mysql> SELECT YEAR('98-02-03');
-> 1998
杩斿洖涓涓棩鏈熷搴旂殑骞存垨鍛ㄣstart鍙傛暟鐨勫伐浣滃悓 start鍙傛暟瀵 WEEK()鐨勫伐浣滅浉鍚屻傜粨鏋滀腑鐨勫勾浠藉彲浠ュ拰璇ュ勾鐨勭涓鍛ㄥ拰鏈鍚庝竴鍛ㄥ搴旂殑鏃ユ湡鍙傛暟鏈夋墍涓嶅悓銆
mysql> SELECT YEARWEEK('1987-01-01');
-> 198653
娉ㄦ剰锛屽懆鏁板拰WEEK()鍑芥暟闃熷彲閫夊弬鏁0鎴 1鍙兘浼氳繑鍥炵殑(0) w鏈夋墍涓嶅悓锛屽師鍥犳槸姝ゆ椂 WEEK() 杩斿洖缁欏畾骞翠唤鐨勮澧冧腑鐨勫懆銆
-> '1997-10-07'
MySQL 浣跨敤閫氬父鎵璇寸殑 proleptic 闃冲巻銆
姣忎釜灏嗘棩鍘嗙敱鏈卞埄瀹鏀逛负闃冲巻鐨勫浗瀹跺湪鏀瑰彉鏃ュ巻鏈熼棿閮戒笉寰椾笉鍒犻櫎鑷冲皯10澶┿ 涓轰簡浜嗚В鍏惰繍浣滐紝璁╂垜浠湅鐪1582骞10鏈堬紝杩欐槸鐢鏈卞埄瀹鏃ュ巻杞崲涓洪槼鍘嗙殑绗竴娆:
鍛ㄤ竴 |
鍛ㄤ簩 |
鍛ㄤ笁 |
鍛ㄥ洓 |
鍛ㄤ簲 |
鍛ㄥ叚 |
鍛ㄦ棩 |
1 |
2 |
3 |
4 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
鍦10鏈4 鏃ュ埌10鏈15鏃ヤ箣闂寸殑鏃ユ湡涓虹┖鐧姐傝繖涓腑鏂绉颁负鎺ュ叆銆傛帴鍏ュ墠鐨勬棩鏈熷潎浣跨敤鏈卞埄瀹鏃ュ巻, 鑰屾帴鍏ュ悗鐨勬棩鏈熷潎浣跨敤闃冲巻銆傛帴鍏ユ湡闂寸殑鏃ユ湡鏄笉瀛樺湪鐨勩
褰撲竴涓敤浜庢棩鏈熺殑鏃ュ巻骞朵负寰楀埌瀹為檯浣跨敤鏃惰绉颁负 proleptic銆傚洜姝, 鑻ユ垜浠亣璁句粠鏉ユ病鏈夋帴鍏ユ湡鐨勫瓨鍦紝鑰岄槼鍘嗗巻娉曞垯濮嬬粓琚娇鐢紝鎴戜滑浼氭湁涓涓鏈熺殑闃冲巻 銆傝繖灏辨槸MySQL 鎵浣跨敤鐨勶紝姝e鏍囧噯SQL鎵瑕佹眰鐨勩 閴翠簬杩欎釜鍘熷洜锛屼綔涓MySQL DATE 鎴 DATETIME鍊艰岃鍌ㄥ瓨鐨勬帴鍏ュ墠鐨勬棩鏈熷繀椤昏皟鏁磋繖涓樊寮傘傛垜浠繀椤绘槑鐧斤紝鎺ュ叆鐨勫彂鐢熸椂闂村湪涓嶅悓鐨勫浗瀹舵湁鎵涓嶅悓锛岃屾帴鍏ョ殑鏃堕棿瓒婃櫄锛岄仐澶辩殑鏃ユ湡瓒婂銆備緥濡傦紝鍦ㄥぇ涓嶅垪棰, 鎺ュ叆鍙戠敓鍦 1752骞,杩欐椂9鏈2鏃ワ紝鍛ㄤ笁鍚庣殑绗簩澶╀负9鏈14鏃ワ紝鍛ㄤ簩锛 淇勭綏鏂粨鏉熶娇鐢鏈卞埄瀹鏃ュ巻鐨勬椂闂翠负1918骞,鎺ュ叆杩囩▼涓仐澶卞ぉ鏁颁负 13澶, 鏍规嵁闃冲巻锛屽叾鏅亶琚О涓衡10鏈堥潻鍛解濈殑鍙戠敓鏃堕棿瀹為檯涓婃槸11鏈堛
MySQL鏀寔鍏ㄦ枃绱㈠紩鍜屾悳绱㈠姛鑳姐MySQL涓殑鍏ㄦ枃绱㈠紩绫诲瀷FULLTEXT鐨勭储寮曘 FULLTEXT 绱㈠紩浠呭彲鐢ㄤ簬 MyISAM 琛紱浠栦滑鍙互浠CHAR銆 VARCHAR鎴TEXT鍒椾腑浣滀负CREATE TABLE璇彞鐨勪竴閮ㄥ垎琚垱寤猴紝鎴栨槸闅忓悗浣跨敤ALTER TABLE 鎴 CREATE INDEX琚坊鍔犮傚浜庤緝澶х殑鏁版嵁闆嗭紝灏嗕綘鐨勮祫鏂欒緭鍏ヤ竴涓病鏈FULLTEXT绱㈠紩鐨勮〃涓紝鐒跺悗鍒涘缓绱㈠紩锛 鍏堕熷害姣旀妸璧勬枡杈撳叆鐜版湁FULLTEXT绱㈠紩鐨勯熷害鏇翠负蹇
鍏充簬鍏ㄦ枃鎼滅储鐨勯檺鍒跺垪琛紝璇峰弬瑙 12.7.4鑺傦紝鈥滃叏鏂囬檺瀹氭潯浠垛.
鍏ㄦ枃鎼滅储鍚MATCH()鍑芥暟涓璧锋墽琛屻
mysql> CREATE TABLE articles (
-> id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
-> title VARCHAR(200),
-> body TEXT,
-> FULLTEXT (title,body)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO articles (title,body) VALUES
-> ('MySQL Tutorial','DBMS stands for DataBase ...'),
-> ('How To Use MySQL Well','After you went through a ...'),
-> ('Optimizing MySQL','In this tutorial we will show ...'),
-> ('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
-> ('MySQL vs. YourSQL','In the following database comparison ...'),
-> ('MySQL Security','When configured properly, MySQL ...');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM articles
-> WHERE MATCH (title,body) AGAINST ('database');
+----+-------------------+------------------------------------------+
| id | title | body |
+----+-------------------+------------------------------------------+
| 5 | MySQL vs. YourSQL | In the following database comparison ... |
| 1 | MySQL Tutorial | DBMS stands for DataBase ... |
+----+-------------------+------------------------------------------+
2 rows in set (0.00 sec)
MATCH()鍑芥暟瀵逛簬涓涓瓧绗︿覆鎵ц璧勬枡搴撳唴鐨勮嚜鐒惰瑷鎼滅储銆備竴涓祫鏂欏簱灏辨槸1濂1涓垨2涓寘鍚湪FULLTEXT鍐呯殑鍒椼傛悳绱㈠瓧绗︿覆浣滀负瀵AGAINST()鐨勫弬鏁拌岃缁欏畾銆傚浜庤〃涓殑姣忎竴琛, MATCH() 杩斿洖涓涓浉鍏冲硷紝鍗, 鎼滅储瀛楃涓插拰 MATCH()琛ㄤ腑鎸囧畾鍒椾腑璇ヨ鏂囧瓧涔嬮棿鐨勪竴涓浉浼兼у害閲忋
鍦ㄩ粯璁ょ姸鎬佷笅, 鎼滅储鐨勬墽琛屾柟寮忎负涓嶅尯鍒嗗ぇ灏忓啓鏂瑰紡銆傜劧鑰,浣犲彲浠ラ氳繃瀵圭紪鍏ョ储寮曠殑鍒椾娇鐢ㄤ簩杩涘埗鎺掑簭鏂瑰紡鎵ц鍖哄垎澶у皬鍐欑殑鍏ㄦ枃鎼滅储銆 渚嬪锛屽彲浠ュ悜涓涓娇鐢latin1瀛楃闆嗙殑鍒楃粰瀹latin1_bin 鐨勬帓搴忔柟寮忥紝瀵逛簬鍏ㄦ枃鎼滅储鍖哄垎澶у皬鍐欍
濡備笂杩版墍涓句緥瀛愶紝褰MATCH()琚敤鍦ㄤ竴涓 WHERE 璇彞涓椂锛岀浉鍏冲兼槸闈炶礋娴偣鏁般傞浂鐩稿叧鐨勬剰鎬濇槸娌℃湁鐩镐技鎬с傜浉鍏虫х殑璁$畻鏄熀浜庤琛屼腑鍗曡瘝鐨勬暟鐩, 璇ヨ涓嫭鐗瑰瓙鐨勬暟鐩紝璧勬枡搴撲腑鍗曡瘝鐨勬绘暟锛屼互鍙婂寘鍚壒娈婅瘝鐨勬枃浠(琛)鏁扮洰銆
瀵逛簬鑷劧璇█鍏ㄦ枃鎼滅储锛岃姹MATCH() 鍑芥暟涓懡鍚嶇殑鍒楀拰浣犵殑琛ㄤ腑涓浜FULLTEXT绱㈠紩涓寘鍚殑鍒楃浉鍚屻傚浜庡墠杩伴棶璁紝 娉ㄦ剰锛MATCH()鍑芥暟(棰樼洰鍙婂叏鏂)涓墍鍛藉悕鐨勫垪鍜屾枃绔犺〃鐨FULLTEXT绱㈠紩涓殑鍒楃浉鍚屻傝嫢瑕佸垎鍒悳绱㈤鐩拰鍏ㄦ枃锛屽簲璇ュ姣忎釜鍒楀垱寤FULLTEXT绱㈠紩銆
鎴栬呬篃鍙互杩愯甯冨皵鎼滅储鎴栦娇鐢ㄦ煡璇㈡墿灞曡繘琛屾悳绱€傚叧浜庤繖浜涙悳绱㈢被鍨嬬殑璇存槑瑙12.7.1鑺傦紝鈥滃竷灏斿叏鏂囨悳绱⑩鍜12.7.2鑺傦紝鈥滃叏鏂囨悳绱㈠甫鏌ヨ鎵╁睍鈥銆
涓婇潰鐨勪緥瀛愬熀鏈笂灞曠ず浜嗘庢牱浣跨敤杩斿洖琛岀殑鐩稿叧鎬ч『搴忔笎寮辩殑MATCH()鍑芥暟銆傝屼笅闈㈢殑渚嬪瓙鍒欏睍绀轰簡鎬庢牱鏄庣‘鍦版绱㈢浉鍏冲笺傝繑鍥炶鐨勯『搴忔槸涓嶅畾鐨勶紝鍘熷洜鏄 SELECT 璇彞涓嶅寘鍚 WHERE鎴ORDER BY 瀛愬彞锛
mysql> SELECT id, MATCH (title,body) AGAINST ('Tutorial')
-> FROM articles;
+----+-----------------------------------------+
| id | MATCH (title,body) AGAINST ('Tutorial') |
+----+-----------------------------------------+
| 1 | 0.65545833110809 |
| 2 | 0 |
| 3 | 0.66266459226608 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
+----+-----------------------------------------+
6 rows in set (0.00 sec)
涓嬮潰鐨勪緥瀛愬垯鏇村姞澶嶆潅銆傝闂繑鍥炵浉鍏冲硷紝鍚屾椂瀵硅鎸夌収鐩稿叧鎬ф笎寮辩殑椤哄簭杩涜鎺掑簭銆備负瀹炵幇杩欎釜缁撴灉锛屼綘搴旇涓ゆ鎸囧畾 MATCH()锛 涓娆″湪 SELECT 鍒楄〃涓屽彟涓娆″湪 WHERE瀛愬彞涓傝繖涓嶄細寮曡捣棰濆鐨勫唴鍔℃搷浣滐紝鍘熷洜鏄MySQL 浼樺寲绋嬪簭娉ㄦ剰鍒颁袱涓MATCH()璋冪敤鏄浉鍚岀殑锛屼粠鑰屽彧浼氭縺娲讳竴娆″叏鏂囨悳绱唬鐮併
mysql> SELECT id, body, MATCH (title,body) AGAINST
-> ('Security implications of running MySQL as root') AS score
-> FROM articles WHERE MATCH (title,body) AGAINST
-> ('Security implications of running MySQL as root');
+----+-------------------------------------+-----------------+
| id | body | score |
+----+-------------------------------------+-----------------+
| 4 | 1. Never run mysqld as root. 2. ... | 1.5219271183014 |
| 6 | When configured properly, MySQL ... | 1.3114095926285 |
+----+-------------------------------------+-----------------+
2 rows in set (0.00 sec)
琛ㄤ腑鏈2琛(0.00 绉)
MySQL FULLTEXT 鎵ц灏嗕换浣曞崟瀛楀瓧绗﹀師褰 (瀛楁瘝銆佹暟瀛楀拰涓嬪垝绾块儴鍒)鐨勫簭鍒楄涓轰竴涓崟璇嶃傝繖涓簭鍒楁垨璁镐篃鍖呭惈鍗曞紩鍙 ('),浣嗗湪涓琛屼腑涓嶄細瓒呰繃涓涓 杩欐剰鍛崇潃 aaa'bbb 浼氳瑙嗕负涓涓崟璇嶏紝鑰 aaa''bbb鍒欒瑙嗕负2涓崟璇嶃備綅浜庡崟璇嶄箣鍓嶆垨鍏跺悗鐨勫崟寮曞彿浼氳FULLTEXT鍒嗘瀽绋嬪簭鍘绘帀锛 'aaa'bbb' 浼氬彉鎴 aaa'bbb銆
FULLTEXT鍒嗘瀽绋嬪簭浼氶氳繃瀵绘壘鏌愪簺鍒嗛殧绗︽潵纭畾鍗曡瘝鐨勮捣濮嬩綅缃拰缁撴潫浣嶇疆锛屼緥濡' ' (闂撮殧绗﹀彿)銆 , (閫楀彿)浠ュ強 . (鍙ュ彿 )銆傚亣濡傚崟璇嶆病鏈夎鍒嗛殧绗﹀垎寮锛(渚嬪鍦ㄤ腑鏂囬噷 ), 鍒 FULLTEXT 鍒嗘瀽绋嬪簭涓嶈兘纭畾涓涓瘝鐨勮捣濮嬩綅缃拰缁撴潫浣嶇疆銆備负浜嗚兘澶熷湪杩欐牱鐨勮瑷涓悜FULLTEXT 绱㈠紩娣诲姞鍗曡瘝鎴栧叾瀹冪紪鍏ョ储寮曠殑鏈锛屼綘蹇呴』瀵瑰畠浠繘琛岄澶勭悊锛屼娇鍏惰涓浜涜濡"涔嬬被鐨勪换鎰忓垎闅旂鍒嗛殧寮銆
涓浜涜瘝鍦ㄥ叏鏂囨悳绱腑浼氳蹇界暐锛
榛樿鐨勫仠姝㈠瓧鍦12.7.3鑺傦紝鈥滃叏鏂囧仠姝㈠瓧鈥涓缁欏嚭銆傞粯璁ょ殑鏈灏忓崟璇嶉暱搴﹀拰 鍋滄瀛楀彲浠ヨ鏀瑰彉锛屽12.7.5鑺傦紝鈥滃井璋僊ySQL鍏ㄦ枃鎼滅储鈥涓墍杩般
璇嶅簱鍜岃闂腑姣忎竴涓纭殑鍗曡瘝鏍规嵁鍏跺湪璇嶅簱鍜岃闂腑鐨勯噸瑕佹ц岃琛¢噺銆 閫氳繃杩欑鏂瑰紡锛屼竴涓嚭鐜板湪璁稿鏂囦欢涓殑鍗曡瘝鍏锋湁杈冧綆鐨勯噸瑕佹(鑰屼笖鐢氳嚦寰堝鍗曡瘝鐨勯噸瑕佹т负闆),鍘熷洜鏄湪杩欎釜鐗瑰埆璇嶅簱涓叾璇箟浠峰艰緝浣庛傚弽涔嬶紝鍋囧杩欎釜鍗曡瘝姣旇緝灏戣锛岄偅涔堝畠浼氬緱鍒颁竴涓緝楂樼殑閲嶈鎬с傜劧鍚庡崟璇嶇殑閲嶈鎬ц缁勫悎锛屼粠鑰岀敤鏉ヨ绠楄琛岀殑鐩稿叧鎬с
杩欓」鎶鏈渶閫傚悎鍚屽ぇ鍨嬭瘝搴撲竴璧蜂娇鐢 (浜嬪疄涓, 姝ゆ椂瀹冪粡杩囦粩缁嗙殑璋冩暣 )銆傚浜庡緢灏忕殑琛紝鍗曡瘝鍒嗗竷骞朵笉鑳藉厖鍒嗗弽鏄犲畠浠殑璇箟浠峰硷紝 鑰岃繖涓ā寮忔湁鏃跺彲鑳戒細浜х敓濂囩壒鐨勭粨鏋溿備緥濡, 铏界劧鍗曡瘝 鈥MySQL鈥 鍑虹幇鍦ㄦ枃绔犺〃涓殑姣忎竴琛岋紝浣嗗杩欎釜璇嶇殑鎼滅储鍙兘寰椾笉鍒颁换浣曠粨鏋滐細
mysql> SELECT * FROM articles
-> WHERE MATCH (title,body) AGAINST ('MySQL');
鎵句笉鍒版悳绱㈢殑璇(0.00 绉)
杩欎釜鎼滅储鐨勭粨鏋滀负绌猴紝鍘熷洜鏄崟璇 鈥MySQL鈥 鍑虹幇鍦ㄨ嚦灏戝叏鏂囩殑50%鐨勮涓 鍥犳, 瀹冭鍒楀叆鍋滄瀛椼傚浜庡ぇ鍨嬫暟鎹泦锛屼娇鐢ㄨ繖涓搷浣滄渶鍚堥備笉杩囦簡----涓涓嚜鐒惰瑷闂涓嶄細浠庝竴涓1GB 鐨勮〃姣忛殧涓琛岃繑鍥炰竴娆°傚浜庡皬鍨嬫暟鎹泦锛屽畠鐨勭敤澶勫彲鑳芥瘮杈冨皬銆
涓涓鍚堣〃涓墍鏈夎鐨勫唴瀹圭殑涓鍗婄殑鍗曡瘝鏌ユ壘鐩稿叧鏂囨。鐨勫彲鑳芥ц緝灏忋備簨瀹炰笂, 瀹冩洿瀹规槗鎵惧埌寰堝涓嶇浉鍏崇殑鍐呭銆傛垜浠兘鐭ラ亾锛屽綋鎴戜滑鍦ㄥ洜鐗圭綉涓婅瘯鍥句娇鐢ㄦ悳绱㈠紩鎿庡鎵捐祫鏂欑殑鏃跺欙紝杩欑鎯呭喌鍙戠敓鐨勯鐜囬楂樸傚彲浠ユ帹璁猴紝鍖呭惈璇ュ崟璇嶇殑琛屽洜鍏舵墍鍦ㄧ壒鍒暟鎹泦 鑰岃璧嬩簣杈冧綆鐨勮涔変环鍊笺 涓涓粰瀹氱殑璇嶆湁鍙兘鍦ㄤ竴涓暟鎹泦涓嫢鏈夎秴杩囧叾50%鐨勫煙鍊硷紝鑰屽湪鍙︿竴涓暟鎹泦鍗翠笉鐒躲
褰撲綘绗竴娆″皾璇曚娇鐢ㄥ叏鏂囨悳绱互浜嗚В鍏跺伐浣滆繃绋嬫椂锛岃繖涓50% 鐨勫煙鍊兼彁渚涢噸瑕佺殑钑存兜鎿嶄綔锛氳嫢浣犲垱寤轰簡涓涓〃锛屽苟涓斿彧灏嗘枃绔犵殑1銆2琛屾彃鍏ュ叾涓紝 鑰屾枃涓殑姣忎釜鍗曡瘝鍦ㄦ墍鏈夎涓嚭鐜扮殑鏈虹巼鑷冲皯涓 50% 銆傞偅涔堢粨鏋滄槸浣犱粈涔堜篃涓嶄細鎼滅储鍒般備竴瀹氳鎻掑叆鑷冲皯3琛岋紝骞朵笖澶氬鐩婂杽銆傞渶瑕佺粫杩囪50% 闄愬埗鐨勭敤鎴峰彲浣跨敤甯冨皵鎼滅储浠g爜锛涜12.7.1鑺傦紝鈥滃竷灏斿叏鏂囨悳绱⑩銆
鍒╃敤IN BOOLEAN MODE淇敼绋嬪簭锛 MySQL 涔熷彲浠ユ墽琛屽竷灏斿叏鏂囨悳绱細
mysql> SELECT * FROM articles WHERE MATCH (title,body)
-> AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);
+----+-----------------------+-------------------------------------+
| id | title | body |
+----+-----------------------+-------------------------------------+
| 1 | MySQL Tutorial | DBMS stands for DataBase ... |
| 2 | How To Use MySQL Well | After you went through a ... |
| 3 | Optimizing MySQL | In this tutorial we will show ... |
| 4 | 1001 MySQL Tricks | 1. Never run mysqld as root. 2. ... |
| 6 | MySQL Security | When configured properly, MySQL ... |
+----+-----------------------+-------------------------------------+
杩欎釜闂妫绱㈡墍鏈夊寘鍚崟璇嶁MySQL鈥濈殑琛岋紝浣涓妫绱㈠寘鍚崟璇嶁YourSQL鈥濈殑琛屻
甯冨皵鍏ㄦ枃鎼滅储鍏锋湁浠ヤ笅鐗圭偣锛
甯冨皵鍏ㄦ枃鎼滅储鐨勬ц兘鏀寔浠ヤ笅鎿嶄綔绗︼細
涓涓墠瀵肩殑鍔犲彿琛ㄧず璇ュ崟璇蹇呴』 鍑虹幇鍦ㄨ繑鍥炵殑姣忎竴琛岀殑寮澶翠綅缃
涓涓墠瀵肩殑鍑忓彿琛ㄧず璇ュ崟璇涓瀹氫笉鑳鍑虹幇鍦ㄤ换浣曡繑鍥炵殑琛屼腑銆
鍦ㄩ粯璁ょ姸鎬佷笅(褰撴病鏈夋寚瀹 + 鎴栤撶殑鎯呭喌涓)锛岃鍗曡瘝鍙湁鍙棤锛屼絾鍚湁璇ュ崟璇嶇殑琛岀瓑绾ц緝楂樸傝繖鍜MATCH() ... AGAINST()涓嶄娇鐢IN BOOLEAN MODE淇敼绋嬪簭鏃剁殑杩愪綔寰堢被浼笺
杩欎袱涓搷浣滅鐢ㄦ潵鏀瑰彉涓涓崟璇嶅璧嬩簣鏌愪竴琛岀殑鐩稿叧鍊肩殑褰卞搷銆 > 鎿嶄綔绗﹀寮哄叾褰卞搷锛岃 <鎿嶄綔绗﹀垯鍑忓急鍏跺奖鍝嶃傝鍙傝涓嬮潰鐨勪緥瀛愩
鎷彿鐢ㄦ潵灏嗗崟璇嶅垎鎴愬瓙琛ㄨ揪寮忋傛嫭鍏ユ嫭鍙风殑閮ㄥ垎鍙互琚祵濂椼
涓涓墠瀵肩殑浠e瓧鍙风敤浣滃惁瀹氱锛 鐢ㄦ潵鍚﹀畾鍗曡瘝瀵硅琛岀浉鍏虫х殑褰卞搷銆 杩欏浜庢爣璁扳noise锛堟棤鐢ㄤ俊鎭級鈥濈殑鍗曡瘝寰堟湁鐢ㄣ傚寘鍚繖绫诲崟璇嶇殑琛岃緝鍏跺畠琛岀瓑绾т綆锛屼絾鍥犲叾鍙兘浼氬拰-鍙峰悓鏃朵娇鐢紝鍥犺屼笉浼氬湪浠讳綍鏃跺欓兘娲惧嚭鎵鏈夋棤鐢ㄤ俊鎭銆
鏄熷彿鐢ㄤ綔鎴柇绗︺備簬鍏跺畠绗﹀彿涓嶅悓鐨勬槸锛屽畠搴斿綋琚杩藉姞鍒拌鎴柇鐨勮瘝涓娿
涓涓鎷叆鍙屽紩鍙风殑鐭 (鈥"鈥) 鍙拰瀛楅潰涓鍖呭惈璇ョ煭璇杈撳叆鏍煎紡鐨勮杩涜鍖归厤銆傚叏鏂囧紩鎿庡皢鐭鎷嗗垎鎴愬崟璇嶏紝鍦FULLTEXT绱㈠紩涓悳绱㈣鍗曡瘝銆 闈炲崟璇嶅瓧绗︿笉闇瑕佷弗瀵嗙殑鍖归厤锛氱煭璇悳绱㈠彧瑕佹眰绗﹀悎鎼滅储鐭鍖呭惈鐨勫崟璇嶄笖鍗曡瘝鐨勬帓鍒楅『搴忕浉鍚岀殑鍐呭銆備緥濡傦紝 "test phrase" 绗﹀悎 "test, phrase"銆
鑻ョ储寮曚腑涓嶅瓨鍦ㄨ鐭鍖呭惈鐨勫崟璇嶏紝鍒欑粨鏋滀负绌恒備緥濡傦紝鑻ユ墍鏈夊崟璇嶉兘鏄鐢ㄨ瘝锛屾垨鏄暱搴﹂兘灏忎簬缂栧叆绱㈠紩鍗曡瘝鐨勬渶灏忛暱搴︼紝鍒欑粨鏋滀负绌恒
浠ヤ笅渚嬪瓙灞曠ず浜嗕竴浜涗娇鐢ㄥ竷灏斿叏鏂囩鍙风殑鎼滅储瀛楃涓诧細
瀵绘壘鍖呭惈鑷冲皯涓や釜鍗曡瘝涓殑涓涓殑琛屻
瀵绘壘涓や釜鍗曡瘝閮藉寘鍚殑琛屻
瀵绘壘鍖呭惈鍗曡瘝鈥apple鈥濈殑琛岋紝鑻ヨ繖浜涜涔熷寘鍚崟璇嶁macintosh鈥濓紝 鍒欏垪涓烘洿楂樼瓑绾с
瀵绘壘鍖呭惈鍗曡瘝鈥apple鈥 浣嗕笉鍖呭惈鍗曡瘝 鈥macintosh鈥濈殑琛屻
瀵绘壘鍖呭惈鍗曡瘝鈥apple鈥濆拰鈥turnover鈥 鐨勮锛屾垨鍖呭惈鈥apple鈥 鍜屸strudel鈥濈殑琛 (鏃犲厛鍚庨『搴),鐒惰屽寘鍚 鈥apple turnover鈥濈殑琛岃緝鍖呭惈鈥apple strudel鈥濈殑琛屾帓鍒楃瓑绾ф洿涓洪珮銆
瀵绘壘鍖呭惈鈥apple鈥濄佲apples鈥濄佲applesauce鈥濇垨鈥applet鈥濈殑琛屻
瀵绘壘鍖呭惈鍘熺煭璇some words鈥濈殑琛 (渚嬪,鍖呭惈鈥some words of wisdom鈥 鐨勮锛岃岄潪鍖呭惈 鈥some noise words鈥濈殑琛)銆傛敞鎰忓寘鍥磋瘝缁勭殑鈥"鈥 绗﹀彿鏄晫瀹氱煭璇殑鎿嶄綔绗﹀瓧绗︺傚畠浠笉鏄寘鍥存悳绱㈠瓧绗︿覆鏈韩鐨勫紩鍙枫
鍏ㄦ枃鎼滅储鏀寔鏌ヨ鎵╁睍鍔熻兘 (鐗瑰埆鏄叾澶氬彉鐨勨滅洸鏌ヨ鎵╁睍鍔熻兘鈥 )銆傝嫢鎼滅储鐭鐨勯暱搴﹁繃鐭, 閭d箞鐢ㄦ埛鍒欓渶瑕佷緷闈犲叏鏂囨悳绱㈠紩鎿庨氬父缂轰箯鐨鍐呴殣鐭ヨ瘑杩涜鏌ヨ銆傝繖鏃讹紝鏌ヨ鎵╁睍鍔熻兘閫氬父寰堟湁鐢ㄣ備緥濡, 鏌愪綅鎼滅储 鈥database鈥 涓璇嶇殑鐢ㄦ埛锛屽彲鑳借涓衡MySQL鈥濄佲Oracle鈥濄佲DB2鈥 and 鈥RDBMS鈥濆潎涓虹鍚 鈥databases鈥濈殑椤癸紝鍥犳閮藉簲琚繑鍥炪傝繖鏃负鍐呴殣鐭ヨ瘑銆
鍦ㄤ笅鍒楁悳绱㈢煭璇悗娣诲姞WITH QUERY EXPANSION锛屾縺娲荤洸鏌ヨ鎵╁睍鍔熻兘(鍗抽氬父鎵璇寸殑鑷姩鐩稿叧鎬у弽棣)銆傚畠灏嗘墽琛屼袱娆℃悳绱紝鍏朵腑绗簩娆℃悳绱㈢殑鎼滅储鐭鏄悓绗竴娆℃悳绱㈡椂鎵惧埌鐨勫皯鏁伴《灞傛枃浠惰繛鎺ョ殑鍘熷鎼滅储鐭銆傝繖鏍,鍋囧杩欎簺鏂囦欢涓殑涓涓 鍚湁鍗曡瘝 鈥databases鈥 浠ュ強鍗曡瘝 鈥MySQL鈥, 鍒欑浜屾鎼滅储浼氬鎵惧惈鏈夊崟璇嶁MySQL鈥 鐨勬枃浠讹紝鍗充娇杩欎簺鏂囦欢涓嶅寘鍚崟璇 鈥database鈥濄備笅闈㈢殑渚嬪瓙鏄剧ず浜嗚繖涓笉鍚屼箣澶勶細
mysql> SELECT * FROM articles
-> WHERE MATCH (title,body) AGAINST ('database');
+----+-------------------+------------------------------------------+
| id | title | body |
+----+-------------------+------------------------------------------+
| 5 | MySQL vs. YourSQL | In the following database comparison ... |
| 1 | MySQL Tutorial | DBMS stands for DataBase ... |
+----+-------------------+------------------------------------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM articles
-> WHERE MATCH (title,body)
-> AGAINST ('database' WITH QUERY EXPANSION);
+----+-------------------+------------------------------------------+
| id | title | body |
+----+-------------------+------------------------------------------+
| 1 | MySQL Tutorial | DBMS stands for DataBase ... |
| 5 | MySQL vs. YourSQL | In the following database comparison ... |
| 3 | Optimizing MySQL | In this tutorial we will show ... |
+----+-------------------+------------------------------------------+
3 rows in set (0.00 sec)
鍙︿竴涓緥瀛愭槸Georges Simenon 鎼滅储鍏充簬Maigret鐨勪功绫, 杩欎釜鐢ㄦ埛涓嶇‘瀹氣Maigret鈥濅竴璇嶇殑鎷兼硶銆傝嫢涓嶄娇鐢ㄦ煡璇㈡墿灞曡屾悳绱⑩Megre and the reluctant witnesses鈥 寰楀埌鐨勭粨鏋滃彧鑳芥槸鐨勨Maigret and the Reluctant Witnesses鈥 銆 鑰屽甫鏈夋煡璇㈡墿灞曠殑鎼滅储浼氬湪绗簩閬嶅緱鍒板甫鏈夆Maigret鈥濅竴璇嶇殑鎵鏈変功鍚嶃
娉ㄩ噴锛 鐩叉煡璇㈡墿灞曞姛鑳藉緢瀹规槗杩斿洖闈炵浉鍏虫枃浠惰屽鍔犳棤鐢ㄤ俊鎭紝鍥犳鍙湁鍦ㄦ煡璇竴涓暱搴﹀緢鐭殑鐭鏃舵墠鏈夊繀瑕佷娇鐢ㄨ繖椤瑰姛鑳姐浠ヤ笅琛ㄥ垪鍑轰簡榛樿鐨勫叏鏂囧仠姝㈠瓧锛
a's | able | about | above | according |
accordingly | across | actually | after | afterwards |
again | against | ain't | all | allow |
allows | almost | alone | along | already |
also | although | always | am | among |
amongst | an | and | another | any |
anybody | anyhow | anyone | anything | anyway |
anyways | anywhere | apart | appear | appreciate |
appropriate | are | aren't | around | as |
aside | ask | asking | associated | at |
available | away | awfully | be | became |
because | become | becomes | becoming | been |
before | beforehand | behind | being | believe |
below | beside | besides | best | better |
between | beyond | both | brief | but |
by | c'mon | c's | came | can |
can't | cannot | cant | cause | causes |
certain | certainly | changes | clearly | co |
com | come | comes | concerning | consequently |
consider | considering | contain | containing | contains |
corresponding | could | couldn't | course | currently |
definitely | described | despite | did | didn't |
different | do | does | doesn't | doing |
don't | done | down | downwards | during |
each | edu | eg | eight | either |
else | elsewhere | enough | entirely | especially |
et | etc | even | ever | every |
everybody | everyone | everything | everywhere | ex |
exactly | example | except | far | few |
fifth | first | five | followed | following |
follows | for | former | formerly | forth |
four | from | further | furthermore | get |
gets | getting | given | gives | go |
goes | going | gone | got | gotten |
greetings | had | hadn't | happens | hardly |
has | hasn't | have | haven't | having |
he | he's | hello | help | hence |
her | here | here's | hereafter | hereby |
herein | hereupon | hers | herself | hi |
him | himself | his | hither | hopefully |
how | howbeit | however | i'd | i'll |
i'm | i've | ie | if | ignored |
immediate | in | inasmuch | inc | indeed |
indicate | indicated | indicates | inner | insofar |
instead | into | inward | is | isn't |
it | it'd | it'll | it's | its |
itself | just | keep | keeps | kept |
know | knows | known | last | lately |
later | latter | latterly | least | less |
lest | let | let's | like | liked |
likely | little | look | looking | looks |
ltd | mainly | many | may | maybe |
me | mean | meanwhile | merely | might |
more | moreover | most | mostly | much |
must | my | myself | name | namely |
nd | near | nearly | necessary | need |
needs | neither | never | nevertheless | new |
next | nine | no | nobody | non |
none | noone | nor | normally | not |
nothing | novel | now | nowhere | obviously |
of | off | often | oh | ok |
okay | old | on | once | one |
ones | only | onto | or | other |
others | otherwise | ought | our | ours |
ourselves | out | outside | over | overall |
own | particular | particularly | per | perhaps |
placed | please | plus | possible | presumably |
probably | provides | que | quite | qv |
rather | rd | re | really | reasonably |
regarding | regardless | regards | relatively | respectively |
right | said | same | saw | say |
saying | says | second | secondly | see |
seeing | seem | seemed | seeming | seems |
seen | self | selves | sensible | sent |
serious | seriously | seven | several | shall |
she | should | shouldn't | since | six |
so | some | somebody | somehow | someone |
something | sometime | sometimes | somewhat | somewhere |
soon | sorry | specified | specify | specifying |
still | sub | such | sup | sure |
t's | take | taken | tell | tends |
th | than | thank | thanks | thanx |
that | that's | thats | the | their |
theirs | them | themselves | then | thence |
there | there's | thereafter | thereby | therefore |
therein | theres | thereupon | these | they |
they'd | they'll | they're | they've | think |
third | this | thorough | thoroughly | those |
though | three | through | throughout | thru |
thus | to | together | too | took |
toward | towards | tried | tries | truly |
try | trying | twice | two | un |
under | unfortunately | unless | unlikely | until |
unto | up | upon | us | use |
used | useful | uses | using | usually |
value | various | very | via | viz |
vs | want | wants | was | wasn't |
way | we | we'd | we'll | we're |
we've | welcome | well | went | were |
weren't | what | what's | whatever | when |
whence | whenever | where | where's | whereafter |
whereas | whereby | wherein | whereupon | wherever |
whether | which | while | whither | who |
who's | whoever | whole | whom | whose |
why | will | willing | wish | with |
within | without | won't | wonder | would |
would | wouldn't | yes | yet | you |
you'd | you'll | you're | you've | your |
yours | yourself | yourselves | zero | 聽 |
MySQL鐨勫叏鏂囨悳绱㈠閲忓嚑涔庝笉鍏锋湁鐢ㄦ埛璋冭妭鍙傛暟銆傚亣濡備綘鎷ユ湁涓涓 MySQL婧愬垎甯冿紝浣犲氨鑳藉鍏ㄦ枃鎼滅储鎬ц兘琛屼娇鏇村鎺у埗锛屽師鍥犳槸涓浜涘彉鍖栭渶瑕佹簮浠g爜淇敼銆傝鍙傝2.8鑺傦紝鈥滀娇鐢ㄦ簮鐮佸垎鍙戠増瀹夎MySQL鈥銆
娉ㄦ剰锛屼负浜嗘洿鍔犳湁鏁堬紝闇瑕佸鍏ㄦ枃鎼滅储璋ㄦ厧璋冭妭銆傚疄闄呬笂锛屽湪澶у鏁版儏鍐典笅淇敼榛樿鎬ц兘鍙兘闄嶄綆鍏舵ц兘銆 闄ら潪浣犵煡閬撹嚜宸卞湪鍋氫粈涔堬紝鍚﹀垯涓嶈鏀瑰彉 MySQL婧愩
涓嬭堪鐨勫ぇ澶氭暟鍏ㄦ枃鍙橀噺蹇呴』鍦ㄦ湇鍔″櫒鍚姩鏃惰璁剧疆銆備负浜嗘敼鍙樺畠浠紝杩樿閲嶆柊鍚姩鏈嶅姟鍣紱鍦ㄦ湇鍔″櫒姝e湪杩愯鏈熼棿锛屼粬浠笉浼氳鏀瑰彉銆
涓浜涘彉閲忕殑鏀瑰彉闇瑕佷綘閲嶅缓琛ㄤ腑鐨 FULLTEXT 绱㈠紩銆傛湰绔犵粨灏鹃儴鍒嗙粰鍑轰簡鍏舵湁鍏虫搷浣滆鏄庛
路 [mysqld]
路 ft_min_word_len=3
鐒跺悗閲嶆柊鍚姩鏈嶅姟鍣紝閲嶅缓浣犵殑 FULLTEXT绱㈠紩銆傚悓鏃惰繕瑕佺壒鍒敞鎰忚琛ㄥ悗闈㈢殑璇存槑涓殑鍏充簬myisamchk鐨勬敞閲娿
鍋滄瀛楁槸鑷敱褰㈡佺殑锛屾崲瑷涔嬶紝浣犲彲浣跨敤浠讳綍璇稿newline銆 space鎴comma杩欐牱鐨勯潪瀛楁瘝鏁板瓧瀛楃鏉ュ垎闅旂鐢ㄨ瘝銆 涓嬪垝绾垮瓧绗(_) 鍜岃瑙嗕负鍗曡瘝鐨勪竴閮ㄥ垎鐨勫崟寮曞彿 (')渚嬪銆傚仠姝㈠瓧瀛楃闆嗕负鏈嶅姟鍣ㄩ粯璁ゅ瓧绗﹂泦锛涜10.3.1鑺傦紝鈥滄湇鍔″櫒瀛楃闆嗗拰鏍″鈥.
路 #define GWS_IN_USE GWS_PROB
灏嗚琛屾敼涓猴細
#define GWS_IN_USE GWS_FREQ
鐒跺悗閲嶆柊缂栬瘧 MySQL銆傛鏃朵笉闇瑕侀噸寤虹储寮曘傛敞閲锛杩欐牱鍋氫綘浼涓ラ噸鐨By 闄嶄綆 MySQL涓MATCH()鍑芥暟鎻愪緵鍚堥傜殑鐩稿叧鍊煎緱鑳藉姏銆傚亣濡備綘浜夊緱闇瑕佹悳绱㈣繖鏍风殑鏅氳瘝锛岃屼娇鐢IN BOOLEAN MODE浠f浛鐨勬晥鏋滄洿濂斤紝鍥犱负瀹冧笉閬靛惊 50% 闃堝笺
鍋囧浣犳敼鍙樹簡褰卞搷绱㈠紩鐨勫叏鏂囧彉閲 (ft_min_word_len銆 ft_max_word_len鎴ft_stopword_file),鎴栧亣濡備綘鏀瑰彉浜嗙鐢ㄨ瘝鏂囦欢鏈韩锛屽垯浣犲繀椤诲湪鏀瑰彉鍜岄噸鏂板惎鍔ㄦ湇鍔″櫒鍚庨噸寤轰綘鐨 FULLTEXT绱㈠紩銆傝繖鏃讹紝瑕侀噸寤虹储寮曪紝 鍙渶杩涜涓涓 QUICK 淇悊鎿嶄綔锛
mysql> REPAIR TABLE tbl_name QUICK;
娉ㄦ剰锛屽亣濡備綘浣跨敤 myisamchk 鏉ユ墽琛屼竴椤逛慨鏀硅〃绱㈠紩鐨勬搷浣 (璇稿淇悊鎴栧垎鏋 ), 鍒欎娇鐢ㄦ渶灏忓崟璇嶉暱搴﹀拰鏈澶у崟璇嶉暱搴︿互鍙婂仠姝㈠瓧鐨勯粯璁ゅ叏鏂囧弬鏁板奸噸寤FULLTEXT绱㈠紩锛岄櫎闈炰綘宸插彟澶栨寚瀹氥傝繖浼氬鑷撮棶璇㈠け璐ャ
鍙戠敓杩欎釜闂鐨勫師鍥犳槸鍙湁鏈嶅姟鍣ㄨ璇嗚繖浜涘弬鏁般傚畠浠殑瀛樺偍浣嶇疆涓嶅湪 MyISAM 绱㈠紩鏂囦欢涓傝嫢浣犲凡缁忎慨鏀逛簡鏈灏忓崟璇嶉暱搴︽垨鏈澶у崟璇嶉暱搴︽垨鏈嶅姟鍣ㄤ腑鐨勫仠姝㈠瓧锛屼负閬垮厤杩欎釜闂锛屼负浣犲mysqld鎵浣跨敤鐨myisamchk 鎸囧畾鍚屾牱鐨 ft_min_word_len銆 ft_max_word_len鍜ft_stopword_file鍊笺備緥濡傦紝鍋囧浣犲凡缁忓皢鏈灏忓崟璇嶉暱搴﹁缃负 3, 鍒欎綘鍙互杩欐牱淇敼涓涓甫鏈myisamchk鐨勮〃锛
shell> myisamchk --recover --ft_min_word_len=3 tbl_name.MYI
涓轰繚璇 myisamchk 鍙婃湇鍔″櫒瀵瑰叏鏂囧弬鏁颁娇鐢ㄧ浉鍚岀殑鍊硷紝 鍙皢姣忎竴椤归兘鏀惧湪渚涢夋枃浠朵腑鐨 [mysqld]鍜 [myisamchk] 閮ㄥ垎锛
[mysqld]
ft_min_word_len=3
[myisamchk]
ft_min_word_len=3
浣跨敤 REPAIR TABLE銆 ANALYZE TABLE銆OPTIMIZE TABLE鎴ALTER TABLE鏉ヤ唬鏇夸娇鐢 myisamchk 銆傝繖浜涜鍙ラ氳繃鏈嶅姟鍣ㄦ潵鎵ц锛屾湇鍔″櫒鐭ラ亾浣跨敤鍝釜鍏ㄦ枃鍙傛暟鍊兼洿鍔犲悎閫傘
BINARY鎿嶄綔绗﹀皢鍚庨潰鐨勫瓧绗︿覆鎶涚粰涓涓簩杩涘埗瀛楃涓层杩欐槸涓绉嶇畝鍗曠殑鏂瑰紡鏉ヤ績浣块愬瓧鑺傝屼笉鏄愬瓧绗︾殑杩涜鍒楁瘮杈冦傝繖浣垮緱姣旇緝鍖哄垎澶у皬鍐欙紝鍗充娇璇ュ垪涓嶈瀹氫箟涓 BINARY鎴 BLOB銆BINARY涔熶細浜х敓缁撳熬绌虹櫧锛屼粠鑰屾洿鍔犳樉鐪笺
mysql> SELECT 'a' = 'A';
-> 1
mysql> SELECT BINARY 'a' = 'A';
-> 0
mysql> SELECT 'a' = 'a ';
-> 1
mysql> SELECT BINARY 'a' = 'a ';
-> 0
BINARY褰卞搷鏁翠釜姣旇緝锛涘畠鍙互鍦ㄤ换浣曟搷浣滄暟鍓嶈缁欏畾锛岃屼骇鐢熺浉鍚岀殑缁撴灉銆
BINARY str 鏄CAST(str AS BINARY)鐨勭缉鐣ュ舰寮忋
娉ㄦ剰锛屽湪涓浜涜澧冧腑锛屽亣濡備綘灏嗕竴涓紪鍏ョ储寮曠殑鍒楁淳缁BINARY, MySQL 灏嗕笉鑳芥湁鏁堜娇鐢ㄨ繖涓储寮曘
鍋囧浣犳兂瑕佸皢涓涓 BLOB鍊兼垨鍏跺畠浜岃繘鍒跺瓧绗︿覆杩涜鍖哄垎澶у皬鍐欑殑姣旇緝锛屼綘鍙埄鐢ㄤ簩杩涘埗瀛楃涓叉病鏈夊瓧绗﹂泦杩欎竴浜嬪疄瀹炵幇杩欎釜鐩殑锛岃繖鏍峰氨涓嶄細鏈夋枃涔﹀す鐨勬蹇点備负鎵ц涓涓尯鍒嗗ぇ灏忓啓鐨勬瘮杈冿紝鍙娇鐢 CONVERT()鍑芥暟灏嗕竴涓瓧绗︿覆鍊艰浆鍖栦负涓涓笉鍖哄垎澶у皬鍐欑殑瀛楃闆嗐傚叾缁撴灉涓轰竴涓潪浜岃繘鍒跺瓧绗︿覆锛屽洜姝 LIKE 鎿嶄綔涔熶笉浼氬尯鍒嗗ぇ灏忓啓锛
SELECT 'A' LIKE CONVERT(blob_col USING latin1) FROM tbl_name;
鑻ヨ浣跨敤涓涓笉鍚岀殑瀛楃闆, 鏇挎崲鍏跺湪涓婅堪璇彞涓殑latin1鍚嶃
CONVERT()涓鑸彲鐢ㄤ簬姣旇緝鍑虹幇鍦ㄤ笉鍚屽瓧绗﹂泦涓殑瀛楃涓层
CAST() 鍜CONVERT() 鍑芥暟鍙敤鏉ヨ幏鍙栦竴涓被鍨嬬殑鍊硷紝骞朵骇鐢熷彟涓涓被鍨嬬殑鍊笺
杩欎釜绫诲瀷 鍙互鏄互涓嬪煎叾涓殑 涓涓細
BINARY 浜х敓涓涓簩杩涘埗瀛楃涓层傚叧浜庡畠鎬庢牱褰卞搷姣旇緝缁撴灉鐨勮鏄庤鏈珷涓 BINARY鎿嶄綔绗﹂」銆
鍋囧缁欏畾浜嗛殢鎰忛暱搴N锛屽垯 BINARY[N] 浣 cast浣跨敤璇ュ弬鏁扮殑涓嶅浜 N 涓瓧鑺傘傚悓鏍风殑锛 CHAR[N]浼氫娇 cast 浣跨敤璇ュ弬鏁扮殑涓嶅浜N 涓瓧绗︺
CAST() and CONVERT(... USING ...) 鏄爣鍑 SQL璇硶銆CONVERT()鐨勯潪USING 鏍煎紡鏄ofis ODBC璇硶銆
甯︽湁USING鐨CONVERT() 琚敤鏉ュ湪涓嶅悓鐨勫瓧绗﹂泦涔嬮棿杞寲鏁版嵁銆傚湪 MySQL涓, 鑷姩璇戠爜鍚嶅拰鐩稿簲鐨勫瓧绗﹂泦鍚嶇О鐩稿悓銆備緥濡傘 杩欎釜璇彞灏嗘湇鍔″櫒鐨勯粯璁ゅ瓧绗﹂泦涓殑瀛楃涓 'abc'杞寲涓utf8瀛楃闆嗕腑鐩稿簲鐨勫瓧绗︿覆锛
SELECT CONVERT('abc' USING utf8);
褰撲綘鎯宠鍦ㄤ竴涓CREATE ... SELECT 璇彞涓垱寤轰竴涓壒娈婄被鍨嬬殑鍒楋紝鍒cast鍑芥暟浼氬緢鏈夌敤锛
CREATE TABLE new_table SELECT CAST('2000-01-01' AS DATE);
璇ュ嚱鏁颁篃鐢ㄤ簬ENUM 鍒楁寜璇嶆硶椤哄簭鐨勬帓搴忋傞氬父ENUM鍒楃殑鎺掑簭鍦ㄤ娇鐢ㄥ唴閮ㄦ暟鍊兼椂鍙戠敓銆傚皢杩欎簺鍊兼寜鐓ц瘝娉曢『搴忔淳缁 CHAR 缁撴灉锛
SELECT enum_col FROM tbl_name ORDER BY CAST(enum_col AS CHAR);
CAST(str AS BINARY)鍜BINARY str鐩稿悓銆 CAST(expr AS CHAR) 灏嗚〃杈惧紡瑙嗕负涓涓甫鏈夐粯璁ゅ瓧绗﹂泦鐨勫瓧绗︿覆銆
鑻ョ敤浜庝竴涓濡 CONCAT('Date: ',CAST(NOW() AS DATE))杩欐牱鐨勬瘮杈冨鏉傜殑琛ㄨ揪寮忕殑涓閮ㄥ垎锛CAST()涔熶細鏀瑰彉缁撴灉銆
浣犱笉搴斿湪涓嶅悓鐨勬牸寮忎腑浣跨敤 CAST() 鏉ユ瀽鍙栨暟鎹紝浣嗗彲浠ヤ娇鐢ㄨ濡LEFT() 鎴 EXTRACT() 鐨勬牱鐨勫瓧绗︿覆鍑芥暟鏉ヤ唬鏇裤傝鍙傝12.5鑺傦紝鈥滄棩鏈熷拰鏃堕棿鍑芥暟鈥銆
鑻ヨ鍦ㄦ暟鍊艰澧冧腑灏嗕竴涓瓧绗︿覆娲剧粰涓涓暟鍊, 閫氬父鎯呭喌涓嬶紝闄や簡灏嗗瓧绗︿覆鍊间綔涓烘暟瀛椾娇鐢ㄥ锛屼綘涓嶉渶瑕佸仛浠讳綍浜嬶細
mysql> SELECT 1+'1';
-> 2
鑻ヨ鍦ㄤ竴涓瓧绗︿覆璇涓娇鐢ㄤ竴涓暟瀛楋紝璇ユ暟瀛椾細琚嚜鍔ㄨ浆鍖栦负涓涓BINARY 瀛楃涓层
mysql> SELECT CONCAT('hello you ',2);
-> 'hello you 2'
MySQL 鏀寔甯︾鍙峰拰鏃犵鍙风殑64姣旂壒鍊肩殑杩愮畻銆傝嫢浣犳鍦ㄤ娇鐢ㄦ暟瀛楁搷浣滅 (濡 +) 鑰屽叾涓竴涓搷浣滄暟涓烘棤绗﹀彿鏁存暟锛屽垯缁撴灉涓烘棤绗﹀彿銆傚彲浣跨敤SIGNED 鍜UNSIGNED cast 鎿嶄綔绗︽潵瑕嗙洊瀹冦傚皢杩愮畻鍒嗗埆娲剧粰甯︾鍙锋垨鏃犵鍙64姣旂壒鏁存暟銆
mysql> SELECT CAST(1-2 AS UNSIGNED)
-> 18446744073709551615
mysql> SELECT CAST(CAST(1-2 AS UNSIGNED) AS SIGNED);
-> -1
娉ㄦ剰锛屽亣濡備换鎰忎竴涓搷浣滄暟涓轰竴涓诞鐐瑰硷紝鍒欑粨鏋滀负涓涓诞鐐瑰硷紝 涓斾笉浼氬彈鍒颁笂杩拌鍒欏奖鍝 (鍏充簬杩欎竴鐐, DECIMAL 鍒楀艰瑙嗕负娴偣鍊硷級銆
mysql> SELECT CAST(1 AS UNSIGNED) - 2.0;
-> -1.0
鑻ヤ綘鍦ㄤ竴涓畻鏈繍绠椾腑浣跨敤浜嗕竴涓瓧绗︿覆锛屽畠浼氳杞寲涓轰竴涓诞鐐规暟銆
瀵逛簬姣旂壒杩愮畻锛MySQL 浣跨敤 BIGINT (64姣旂壒) 绠楁硶锛屽洜姝よ繖浜涙搷浣滅鐨勬渶澶ц寖鍥存槸 64 姣旂壒銆
Bitwise OR:
mysql> SELECT 29 | 15;
-> 31
鍏剁粨鏋滀负涓涓64姣旂壒鏃犵鍙锋暣鏁般
Bitwise AND:
mysql> SELECT 29 & 15;
-> 13
鍏剁粨鏋滀负涓涓64姣旂壒鏃犵鍙锋暣鏁般
Bitwise XOR:
mysql> SELECT 1 ^ 1;
-> 0
mysql> SELECT 1 ^ 0;
-> 1
mysql> SELECT 11 ^ 3;
-> 8
缁撴灉涓轰竴涓64姣旂壒鏃犵鍙锋暣鏁般
鎶婁竴涓longlong (BIGINT)鏁板乏绉讳袱浣嶃
mysql> SELECT 1 << 2;
-> 4
鍏剁粨鏋滀负涓涓64姣旂壒鏃犵鍙锋暣鏁般
鎶婁竴涓longlong (BIGINT)鏁板彸绉讳袱浣銆
mysql> SELECT 4 >> 2;
-> 1
鍏剁粨鏋滀负涓涓64姣旂壒鏃犵鍙锋暣鏁般
鍙嶈浆鎵鏈夋瘮鐗广
mysql> SELECT 5 & ~1;
-> 4
鍏剁粨鏋滀负涓涓64姣旂壒鏃犵鍙锋暣鏁般
杩斿洖鍙傛暟N 涓墍璁剧疆鐨勬瘮鐗规暟
mysql> SELECT BIT_COUNT(29);
-> 4
鏈妭浠嬬粛浜嗗姞瀵嗗拰鍔犲瘑鍊笺傝嫢浣犳兂瑕佸偍瀛樹竴浜涚敱鍙兘鍖呭惈浠绘剰瀛楄妭鍊肩殑鍔犲瘑鍑芥暟杩斿洖鐨勭粨鏋滐紝浣跨敤BLOB鍒楄屼笉鏄 CHAR 鎴VARCHAR 鍒楋紝浠庤岄伩鍏嶇敱浜庣粨灏剧┖鏍肩殑鍒犻櫎鑰屾敼鍙樹竴浜涙暟鎹肩殑娼滃湪闂銆
杩欎簺鍑芥暟鍏佽浣跨敤瀹樻柟AES杩涜鍔犲瘑鍜屾暟鎹姞瀵 (楂樼骇鍔犲瘑鏍囧噯 ) 绠楁硶, 鍗充互鍓嶄汉浠墍鐔熺煡鐨 鈥Rijndael鈥濄 淇濆瘑鍏抽敭瀛楃殑闀垮害涓128姣旂壒锛屼笉杩囦綘鍙互閫氳繃鏀瑰彉婧愯屽皢鍏跺欢闀垮埌 256 姣旂壒銆傛垜浠夋嫨浜 128姣旂壒鐨勫師鍥犳槸瀹冪殑閫熷害瑕佸揩寰楀锛屼笖瀵逛簬澶у鏁扮敤閫旇岃█杩欎釜淇濆瘑绋嬪害宸茬粡澶熺敤銆
杈撳叆鍙傛暟鍙互涓轰换浣曢暱搴︺傝嫢浠讳綍涓涓弬鏁颁负NULL,鍒欏嚱鏁扮殑缁撴灉涔熸槸NULL銆
鍥犱负 AES 鏄鍧楃骇绠楁硶锛浣跨敤濉厖灏嗕笉鍧囪 闀垮害瀛楃涓茬紪鐮侊紝杩欐牱缁撴灉瀛楃涓茬殑闀垮害鐨勭畻娉曚负 16 * (trunc(string_length / 16) + 1)銆
鑻 AES_DECRYPT()妫娴嬪埌鏃犳晥鏁版嵁鎴栦笉姝g‘濉厖锛屽畠浼氳繑鍥 NULL銆傜劧鑰岋紝鑻ヨ緭鍏ョ殑璧勬枡鎴栧瘑鐮佹棤鏁堟椂锛 AES_DECRYPT()鏈夊彲鑳借繑鍥炰竴涓潪 NULL 鍊 (鍙兘涓烘棤鐢ㄤ俊鎭 )銆
浣犲彲浠ラ氳繃淇敼浣犵殑闂锛屼粠鑰屼娇鐢AES鍑芥暟浠ュ姞瀵嗗舰寮忔潵瀛樺偍鏁版嵁锛
INSERT INTO t VALUES (1,AES_ENCRYPT('text','password'));
AES_ENCRYPT()鍜AES_DECRYPT() 鍙互琚湅浣MySQL涓櫘閬嶉氱敤鐨勫瘑鐮佹渶瀹夊叏鐨勫姞瀵嗗嚱鏁般
浣跨敤 pass_str 浣滀负瀵嗙爜锛岃В瀵嗗姞瀵嗗瓧绗︿覆 crypt_str锛 crypt_str 搴旇鏄敱ENCODE()杩斿洖鐨勫瓧绗︿覆銆
浣跨敤pass_str 浣滀负瀵嗙爜锛岃В瀵 str 銆 浣跨敤DECODE()瑙e瘑缁撴灉銆
缁撴灉鏄竴涓拰str闀垮害鐩稿悓鐨勪簩杩涘埗瀛楃涓层傝嫢浣犳兂瑕佸皢鍏朵繚鐣欏湪涓涓垪涓紝鍙娇鐢 BLOB 鍒楃被鍨嬨
浣跨敤DES_ENCRYPT()鍔犲瘑涓涓瓧绗︿覆銆傝嫢鍑虹幇閿欒锛岃繖涓嚱鏁颁細杩斿洖 NULL銆
娉ㄦ剰锛岃繖涓嚱鏁板彧鏈夊綋MySQL鍦SSL 鐨勬敮鎸佷笅閰嶇疆瀹屾瘯鏃舵墠浼氳繍浣溿傝鍙傝5.8.7鑺傦紝鈥滀娇鐢ㄥ畨鍏ㄨ繛鎺モ.
鍋囧娌℃湁缁欏畾 key_str 鍙傛暟锛 DES_DECRYPT() 浼氶鍏堟鏌ュ姞瀵嗗瓧绗︿覆鐨勭涓涓瓧鑺傦紝 浠庤岀‘瀹氱敤鏉ュ姞瀵嗗師濮嬪瓧绗︿覆鐨DES瀵嗙爜鍏抽敭瀛楁暟瀛楋紝涔嬪悗浠DES鍏抽敭瀛楁枃浠朵腑璇诲彇鍏抽敭瀛椾粠鑰岃В瀵嗕俊鎭備负浣垮叾杩愯锛岀敤鎴峰繀椤讳韩鏈 SUPER 鐗规潈銆傚彲浠ラ夋嫨--des-key-file鏈嶅姟鍣ㄦ寚瀹氬叧閿瓧鏂囦欢銆
鍋囧浣犲悜杩欎釜鍑芥暟浼犻掍竴涓key_str 鍙傛暟锛岃瀛楃涓茶鐢ㄤ綔瑙e瘑淇℃伅鐨勫叧閿瓧銆
鑻 crypt_str 鍙傛暟鐪嬭捣鏉ヤ笉鏄竴涓姞瀵嗗瓧绗︿覆锛 MySQL 浼氳繑鍥炵粰瀹氱殑 crypt_str銆
鐢Triple-DES 绠楁硶缁欏嚭鐨勫叧閿瓧鍔犲瘑瀛楃涓层傝嫢鍑虹幇閿欒锛岃繖涓嚱鏁颁細杩斿洖NULL銆
娉ㄦ剰锛岃繖涓嚱鏁板彧鏈夊綋MySQL 鍦SSL鐨勬敮鎸佷笅閰嶇疆瀹屾瘯鍚庢墠浼氳繍琛屻傝鍙傝5.8.7鑺傦紝鈥滀娇鐢ㄥ畨鍏ㄨ繛鎺モ.
浣跨敤鐨勫姞瀵嗗叧閿瓧鐨勯夋嫨鍩轰簬绗簩涓埌 DES_ENCRYPT()鐨勫弬鏁帮紝鍋囧缁欏畾锛
鍙傛暟 |
璇存槑 |
鏃犲弬鏁 |
浣跨敤鏉ヨ嚜DES鍏抽敭瀛楁枃浠剁殑绗竴涓叧閿瓧銆 |
key_num |
浣跨敤DES 鍏抽敭瀛楁枃浠剁粰鍑虹殑鍏抽敭瀛楁暟瀛(0-9)銆 |
key_str |
浣跨敤缁欏嚭鐨勫叧閿瓧瀛楃涓蹭负 str 鍔犲瘑銆 |
閫夋嫨--des-key-file鏈嶅姟鍣ㄦ寚瀹氬叧閿瓧鏂囦欢銆
杩斿洖瀛楃涓叉槸涓涓簩杩涘埗瀛楃涓诧紝鍏朵腑绗竴涓瓧绗︿负 CHAR(128 | key_num)銆
鍔犱笂 128浣垮緱璇嗗埆鍔犲瘑鍏抽敭瀛楁洿鍔犲鏄撱傝嫢浣犱娇鐢ㄤ竴涓瓧绗︿覆鍏抽敭瀛楋紝鍒 key_num 涓127銆
缁撴灉鐨勫瓧绗︿覆闀垮害涓 new_len = orig_len + (8-(orig_len % 8))+1銆
DES鍏抽敭瀛楁枃浠朵腑鐨勬瘡涓琛岄兘鍏锋湁濡備笅鏍煎紡锛
key_num des_key_str
姣忎釜key_num 蹇呴』鏄竴涓粠0鍒0鑼冨洿鍐呯殑鏁板瓧銆傛枃浠朵腑琛岀殑鎺掑垪椤哄簭鏄换鎰忕殑銆 des_key_str 鏄敤鏉ュ姞瀵嗕俊鎭殑瀛楃涓层傚湪鏁板瓧鍜屽叧閿瓧涔嬮棿搴旇鑷冲皯鏈変竴涓┖鏍笺傝嫢浣犳湭鎸囧畾浠讳綍鍒DES_ENCRYPT()鐨勫叧閿瓧鍙傛暟锛屽垯绗竴涓叧閿瓧涓洪粯璁ょ殑浣跨敤鍏抽敭瀛椼
浣跨敤FLUSH DES_KEY_FILE璇彞锛屼綘鍙互璁 MySQL浠庡叧閿瓧鏂囦欢璇诲彇鏂扮殑鍏抽敭瀛楀笺傝繖瑕佹眰浣犱韩鏈 RELOAD鐗规潈銆
鎷ユ湁涓濂楅粯璁ゅ叧閿瓧鐨勪竴涓ソ澶勫氨鏄畠鍚戝簲鐢ㄧ▼搴忔彁渚涗簡涓涓楠屽姞瀵嗗垪鍊肩殑鏂瑰紡锛岃屾棤椤诲悜鏈缁堢敤鎴锋彁渚涜В瀵嗚繖浜涘肩殑鏉冨姏銆
mysql> SELECT customer_address FROM customer_table
> WHERE crypted_credit_card = DES_ENCRYPT('credit_card_number');
浣跨敤Unix crypt() 绯荤粺璋冪敤鍔犲瘑 str銆 salt 鍙傛暟搴斾负涓涓嚦灏戝寘鍚2涓瓧绗︾殑瀛楃涓层傝嫢娌℃湁缁欏嚭 salt 鍙傛暟锛屽垯浣跨敤浠绘剰鍊笺
mysql> SELECT ENCRYPT('hello');
-> 'VxuFAJXVARROc'
鑷冲皯鍦ㄤ竴浜涚郴缁熶腑锛ENCRYPT()闄や簡str鐨勫墠鍏綅瀛楃涔嬪浼氬拷鐣ユ墍鏈夊唴瀹广傝繖涓涓虹敱涓嬪垝绾跨殑crypt() 绯荤粺璋冪敤鐨勬墽琛屾墍鍐冲畾銆
鍋囧crypt()鍦ㄤ綘鐨勭郴缁熶腑涓嶅彲鐢(姝e鍦 Windows绯荤粺), ENCRYPT() 鍒欎細濮嬬粓杩斿洖NULL銆傞壌浜庤繖涓師鍥狅紝鎴戜滑鍚戜綘鎺ㄨ崘浣跨敤 MD5() 鎴SHA1() 鏉ヤ唬鏇,鍥犱负杩欎袱涓嚱鏁伴傚悎鎵鏈夌殑骞冲彴銆
涓哄瓧绗︿覆绠楀嚭涓涓 MD5 128姣旂壒妫鏌ュ拰銆傝鍊间互32浣嶅崄鍏繘鍒舵暟瀛楃殑浜岃繘鍒跺瓧绗︿覆鐨勫舰寮忚繑鍥, 鑻ュ弬鏁颁负 NULL 鍒欎細杩斿洖 NULL銆備緥濡傦紝杩斿洖鍊煎彲琚敤浣滄暎鍒楀叧閿瓧銆
mysql> SELECT MD5('testing');
-> 'ae2b1fca515949e5d54fb22b8ed95575'
杩欐槸"RSA Data Security, Inc. MD5 Message-Digest Algorithm."
鍋囧浣犳兂瑕佸皢杩欎釜鍊艰浆鍖栦负澶у啓瀛楁瘝锛屽弬瑙12.8鑺傦紝鈥淐ast鍑芥暟鍜屾搷浣滅鈥涓BINARY鎿嶄綔绗﹂」涓粰鍑虹殑浜岃繘鍒跺瓧绗︿覆杞崲銆
褰PASSWORD()鐨勬墽琛屽彉涓烘敼鍠勫畨鍏ㄦф椂锛OLD_PASSWORD()浼氳娣诲姞鍒 MySQL銆OLD_PASSWORD()杩斿洖浠庡墠鐨PASSWORD()鎵ц鍊( 4.1涔嬪墠)锛屽悓鏃跺厑璁镐綘涓轰换浣4.1 涔嬪墠鐨勯渶瑕佽繛鎺ュ埌浣犵殑5.1 鐗堟湰MySQL鏈嶅姟鍣ㄥ墠瀹㈡埛绔缃瘑鐮侊紝浠庤屼笉鑷充簬灏嗗畠浠垏鏂傝鍙傝5.7.9鑺傦紝鈥淢ySQL 4.1涓殑瀵嗙爜鍝堝笇澶勭悊鈥銆
浠庡師鏂囧瘑鐮str 璁$畻骞惰繑鍥炲瘑鐮佸瓧绗︿覆锛屽綋鍙傛暟涓 NULL 鏃惰繑鍥 NULL銆傝繖涓嚱鏁扮敤浜庣敤鎴锋巿鏉冭〃鐨Password鍒椾腑鐨勫姞瀵MySQL瀵嗙爜瀛樺偍
mysql> SELECT PASSWORD('badpwd');
-> '7f84554057dd964b'
PASSWORD() 鍔犲瘑鏄崟鍚戠殑 (涓嶅彲閫 )銆
PASSWORD() 鎵ц瀵嗙爜鍔犲瘑涓Unix 瀵嗙爜琚姞瀵嗙殑鏂瑰紡涓嶅悓銆傝鍙傝ENCRYPT()銆
娉ㄩ噴锛 PASSWORD()鍑芥暟鍦MySQL鏈嶅姟鍣ㄤ腑鐨勯壌瀹氱郴缁熶娇鐢紱浣涓搴斿皢瀹冪敤鍦ㄤ綘涓汉鐨勫簲鐢ㄧ▼搴忎腑銆備负杈惧埌鍚屾牱鐩殑锛屽彲浣跨敤 MD5()鎴SHA1() 浠f浛銆 鏇村鍏充簬鍦ㄦ偍鐨勫簲鐢ㄧ▼搴忎腑澶勭悊瀵嗙爜鍙婂畨鍏ㄩ壌瀹氱殑淇℃伅瑙RFC 2195
涓哄瓧绗︿覆绠楀嚭涓涓 SHA1 160姣旂壒妫鏌ュ拰锛屽RFC 3174 (瀹夊叏鏁e垪绠楁硶 )涓墍杩般傝鍊艰浣滀负40浣嶅崄鍏繘鍒舵暟瀛楄繑鍥烇紝鑰屽綋鍙傛暟涓NULL 鏃跺垯杩斿洖 NULL銆傝繖涓嚱鏁扮殑涓涓彲鑳界殑鐢ㄥ灏卞湪浜庡叾浣滀负鏁e垪鍏抽敭瀛椼備綘涔熷彲浠ュ皢鍏朵綔涓哄瓨鍌ㄥ瘑鐮佺殑瀵嗙爜瀹夊叏鍑芥暟浣跨敤銆
mysql> SELECT SHA1('abc');
-> 'a9993e364706816aba3e25717850c26c9cd0d89d'
SHA1()鍙互琚涓轰竴涓瘑鐮佹洿鍔犲畨鍏ㄧ殑鍑芥暟锛岀浉褰撲簬 MD5()銆 SHA() 鍜SHA1()鍏锋湁鐩稿悓鐨勬剰涔夈
BENCHMARK() 鍑芥暟閲嶅count 娆℃墽琛岃〃杈惧紡 expr 銆 瀹冨彲浠ヨ鐢ㄤ簬璁$畻 MySQL 澶勭悊琛ㄨ揪寮忕殑閫熷害銆傜粨鏋滃奸氬父涓 0銆傚彟涓绉嶇敤澶勬潵鑷 mysql瀹㈡埛绔唴閮,鑳藉鎶ュ憡闂鎵ц鐨勬鏁帮細
mysql> SELECT BENCHMARK(1000000,ENCODE('hello','goodbye'));
+----------------------------------------------+
| BENCHMARK(1000000,ENCODE('hello','goodbye')) |
+----------------------------------------------+
| 0 |
+----------------------------------------------+
1 row in set (4.74 sec)
姝ゅ鎶ュ憡鐨勬椂闂存槸瀹㈡埛绔笂鐨勫叡鐢ㄦ椂闂达紝鑰屼笉鏄湇鍔″櫒绔笂鐨CPU鏃堕棿銆傚缓璁墽琛屽閬BENCHMARK()锛屽苟瑙i噴涓庢湇鍔″櫒鏈哄櫒璐熻嵎绋嬪害鏈夊叧鐨勭粨鏋溿
路 CHARSET(str)
杩斿洖瀛楃涓茶嚜鍙橀噺鐨勫瓧绗﹂泦銆
mysql> SELECT CHARSET('abc');
-> 'latin1'
mysql> SELECT CHARSET(CONVERT('abc' USING utf8));
-> 'utf8'
mysql> SELECT CHARSET(USER());
-> 'utf8'
路 COERCIBILITY(str)
杩斿洖瀛楃涓茶嚜鍙橀噺鐨勬暣搴忓彲鍘嬬缉鎬у笺
mysql> SELECT COERCIBILITY('abc' COLLATE latin1_swedish_ci);
-> 0
mysql> SELECT COERCIBILITY(USER());
-> 3
mysql> SELECT COERCIBILITY('abc');
-> 4
杩斿洖鍊煎叿鏈夊涓嬫剰涔夛細
鍙帇缂╂ |
鎰忎箟 |
涓句緥 |
0 |
鏄庣‘鎺掑簭 |
甯︽湁COLLATE 瀛愬彞鐨勫 |
1 |
鏃犳帓搴 |
涓嶅悓鎺掑簭鐨勫瓧绗︿覆杩炴帴 |
2 |
鏄庣‘鎺掑簭 |
鍒楀 |
3 |
绯荤粺甯搁噺 |
USER()杩斿洖鍊 |
4 |
鍙帇缂 |
鏂囧瓧瀛楃涓 |
5 |
鍙拷鐣 |
NULL寰楁潵鐨NULL鎴栦竴涓〃杈惧紡 |
涓嬫柟鍊煎緱浼樺厛绾ц緝楂樸
杩斿洖鎯犲瓧绗︿覆鍙傛暟鐨勬帓搴忔柟寮忋
mysql> SELECT COLLATION('abc');
-> 'latin1_swedish_ci'
mysql> SELECT COLLATION(_utf8'abc');
-> 'utf8_general_ci'
杩斿洖瀵逛簬杩炴帴鐨勮繛鎺ID (绾跨▼ID)銆傛瘡涓繛鎺ラ兘鏈夊悇鑷殑鍞竴 ID銆
mysql> SELECT CONNECTION_ID();
-> 23786
杩斿洖褰撳墠璇濊矾琚獙璇佺殑鐢ㄦ埛鍚嶅拰涓绘満鍚嶇粍鍚堛傝繖涓肩鍚堢‘瀹氫綘鐨勫瓨鍙栨潈闄愮殑MySQL 璐︽埛銆傚湪琚寚瀹SQL SECURITY DEFINER鐗瑰緛鐨勫瓨鍌ㄧ▼搴忓唴锛 CURRENT_USER() 杩斿洖绋嬪簭鐨勫垱寤鸿呫
CURRENT_USER()鐨勫煎彲浠ュ拰USER()鐨勫兼湁鎵涓嶅悓銆
mysql> SELECT USER();
-> 'davida@localhost'
mysql> SELECT * FROM mysql.user;
ERROR 1044: Access denied for user ''@'localhost' to
database 'mysql'
mysql> SELECT CURRENT_USER();
-> '@localhost'
杩欎釜渚嬪瓙瑙i噴浜嗚櫧鐒跺鎴风鎸囧畾浜嗕竴涓 davida鐢ㄦ埛鍚 (姝eUSER()鍑芥暟鐨勫兼墍鎸囩ず鐨), 鏈嶅姟鍣ㄥ嵈浣跨敤涓涓尶鍚嶇殑鐢ㄦ埛璐︽埛纭璇ュ鎴风 (瑙CURRENT_USER()鍊煎緱绌虹敤鎴峰悕閮ㄥ垎 )銆傝繖绉嶆儏鍐靛彂鐢熺殑涓涓師鍥犳槸 One 鍦ㄥ悜 davida鐨勬巿鏉冨垪琛ㄤ腑娌℃湁瓒冲鐨勮处鎴枫
CURRENT_USER() 杩斿洖鐨勫瓧绗︿覆浣跨敤 utf8瀛楃闆嗐
杩斿洖浣跨敤 utf8 瀛楃闆嗙殑榛樿(褰撳墠)鏁版嵁搴撳悕銆傚湪瀛樺偍绋嬪簭閲岋紝榛樿鏁版嵁搴撴槸鍚岃绋嬪簭鍚戝叧鑱旂殑鏁版嵁搴擄紝浣嗗苟涓嶄竴瀹氫笌璋冪敤璇鐨勯粯璁ゆ暟鎹簱鐩稿悓銆
mysql> SELECT DATABASE();
-> 'test'
鑻ユ病鏈夐粯璁ゆ暟鎹簱, DATABASE()杩斿洖 NULL銆
A SELECT璇彞鍙兘鍖呮嫭涓涓 LIMIT 瀛愬彞锛岀敤鏉ラ檺鍒舵湇鍔″櫒杩斿洖瀹㈡埛绔殑琛屾暟銆傚湪鏈変簺鎯呭喌涓嬶紝闇瑕佷笉鐢ㄥ啀娆¤繍琛岃璇彞鑰屽緱鐭ュ湪娌℃湁LIMIT 鏃跺埌搴曡璇彞杩斿洖浜嗗灏戣銆備负浜嗙煡閬撹繖涓鏁, 鍖呮嫭鍦SELECT 璇彞涓夋嫨 SQL_CALC_FOUND_ROWS 锛岄殢鍚庤皟鐢 FOUND_ROWS() 锛
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
-> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
绗簩涓 SELECT杩斿洖涓涓暟瀛楋紝鎸囩ず浜嗗湪娌℃湁LIMIT瀛愬彞鐨勬儏鍐典笅锛岀涓涓SELECT杩斿洖浜嗗灏戣 (鑻ヤ笂杩扮殑 SELECT璇彞涓嶅寘鎷 SQL_CALC_FOUND_ROWS 閫夐」,鍒欎娇鐢LIMIT 鍜屼笉浣跨敤鏃讹紝FOUND_ROWS() 鍙兘浼氳繑鍥炰笉鍚岀殑缁撴灉锛銆
閫氳繃 FOUND_ROWS()鐨勬湁鏁堣鏁版槸鐬椂鐨勶紝骞朵笖涓嶇敤浜庤秺杩SELECT SQL_CALC_FOUND_ROWS璇彞鍚庨潰鐨勮鍙ャ傝嫢浣犻渶瑕佺◢鍊欏弬闃呰繖涓硷紝閭d箞灏嗗叾淇濆瓨锛
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM ... ;
mysql> SET @rows = FOUND_ROWS();
鍋囧浣犳鍦ㄤ娇鐢 SELECT SQL_CALC_FOUND_ROWS, MySQL 蹇呴』璁$畻鍑哄湪鍏ㄩ儴缁撴灉闆嗗悎涓湁鎵灏戣銆傜劧鑰岋紝 杩欐瘮涓嶇敤LIMIT鑰屽啀娆¤繍琛岄棶璇㈣蹇紝鍘熷洜鏄粨鏋滈泦鍚堜笉闇瑕佽閫佽嚦瀹㈡埛绔
SQL_CALC_FOUND_ROWS 鍜 FOUND_ROWS() 鍦ㄥ綋浣犲笇鏈涢檺鍒朵竴涓棶璇㈣繑鍥炵殑琛屾暟鏃跺緢鏈夌敤锛屽悓鏃惰繕鑳戒笉闇瑕佸啀娆¤繍琛岄棶璇㈣岀‘瀹氬叏閮ㄧ粨鏋滈泦鍚堜腑鐨勮鏁般備竴涓緥瀛愬氨鏄彁渚涢〉寮忔樉绀虹殑Web鑴氭湰锛岃鏄剧ず鍖呭惈鏄剧ず鎼滅储缁撴灉鍏跺畠閮ㄥ垎鐨勯〉鐨勮繛鎺ャ備娇鐢FOUND_ROWS() 浣夸綘纭畾鍓╀笅鐨勭粨鏋滈渶瑕佸灏戝叾瀹冪殑椤点
SQL_CALC_FOUND_ROWS 鍜 FOUND_ROWS() 鐨勫簲鐢ㄥ浜UNION 闂姣斿浜庣畝鍗SELECT 璇彞鏇翠负澶嶆潅锛屽師鍥犳槸鍦UNION 涓紝LIMIT 鍙兘浼氬嚭鐜板湪澶氫釜浣嶇疆銆傚畠鍙兘閫傜敤浜UNION涓殑涓汉 SELECT璇彞锛屾垨鏄讳綋涓 鍒UNION 缁撴灉鐨勫叏绋嬨
SQL_CALC_FOUND_ROWS瀵逛簬 UNION鐨勬剰鍚戞槸瀹冨簲璇ヤ笉闇瑕佸叏绋LIMIT鑰岃繑鍥炲簲杩斿洖鐨勮鏁般SQL_CALC_FOUND_ROWS 鍜UNION 涓鍚屼娇鐢ㄧ殑鏉′欢鏄細
鑷姩杩斿洖鏈鍚庝竴涓INSERT鎴 UPDATE 闂涓 AUTO_INCREMENT鍒楄缃殑绗竴涓 鍙戠敓鐨勫笺
mysql> SELECT LAST_INSERT_ID();
-> 195
浜х敓鐨ID 姣忔杩炴帴鍚淇濆瓨鍦ㄦ湇鍔″櫒涓傝繖鎰忓懗鐫鍑芥暟鍚戜竴涓粰瀹氬鎴风杩斿洖鐨勫兼槸璇ュ鎴风浜х敓瀵瑰奖鍝AUTO_INCREMENT鍒楃殑鏈鏂拌鍙ョ涓涓 AUTO_INCREMENT鍊肩殑銆傝繖涓间笉鑳借鍏跺畠瀹㈡埛绔奖鍝嶏紝鍗充娇瀹冧滑浜х敓瀹冧滑鑷繁鐨 AUTO_INCREMENT鍊笺傝繖涓涓轰繚璇佷簡浣犺兘澶熸壘鍥炶嚜宸辩殑 ID 鑰屼笉鐢ㄦ媴蹇冨叾瀹冨鎴风鐨勬椿鍔紝鑰屼笖涓嶉渶瑕佸姞閿佹垨澶勭悊銆
鍋囧浣犱娇鐢ㄤ竴涓潪鈥magic鈥濆兼潵鏇存柊鏌愪竴琛岀殑AUTO_INCREMENT 鍒楋紝鍒LAST_INSERT_ID() 鐨勫间笉浼氬彉鍖(鎹㈣█涔, 涓涓笉鏄 NULL涔熶笉鏄 0鐨勫)銆
閲嶇偣: 鍋囧浣犱娇鐢ㄥ崟INSERT璇彞鎻掑叆澶氫釜琛岋紝 LAST_INSERT_ID() 鍙杩斿洖鎻掑叆鐨绗竴琛屼骇鐢熺殑鍊笺傚叾鍘熷洜鏄繖浣夸緷闈犲叾瀹冩湇鍔″櫒澶嶅埗鍚屾牱鐨 INSERT璇彞鍙樺緱绠鍗曘
渚嬪锛
mysql> USE test;
Database changed
mysql> CREATE TABLE t (
-> id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
-> name VARCHAR(10) NOT NULL
-> );
Query OK, 0 rows affected (0.09 sec)
mysql> INSERT INTO t VALUES (NULL, 'Bob');
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM t;
+----+------+
| id | name |
+----+------+
| 1 | Bob |
+----+------+
1 row in set (0.01 sec)
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> INSERT INTO t VALUES
-> (NULL, 'Mary'), (NULL, 'Jane'), (NULL, 'Lisa');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t;
+----+------+
| id | name |
+----+------+
| 1 | Bob |
| 2 | Mary |
| 3 | Jane |
| 4 | Lisa |
+----+------+
4 rows in set (0.01 sec)
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)
铏界劧绗簩涓棶璇㈠皢3 涓柊琛屾彃鍏 t, 瀵硅繖浜涜鐨勭涓琛屼骇鐢熺殑 ID 涓 2, 杩欎篃鏄 LAST_INSERT_ID()杩斿洖鐨勫笺
鍋囧浣犱娇鐢 INSERT IGNORE鑰岃褰曡蹇界暐锛屽垯AUTO_INCREMENT 璁℃暟鍣ㄤ笉浼氬閲忥紝鑰 LAST_INSERT_ID() 杩斿洖0, 杩欏弽鏄犲嚭娌℃湁鎻掑叆浠讳綍璁板綍銆
鑻ョ粰鍑轰綔涓哄埌LAST_INSERT_ID()鐨勫弬鏁expr 锛屽垯鍙傛暟鐨勫艰鍑芥暟杩斿洖锛屽苟浣滀负琚LAST_INSERT_ID()杩斿洖鐨勪笅涓涓艰岃璁板繂銆傝繖鍙敤浜庢ā鎷熷簭鍒楋細
o mysql> CREATE TABLE sequence (id INT NOT NULL);
o mysql> INSERT INTO sequence VALUES (0);
o mysql> UPDATE sequence SET id=LAST_INSERT_ID(id+1);
o mysql> SELECT LAST_INSERT_ID();
UPDATE 璇彞浼氬鍔犻『搴忚鏁板櫒骞跺紩鍙戝悜LAST_INSERT_ID() 鐨勪笅涓娆¤皟鐢紝鐢ㄦ潵杩斿洖鍗囩骇鍚庣殑鍊笺 SELECT 璇彞浼氭绱㈣繖涓笺 mysql_insert_id() C API鍑芥暟涔熷彲鐢ㄤ簬鑾峰彇杩欎釜鍊笺 瑙25.2.3.36鑺傦紝鈥渕ysql_insert_id()鈥.
浣犲彲浠ヤ笉鐢ㄨ皟鐢LAST_INSERT_ID()鑰屼骇鐢熷簭鍒楋紝浣嗚繖鏍蜂娇鐢ㄨ繖涓嚱鏁扮殑鏁堢敤鍦ㄤ簬 ID鍊艰淇濆瓨鍦ㄦ湇鍔″櫒涓紝浣滀负鑷姩浜х敓鐨勫笺傚畠閫傜敤浜庡涓敤鎴凤紝鍘熷洜鏄涓敤鎴峰潎鍙娇鐢 UPDATE璇彞骞剁敤SELECT璇彞(鎴mysql_insert_id()),寰楀埌浠栦滑鑷繁鐨勫簭鍒楀硷紝鑰屼笉浼氬奖鍝嶅叾瀹冧骇鐢熶粬浠嚜宸辩殑搴忓垪鍊肩殑瀹㈡埛绔垨琚叾瀹冧骇鐢熶粬浠嚜宸辩殑搴忓垪鍊肩殑瀹㈡埛绔墍褰卞搷銆
娉ㄦ剰锛 mysql_insert_id() 浠呬細鍦INSERT 鍜UPDATE璇彞鍚庨潰琚崌绾э紝 鍥犳浣犱笉鑳藉湪鎵ц浜嗗叾瀹冭濡SELECT鎴 SET 杩欐牱鐨SQL璇彞鍚庝娇鐢 C API 鍑芥暟鏉ユ壘鍥 LAST_INSERT_ID(expr) 瀵瑰簲鐨勫笺
ROW_COUNT()杩斿洖琚墠闈㈣鍙ュ崌绾х殑銆佹彃鍏ョ殑鎴栧垹闄ょ殑琛屾暟銆 杩欎釜琛屾暟鍜 mysql 瀹㈡埛绔樉绀虹殑琛屾暟鍙 mysql_affected_rows() C API 鍑芥暟杩斿洖鐨勫肩浉鍚屻
mysql> INSERT INTO t VALUES(1),(2),(3);
闂瀹屾垚, 琛ㄤ腑鏈3 琛 (0.00绉)
璁板綍: 3 閲嶅: 0 璀﹀憡: 0
mysql> SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
| 3 |
+-------------+
琛ㄤ腑鏈1琛 (0.00 绉)
mysql> DELETE FROM t WHERE i IN(1,2);
闂瀹屾垚, 鎵惧埌2 琛 (0.00 绉)
mysql> SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
| 2 |
+-------------+
琛ㄤ腑鏈1琛 (0.00 绉)
杩欎釜鍑芥暟鍜 DATABASE()鍏锋湁鐩稿悓鐨勬剰涔夈
SESSION_USER()鍜 USER()鍏锋湁鐩稿悓鐨勬剰涔夈
SYSTEM_USER()鍚 USER()鍏锋湁鐩稿悓鐨勬剰涔夈
杩斿洖褰撳墠 MySQL鐢ㄦ埛鍚嶅拰鏈轰富鍚/
mysql> SELECT USER();
-> 'davida@localhost'
杩欎釜鍊兼寚绀轰簡浣犳寚瀹氱殑杩炴帴鏈嶅姟鍣ㄦ椂鐨勭敤鎴峰悕,鍙婁綘鎵杩炴帴鐨勫鎴蜂富鏈恒傝繖涓煎彲浠ュ拰CURRENT_USER() 鐨勫间笉鍚屻
浣犲彲浠ヨ繖鏍锋彁鍙栫敤鎴峰悕閮ㄥ垎锛
mysql> SELECT SUBSTRING_INDEX(USER(),'@',1);
-> 'davida'
鐢变簬 USER() 杩斿洖涓涓utf8 瀛楃闆嗕腑鐨勫硷紝浣犱篃搴旂‘淇'@' 瀛楃涓叉枃瀛楀湪璇ュ瓧绗﹂泦涓緱鍒拌В閲婏細
mysql> SELECT SUBSTRING_INDEX(USER(),_utf8'@',1);
-> 'davida'
杩斿洖鎸囩ず MySQL 鏈嶅姟鍣ㄧ増鏈殑瀛楃涓层傝繖涓瓧绗︿覆浣跨敤 utf8 瀛楃闆嗐
mysql> SELECT VERSION();
-> '5.1.2-alpha-standard'
娉ㄦ剰锛屽亣濡備綘鐨勭増鏈瓧绗︿覆浠-log缁撳熬锛岃繖璇存槑鐧诲綍宸茶婵娲汇
杩斿洖涓涓〃鍒楃殑榛樿鍊笺傝嫢璇ュ垪娌℃湁榛樿鍊煎垯浼氫骇鐢熼敊璇
mysql> UPDATE t SET i = DEFAULT(i)+1 WHERE id < 100;
灏嗘暟瀛X 鐨勬牸寮忓啓涓'#,###,###.##',浠ュ洓鑸嶄簲鍏ョ殑鏂瑰紡淇濈暀灏忔暟鐐瑰悗 D 浣嶏紝 骞跺皢缁撴灉浠ュ瓧绗︿覆鐨勫舰寮忚繑鍥炪傝嫢 D 涓 0, 鍒欒繑鍥炵粨鏋滀笉甯︽湁灏忔暟鐐癸紝鎴栦笉鍚皬鏁伴儴鍒嗐
mysql> SELECT FORMAT(12332.123456, 4);
-> '12,332.1235'
mysql> SELECT FORMAT(12332.1,4);
-> '12,332.1000'
mysql> SELECT FORMAT(12332.2,0);
-> '12,332'
璁炬硶浣跨敤瀛楃涓str 缁欏畾鐨勫悕瀛楀緱鍒颁竴涓攣锛 瓒呮椂涓timeout 绉掋傝嫢鎴愬姛寰楀埌閿侊紝鍒欒繑鍥 1锛岃嫢鎿嶄綔瓒呮椂鍒欒繑鍥0 (渚嬪,鐢变簬鍙︿竴涓鎴风宸叉彁鍓嶅皝閿佷簡杩欎釜鍚嶅瓧 ),鑻ュ彂鐢熼敊璇垯杩斿洖NULL (璇稿缂轰箯璁板繂鎴栫嚎绋mysqladmin kill 琚柇寮 )銆傚亣濡備綘鏈変竴涓敤GET_LOCK()寰楀埌鐨勯攣锛屽綋浣犳墽琛RELEASE_LOCK()鎴栦綘鐨勮繛鎺ユ柇寮(姝e父鎴栭潪姝e父)鏃讹紝杩欎釜閿佸氨浼氳В闄ゃ
杩欎釜鍑芥暟鍙敤浜庢墽琛屽簲鐢ㄧ▼搴忛攣鎴栨ā鎷熻褰曢攣瀹氥傚悕绉拌閿佸畾鍦ㄦ湇鍔″櫒鑼冨洿鍐呫傚亣濡備竴涓悕瀛楀凡缁忚涓涓鎴风灏侀攣锛 GET_LOCK() 浼氬皝閿佹潵鑷彟涓涓鎴风鐢宠灏侀攣鍚屼竴涓悕瀛楃殑浠讳綍璇锋眰銆傝繖浣垮涓涓皝閿佸悕杈炬垚鍗忚鐨勫鎴风浣跨敤杩欎釜鍚嶅瓧鍚堜綔鎵ц寤鸿閿併傜劧鑰岃鐭ラ亾瀹冧篃鍏佽涓嶅湪涓缁勫悎浣滃鎴风涓殑涓涓鎴风灏侀攣鍚嶅瓧锛屼笉璁烘槸鏈嶅焦鐨勮繕鏄潪鏁呮剰鐨勶紝杩欐牱闃绘浠讳綍鍚堜綔涓殑瀹㈡埛绔皝閿佽繖涓悕瀛椼備竴涓噺灏戣繖绉嶆儏鍐靛彂鐢熺殑鍔炴硶灏辨槸浣跨敤鏁版嵁搴撶壒瀹氱殑鎴栧簲鐢ㄧ▼搴忕壒瀹氱殑灏侀攣鍚嶃備緥濡傦紝 浣跨敤db_name.str鎴 app_name.str 褰㈠紡鐨勫皝閿佸悕銆
mysql> SELECT GET_LOCK('lock1',10);
-> 1
mysql> SELECT IS_FREE_LOCK('lock2');
-> 1
mysql> SELECT GET_LOCK('lock2',10);
-> 1
mysql> SELECT RELEASE_LOCK('lock2');
-> 1
mysql> SELECT RELEASE_LOCK('lock1');
-> NULL
娉ㄦ剰锛岀浜屼釜 RELEASE_LOCK()璋冪敤杩斿洖 NULL 锛屽師鍥犳槸閿'lock1' 鏉浜屼釜GET_LOCK()璋冪敤瑙e紑銆
缁欏嚭涓涓綔涓哄瓧绗︿覆鐨勭綉缁滃湴鍧鐨勭偣鍦板潃琛ㄧず锛岃繑鍥炰竴涓唬琛ㄨ鍦板潃鏁板肩殑鏁存暟銆傚湴鍧鍙互鏄4鎴8姣旂壒鍦板潃銆
mysql> SELECT INET_ATON('209.207.224.40');
-> 3520061480
浜х敓鐨勬暟瀛楁绘槸鎸夌収缃戠粶瀛楄妭椤哄簭銆傚涓婇潰鐨勪緥瀛愶紝鏁板瓧鎸夌収 209脳2563 + 207脳2562 + 224脳256 + 40 杩涜璁$畻銆
INET_ATON() 涔熻兘鐞嗚В鐭牸寮 IP 鍦板潃锛
mysql> SELECT INET_ATON('127.0.0.1'), INET_ATON('127.1');
-> 2130706433, 2130706433
娉ㄩ噴锛 鍦ㄥ瓨鍌ㄧ敱INET_ATON() 浜х敓鐨勫兼椂锛屾帹鑽愪綘浣跨敤 INT UNSIGNED 鍒椼傚亣濡備綘浣跨敤 (甯︾鍙) INT鍒, 鍒欑浉搴旂殑绗竴涓叓浣嶇粍澶т簬127鐨IP 鍦板潃鍊间細琚埅鑷 2147483647 (鍗, INET_ATON('127.255.255.255') 鎵杩斿洖鐨勫)銆傝鍙傝11.2鑺傦紝鈥滄暟鍊肩被鍨嬧銆
缁欏畾涓涓暟瀛楃綉缁滃湴鍧 (4 鎴 8 姣旂壒),杩斿洖浣滀负瀛楃涓茬殑璇ュ湴鍧鐨勭數鍦板潃琛ㄧず銆
mysql> SELECT INET_NTOA(3520061480);
-> '209.207.224.40'
妫鏌ュ悕涓str鐨勯攣鏄惁鍙互浣跨敤 (鎹㈣█涔,娌℃湁琚皝閿)銆傝嫢閿佸彲浠ヤ娇鐢紝鍒欒繑鍥 1 (娌℃湁浜哄湪鐢ㄨ繖涓攣), 鑻ヨ繖涓攣姝e湪琚娇鐢紝鍒欒繑鍥0 锛屽嚭鐜伴敊璇垯杩斿洖 NULL (璇稿涓嶆纭殑鍙傛暟 )銆
妫鏌ュ悕涓str鐨勯攣鏄惁姝e湪琚娇鐢(鎹㈣█涔,琚皝閿)銆傝嫢琚皝閿侊紝鍒欒繑鍥炰娇鐢ㄨ閿佺殑瀹㈡埛绔殑杩炴帴鏍囪瘑绗︺傚惁鍒欒繑鍥 NULL銆
璇ュ嚱鏁板浜庢帶鍒朵富浠庡悓姝ュ緢鏈夌敤澶勩傚畠浼氭寔缁皝閿侊紝鐩村埌浠庤澶囬槄璇诲拰搴旂敤涓绘満璁板綍涓墍鏈夎ˉ鍏呰祫鏂欏埌鎸囧畾鐨勪綅缃傝繑鍥炲兼槸鍏朵负鍒拌揪鎸囧畾浣嶇疆鑰屽繀椤荤瓑寰呯殑璁板綍浜嬩欢鐨勬暟鐩傝嫢浠庤澶SQL绾跨▼娌℃湁琚惎鍔ㄣ佷粠璁惧涓绘満淇℃伅灏氭湭鍒濆鍖栥佸弬鏁颁笉姝g‘鎴栧嚭鐜颁换浣曢敊璇紝鍒欒鍑芥暟杩斿洖 NULL銆傝嫢瓒呮椂鏃堕棿琚瓒呰繃锛屽垯杩斿洖-1銆鑻鍦MASTER_POS_WAIT() 绛夊緟鏈熼棿锛屼粠璁惧SQL绾跨▼涓锛屽垯璇ュ嚱鏁拌繑鍥 NULL銆傝嫢浠庤澶囩敱鎸囧畾浣嶇疆閫氳繃锛屽垯鍑芥暟浼氱珛鍗宠繑鍥炵粨鏋溿
鍋囧宸茬粡鎸囧畾浜嗕竴涓瓒呮椂鏃堕棿鍊硷紝褰 瓒呮椂鏃堕棿 绉掓暟缁忚繃鍚MASTER_POS_WAIT()浼氬仠姝㈢瓑寰呫瓒呮椂鏃堕棿 蹇呴』澶т簬 0锛涗竴涓负闆舵垨涓鸿礋鍊肩殑 瓒呮椂鏃堕棿 琛ㄧず娌℃湁瓒呭競鏃堕棿銆
杩斿洖缁欏畾鍊笺 褰撶敤鏉ヤ骇鐢熶竴涓粨鏋滈泦鍚堝垪鏃, NAME_CONST()淇冧娇璇ュ垪浣跨敤缁欏畾鍚嶇О銆
mysql> SELECT NAME_CONST('myname', 14);
+--------+
| myname |
+--------+
| 14 |
+--------+
杩欎釜鍑芥暟琚坊鍔犺繘 MySQL 5.0.12銆傚畠鍙仛鍐呴儴浣跨敤銆 鏈嶅姟鍣ㄥ湪涔﹀啓鏉ヨ嚜鍖呭惈灞閮ㄧ▼搴忓彉閲忕殑瀛樺偍绋嬪簭鐨勮鍙ユ椂浼氱敤鍒板畠锛岃瑙20.4鑺傦紝鈥滃瓨鍌ㄥ瓙绋嬪簭鍜岃Е鍙戠▼搴忕殑浜岃繘鍒舵棩蹇楀姛鑳解銆備綘鍙兘浼氬湪mysqlbinlog 鐨勪功姗变腑鐪嬪埌杩欎釜鍑芥暟銆
瑙e紑琚GET_LOCK()鑾峰彇鐨勶紝鐢ㄥ瓧绗︿覆str 鎵鍛藉悕鐨勯攣銆傝嫢閿佽瑙e紑锛屽垯杩斿洖 1锛岃嫢鏀圭嚎绋嬪皻鏈垱寤洪攣锛屽垯杩斿洖0 (姝ゆ椂閿佹病鏈夎瑙e紑 ), 鑻ュ懡鍚嶇殑閿佷笉瀛樺湪锛屽垯杩斿洖 NULL銆傝嫢璇ラ攣浠庢湭琚GET_LOCK()鐨勮皟鐢ㄨ幏鍙栵紝鎴栭攣宸茬粡琚彁鍓嶈В寮锛屽垯璇ラ攣涓嶅瓨鍦ㄣ
DO 璇彞鍜RELEASE_LOCK()鍚屾椂浣跨敤寰堟柟渚裤傝鍙傝13.2.2鑺傦紝鈥淒O璇硶鈥銆
鐫$湢(鏆傚仠) 鏃堕棿涓duration 鍙傛暟缁欏畾鐨勭鏁帮紝鐒跺悗杩斿洖 0銆傝嫢 SLEEP() 琚腑鏂,瀹冧細杩斿洖 1銆 duration 鎴栬鎴栧寘鎷竴涓粰瀹氱殑浠ュ井绉掍负鍗曚綅鐨勫垎鏁伴儴鍒嗐
杩斿洖涓涓氱敤鍞竴鏍囪瘑绗(UUID) 锛屽叾浜х敓鐨勬牴鎹槸銆DCE 1.1: 杩滅▼杩囩▼璋冪敤銆 (闄勫綍A) CAE (鍏叡搴旂敤杞欢鐜) 鐨勮鏄庯紝璇ヤ綔鍝佷簬1997骞10鏈堢敱 The Open Group 鍑虹増 (鏂囦欢缂栧彿 C706, http://www.opengroup.org/public/pubs/catalog/c706.htm).
UUID琚璁℃垚涓涓湪鏃堕棿鍜岀┖闂翠笂閮界嫭涓鏃犱簩鐨勬暟瀛椼2涓UUID() 鐨勮皟鐢ㄥ簲浜х敓2涓笉鍚岀殑鍊硷紝鍗充娇杩欎簺璋冪敤鐨勬墽琛屾槸鍦ㄤ袱涓簰涓嶇浉杩炵殑鍗曠嫭鐢佃剳涓婅繘琛屻
UUID 鏄竴涓敱5浣嶅崄鍏繘鍒舵暟鐨勫瓧绗︿覆琛ㄧず鐨128姣旂壒鏁板瓧 锛屽叾鏍煎紡涓 aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee 锛
鐩墠,涓涓晫闈㈢殑 MAC 鍦板潃灏借FreeBSD 鍜 Linux鑰冭檻鍒般傚湪鍏跺畠鎿嶄綔绯荤粺涓, MySQL浣跨敤闅忔満浜х敓鐨 48姣旂壒鏁板瓧銆
mysql> SELECT UUID();
-> '6ccd780c-baba-1026-9564-0040f4311e29'
娉ㄦ剰锛 UUID() 涓嶆敮鎸佸鍒跺姛鑳姐
鍦ㄤ竴涓INSERT 鈥 ON DUPLICATE KEY UPDATE 鈥﹁鍙ヤ腑锛屼綘鍙互鍦UPDATE 瀛愬彞涓娇鐢 VALUES(col_name)鍑芥暟锛岀敤鏉ヨ闂潵鑷璇彞鐨INSERT 閮ㄥ垎鐨勫垪鍊笺傛崲瑷涔嬶紝UPDATE 瀛愬彞涓殑 VALUES(col_name) 璁块棶闇瑕佽鎻掑叆鐨col_name 鐨勫,骞朵笉浼氬彂鐢熼噸澶嶉敭鍐茬獊銆傝繖涓嚱鏁板湪澶氳鎻掑叆涓壒鍒湁鐢ㄣ VALUES()鍑芥暟鍙湪INSERT ... UPDATE 璇彞涓湁鎰忎箟锛岃屽湪鍏跺畠鎯呭喌涓嬪彧浼氳繑鍥 NULL銆傝鍙傝13.2.4鑺傦紝鈥淚NSERT璇硶鈥.
mysql> INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
-> ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
鏈珷璁鸿堪浜嗙敤浜庝竴缁勬暟鍊兼搷浣滅殑 group (闆嗗悎)鍑芥暟銆傞櫎闈炲彟浣滆鏄庯紝 group 鍑芥暟浼氬拷鐣 NULL 鍊笺
鍋囧浣犲湪涓涓笉鍖呭惈 ROUP BY瀛愬彞鐨勮鍙ヤ腑浣跨敤涓涓 group鍑芥暟 锛屽畠鐩稿綋浜庡鎵鏈夎杩涜鍒嗙粍銆
杩斿洖expr 鐨勫钩鍧囧笺 DISTINCT 閫夐」鍙敤浜庤繑鍥 expr鐨勪笉鍚屽肩殑骞冲潎鍊笺
鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯AVG()杩斿洖 NULL 銆
mysql> SELECT student_name, AVG(test_score)
-> FROM student
-> GROUP BY student_name;
杩斿洖expr涓墍鏈夋瘮鐗圭殑 bitwise AND 銆傝绠楁墽琛岀殑绮剧‘搴︿负64姣旂壒(BIGINT) 銆
鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯杩欎釜鍑芥暟杩斿洖 18446744073709551615 銆(杩欐槸鏃犵鍙 BIGINT 鍊硷紝鎵鏈夋瘮鐗硅璁剧疆涓 1锛銆
杩斿洖expr 涓墍鏈夋瘮鐗圭殑bitwise OR銆傝绠楁墽琛岀殑绮剧‘搴︿负64姣旂壒(BIGINT) 銆
鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯鍑芥暟杩斿洖 0 銆
杩斿洖expr 涓墍鏈夋瘮鐗圭殑bitwise XOR銆傝绠楁墽琛岀殑绮剧‘搴︿负64姣旂壒(BIGINT) 銆
鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯鍑芥暟杩斿洖 0 銆
杩斿洖SELECT璇彞妫绱㈠埌鐨勮涓潪NULL鍊肩殑鏁扮洰銆
鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯COUNT() 杩斿洖 0 銆
mysql> SELECT student.student_name,COUNT(*)
-> FROM student,course
-> WHERE student.student_id=course.student_id
-> GROUP BY student_name;
COUNT(*) 鐨勭◢寰笉鍚屼箣澶勫湪浜庯紝瀹冭繑鍥炴绱㈣鐨勬暟鐩紝 涓嶈鍏舵槸鍚﹀寘鍚 NULL鍊笺
SELECT 浠庝竴涓〃涓绱紝鑰屼笉妫绱㈠叾瀹冪殑鍒楋紝骞朵笖娌℃湁 WHERE瀛愬彞鏃讹紝 COUNT(*)琚紭鍖栧埌鏈蹇殑杩斿洖閫熷害銆備緥濡傦細
mysql> SELECT COUNT(*) FROM student;
杩欎釜浼樺寲浠呴傜敤浜 MyISAM琛, 鍘熷洜鏄繖浜涜〃绫诲瀷浼氬偍瀛樹竴涓嚱鏁拌繑鍥炶褰曠殑绮剧‘鏁伴噺锛岃屼笖闈炲父瀹规槗璁块棶銆瀵逛簬浜嬪姟鍨嬬殑瀛樺偍寮曟搸(InnoDB, BDB), 瀛樺偍涓涓簿纭鏁扮殑闂姣旇緝澶氾紝鍘熷洜鏄彲鑳戒細鍙戠敓澶氶噸浜嬬墿澶勭悊, 鑰屾瘡涓兘鍙兘浼氬琛屾暟浜х敓褰卞搷銆
杩斿洖涓嶅悓鐨勯潪NULL鍊兼暟鐩
鑻ユ壘涓嶅埌鍖归厤鐨勯」锛屽垯COUNT(DISTINCT)杩斿洖 0 銆
mysql> SELECT COUNT(DISTINCT results) FROM student;
鍦MySQL涓, 浣犻氳繃缁欏畾涓涓〃杈惧紡鍒楄〃鑰岃幏鍙栦笉鍖呭惈NULL 涓嶅悓琛ㄨ揪寮忕粍鍚堢殑鏁扮洰銆傚湪鏍囧噯 SQL涓,浣犲皢蹇呴』鍦COUNT(DISTINCT ...)涓繛鎺ユ墍鏈夎〃杈惧紡銆
璇ュ嚱鏁拌繑鍥炲甫鏈夋潵鑷竴涓粍鐨勮繛鎺ョ殑闈NULL鍊肩殑瀛楃涓茬粨鏋溿傚叾瀹屾暣鐨勮娉曞涓嬫墍绀猴細
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
mysql> SELECT student_name,
-> GROUP_CONCAT(test_score)
-> FROM student
-> GROUP BY student_name;
Or:
mysql> SELECT student_name,
-> GROUP_CONCAT(DISTINCT test_score
-> ORDER BY test_score DESC SEPARATOR ' ')
-> FROM student
-> GROUP BY student_name;
鍦MySQL涓,浣犲彲浠ヨ幏鍙栬〃杈惧紡缁勫悎鐨勮繛鎺ュ笺備綘鍙互浣跨敤DISTINCT鍒犲幓閲嶅鍊笺傚亣鑻ヤ綘甯屾湜澶氱粨鏋滃艰繘琛屾帓搴忥紝鍒欏簲璇ヤ娇鐢 ORDER BY瀛愬彞銆傝嫢瑕佹寜鐩稿弽椤哄簭鎺掑垪锛屽皢 DESC (閫掑噺) 鍏抽敭璇嶆坊鍔犲埌浣犺鐢ORDER BY 瀛愬彞杩涜鎺掑簭鐨勫垪鍚嶇О涓傞粯璁ら『搴忎负鍗囧簭锛涘彲浣跨敤ASC灏嗗叾鏄庣‘鎸囧畾銆 SEPARATOR 鍚庨潰璺熼殢搴旇琚彃鍏ョ粨鏋滅殑鍊间腑闂寸殑瀛楃涓插笺傞粯璁や负閫楀彿 (鈥,鈥)銆傞氳繃鎸囧畾SEPARATOR '' 锛屼綘鍙互鍒犻櫎鎵鏈夊垎闅旂銆
浣跨敤group_concat_max_len绯荤粺鍙橀噺锛屼綘鍙互璁剧疆鍏佽鐨勬渶澶ч暱搴︺ 绋嬪簭涓繘琛岃繖椤规搷浣滅殑璇硶濡備笅锛屽叾涓 val 鏄竴涓棤绗﹀彿鏁存暟锛
SET [SESSION | GLOBAL] group_concat_max_len = val;
鑻ュ凡缁忚缃簡鏈澶ч暱搴︼紝 鍒欑粨鏋滆鎴嚦杩欎釜鏈澶ч暱搴︺
杩斿洖expr 鐨勬渶灏忓煎拰鏈澶у笺 MIN() 鍜 MAX() 鐨勫彇鍊煎彲浠ユ槸涓涓瓧绗︿覆鍙傛暟锛涘湪杩欎簺鎯呭喌涓嬶紝 瀹冧滑杩斿洖鏈灏忔垨鏈澶у瓧绗︿覆鍊笺傝鍙傝7.4.5鑺傦紝鈥淢ySQL濡備綍浣跨敤绱㈠紩鈥銆 DISTINCT鍏抽敭璇嶅彲浠ヨ鐢ㄦ潵鏌ユ壘expr 鐨勪笉鍚屽肩殑鏈灏忔垨鏈澶у硷紝鐒惰岋紝杩欎骇鐢熺殑缁撴灉涓庣渷鐣DISTINCT 鐨勭粨鏋滅浉鍚屻
鑻ユ壘涓嶅埌鍖归厤鐨勮锛MIN()鍜MAX()杩斿洖 NULL 銆
mysql> SELECT student_name, MIN(test_score), MAX(test_score)
-> FROM student
-> GROUP BY student_name;
瀵逛簬MIN()銆 MAX()鍜屽叾瀹冮泦鍚堝嚱鏁帮紝 MySQL褰撳墠鎸夌収瀹冧滑鐨勫瓧绗︿覆鍊艰岄潪瀛楃涓插湪闆嗗悎涓殑鐩稿叧浣嶇疆姣旇緝 ENUM鍜 SET 鍒椼傝繖鍚ORDER BY姣旇緝浜岃呯殑鏂瑰紡鏈夋墍涓嶅悓銆傝繖涓鐐瑰簲璇ュ湪MySQL鐨勬湭鏉ョ増鏈腑寰楀埌鏀瑰杽銆
杩斿洖expr 鐨勬讳綋鏍囧噯鍋忓樊銆傝繖鏄爣鍑 SQL 鐨勫欢浼搞傝繖涓嚱鏁扮殑STDDEV() 褰㈠紡鐢ㄦ潵鎻愪緵鍜Oracle 鐨勫吋瀹规с傚彲浣跨敤鏍囧噯SQL鍑芥暟 STDDEV_POP() 杩涜浠f浛銆
鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯杩欎簺鍑芥暟杩斿洖 NULL 銆
杩斿洖expr 鐨勬讳綋鏍囧噯鍋忓樊(VAR_POP()鐨勫钩鏂规牴)銆備綘涔熷彲浠ヤ娇鐢 STD() 鎴STDDEV(), 瀹冧滑鍏锋湁鐩稿悓鐨勬剰涔夛紝鐒惰屼笉鏄爣鍑嗙殑 SQL銆
鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯STDDEV_POP()杩斿洖 NULL銆
杩斿洖expr 鐨勬牱鏈爣鍑嗗樊 ( VAR_SAMP()鐨勫钩鏂规牴)銆
鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯STDDEV_SAMP() 杩斿洖 NULL 銆
杩斿洖expr 鐨勬绘暟銆 鑻ヨ繑鍥為泦鍚堜腑鏃犱换浣曡锛屽垯 SUM() 杩斿洖NULL銆DISTINCT 鍏抽敭璇嶅彲鐢ㄤ簬 MySQL 5.1 涓紝姹傚緱expr 涓嶅悓鍊肩殑鎬诲拰銆
鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯SUM()杩斿洖 NULL銆
杩斿洖expr 鎬讳綋鏍囧噯鏂瑰樊銆傚畠灏嗚瑙嗕负鎬讳綋锛岃屼笉鏄竴涓牱鏈紝 鎵浠ュ畠灏嗚鏁颁綔涓哄垎姣嶃備綘涔熷彲浠ヤ娇鐢 VARIANCE(),瀹冨叿鏈夌浉鍚岀殑鎰忎箟鐒惰屼笉鏄 鏍囧噯鐨 SQL銆
鑻ユ壘涓嶅埌鍖归厤鐨勯」锛屽垯VAR_POP()杩斿洖NULL銆
杩斿洖expr 鐨勬牱鏈柟宸傛洿纭垏鐨勮锛屽垎姣嶇殑鏁板瓧鏄鏁板噺鍘1銆
鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯VAR_SAMP()杩斿洖NULL銆
杩斿洖expr 鐨勬讳綋鏍囧噯鏂瑰樊銆傝繖鏄爣鍑SQL 鐨勫欢浼搞傚彲浣跨敤鏍囧噯SQL 鍑芥暟 VAR_POP() 杩涜浠f浛銆
鑻ユ壘涓嶅埌鍖归厤鐨勯」锛屽垯VARIANCE()杩斿洖NULL銆
GROUP BY瀛愬彞鍏佽涓涓皢棰濆琛屾坊鍔犲埌绠鐣ヨ緭鍑虹 WITH ROLLUP 淇グ绗︺傝繖浜涜浠h〃楂樺眰(鎴栭珮鑱氶泦)绠鐣ユ搷浣溿ROLLUP 鍥犺屽厑璁镐綘鍦ㄥ灞傚垎鏋愮殑瑙掑害鍥炵瓟鏈夊叧闂鐨勯棶棰樸備緥濡傦紝瀹冨彲浠ョ敤鏉ュ悜OLAP (鑱旀満鍒嗘瀽澶勭悊) 鎿嶄綔鎻愪緵鏀寔銆
璁炬兂涓涓悕涓sales 鐨勮〃鍏锋湁骞翠唤銆佸浗瀹躲佷骇鍝佸強璁板綍閿鍞埄娑︾殑鍒╂鼎鍒楋細
CREATE TABLE sales
(
year INT NOT NULL,
country VARCHAR(20) NOT NULL,
product VARCHAR(32) NOT NULL,
profit INT
);
鍙互浣跨敤杩欐牱鐨勭畝鍗GROUP BY锛屾瘡骞村琛ㄧ殑鍐呭鍋氫竴娆℃荤粨锛
mysql> SELECT year, SUM(profit) FROM sales GROUP BY year;
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 | 4525 |
| 2001 | 3010 |
+------+-------------+
杩欎釜杈撳嚭缁撴灉鏄剧ず浜嗘瘡骞寸殑鎬诲埄娑︼紝 浣嗗鏋滀綘涔熸兂纭畾鎵鏈夊勾浠界殑鎬诲埄娑︼紝浣犲繀椤昏嚜宸辩疮鍔犳瘡骞寸殑鍗曚釜鍊兼垨杩愯涓涓姞娉曡闂
鎴栬呬綘鍙互浣跨敤 ROLLUP, 瀹冭兘鐢ㄤ竴涓棶璇㈡彁渚涘弻灞傚垎鏋愩傚皢涓涓 WITH ROLLUP淇グ绗︽坊鍔犲埌GROUP BY 璇彞锛屼娇璇㈤棶浜х敓鍙︿竴琛岀粨鏋滐紝璇ヨ鏄剧ず浜嗘墍鏈夊勾浠界殑鎬讳环鍊硷細
mysql> SELECT year, SUM(profit) FROM sales GROUP BY year WITH ROLLUP;
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 | 4525 |
| 2001 | 3010 |
| NULL | 7535 |
+------+-------------+
鎬昏楂樿仛闆嗚琚勾浠藉垪涓殑NULL鍊兼爣鍑恒
褰撴湁澶氶噸 GROUP BY 鍒楁椂锛ROLLUP浜х敓鐨勬晥鏋滄洿鍔犲鏉傘傝繖鏃讹紝姣忔鍦ㄩ櫎浜嗘渶鍚庝竴涓垎绫诲垪涔嬪鐨勪换浣曞垪鍑虹幇涓涓 鈥break鈥 (鍊肩殑鏀瑰彉) 锛屽垯闂浼氫骇鐢熶竴涓珮鑱氶泦绱琛屻
渚嬪锛屽湪娌℃湁 ROLLUP鐨勬儏鍐典笅锛屼竴涓互骞淬佸浗瀹跺拰浜у搧涓哄熀纭鐨勫叧浜 sales 琛ㄧ殑涓瑙堣〃鍙兘濡備笅鎵绀猴細
mysql> SELECT year, country, product, SUM(profit)
-> FROM sales
-> GROUP BY year, country, product;
+------+---------+------------+-------------+
| year | country | product | SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer | 1500 |
| 2000 | Finland | Phone | 100 |
| 2000 | India | Calculator | 150 |
| 2000 | India | Computer | 1200 |
| 2000 | USA | Calculator | 75 |
| 2000 | USA | Computer | 1500 |
| 2001 | Finland | Phone | 10 |
| 2001 | USA | Calculator | 50 |
| 2001 | USA | Computer | 2700 |
| 2001 | USA | TV | 250 |
+------+---------+------------+-------------+
琛ㄧず鎬诲肩殑杈撳嚭缁撴灉浠呬綅浜庡勾/鍥藉/浜у搧鐨勫垎鏋愮骇鍒傚綋娣诲姞浜 ROLLUP鍚庯紝 闂浼氫骇鐢熶竴浜涢澶栫殑琛岋細
mysql> SELECT year, country, product, SUM(profit)
-> FROM sales
-> GROUP BY year, country, product WITH ROLLUP;
+------+---------+------------+-------------+
| year | country | product | SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer | 1500 |
| 2000 | Finland | Phone | 100 |
| 2000 | Finland | NULL | 1600 |
| 2000 | India | Calculator | 150 |
| 2000 | India | Computer | 1200 |
| 2000 | India | NULL | 1350 |
| 2000 | USA | Calculator | 75 |
| 2000 | USA | Computer | 1500 |
| 2000 | USA | NULL | 1575 |
| 2000 | NULL | NULL | 4525 |
| 2001 | Finland | Phone | 10 |
| 2001 | Finland | NULL | 10 |
| 2001 | USA | Calculator | 50 |
| 2001 | USA | Computer | 2700 |
| 2001 | USA | TV | 250 |
| 2001 | USA | NULL | 3000 |
| 2001 | NULL | NULL | 3010 |
| NULL | NULL | NULL | 7535 |
+------+---------+------------+-------------+
瀵逛簬杩欎釜闂锛 娣诲姞ROLLUP 瀛愬彞浣挎潙杈撳嚭缁撴灉鍖呭惈浜嗗洓灞傚垎鏋愮殑绠鐣ヤ俊鎭紝鑰屼笉鍙槸涓涓笅闈㈡槸鎬庢牱瑙i噴 ROLLUP杈撳嚭锛
浣跨敤ROLLUP 鏃剁殑鍏跺畠娉ㄦ剰浜嬮」
浠ヤ笅鍚勯」鍒楀嚭浜嗕竴浜MySQL鎵цROLLUP鐨勭壒娈婄姸鎬侊細
褰撲綘浣跨敤 ROLLUP鏃, 浣犱笉鑳藉悓鏃朵娇鐢 ORDER BY瀛愬彞杩涜缁撴灉鎺掑簭銆傛崲瑷涔嬶紝 ROLLUP 鍜ORDER BY 鏄簰鐩告帓鏂ョ殑銆傜劧鑰岋紝浣犱粛鍙互瀵规帓搴忚繘琛屼竴浜涙帶鍒躲傚湪 MySQL涓紝 GROUP BY 鍙互瀵圭粨鏋滆繘琛屾帓搴忥紝鑰屼笖浣犲彲浠ュ湪GROUP BY鍒楄〃鎸囧畾鐨勫垪涓娇鐢ㄦ槑纭殑 ASC鍜DESC鍏抽敭璇嶏紝浠庤屽涓埆鍒楄繘琛屾帓搴忋 (涓嶈濡備綍鎺掑簭琚ROLLUP娣诲姞鐨勮緝楂樼骇鍒殑鎬昏琛屼粛鍑虹幇鍦ㄥ畠浠璁$畻鍑虹殑琛屽悗闈級銆
LIMIT鍙敤鏉ラ檺鍒惰繑鍥炲鎴风鐨勮鏁般LIMIT 鐢ㄥ湪 ROLLUP鍚庨潰, 鍥犳杩欎釜闄愬埗 浼氬彇娑堣ROLLUP娣诲姞鐨勮銆備緥濡:
mysql> SELECT year, country, product, SUM(profit)
-> FROM sales
-> GROUP BY year, country, product WITH ROLLUP
-> LIMIT 5;
+------+---------+------------+-------------+
| year | country | product | SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer | 1500 |
| 2000 | Finland | Phone | 100 |
| 2000 | Finland | NULL | 1600 |
| 2000 | India | Calculator | 150 |
| 2000 | India | Computer | 1200 |
+------+---------+------------+-------------+
灏ROLLUP鍚 LIMIT涓璧蜂娇鐢ㄥ彲鑳戒細浜х敓鏇村姞闅句互瑙i噴鐨勭粨鏋滐紝鍘熷洜鏄浜庣悊瑙i珮鑱氶泦琛岋紝浣犳墍鎺屾彙鐨勪笂涓嬫枃杈冨皯銆
鍦ㄦ瘡涓珮鑱氶泦琛屼腑鐨NULL 鎸囩ず绗︿細鍦ㄨ琛岃閫佽嚦瀹㈡埛绔椂浜х敓銆傛湇鍔″櫒浼氭煡鐪嬫渶宸﹁竟鐨勬敼鍙樺煎悗闈㈢殑GROUP BY瀛愬彞鎸囧畾鐨勫垪銆傚浜庝换浣曠粨鏋滈泦鍚堜腑鐨勶紝鏈変竴涓瘝鍖归厤杩欎簺鍚嶅瓧鐨勫垪, 鍏跺艰璁句负 NULL銆(鑻ヤ綘浣跨敤鍒楁暟瀛楁寚瀹氫簡鍒嗙粍鍒楋紝鍒欐湇鍔″櫒浼氶氳繃鏁板瓧纭畾灏嗗摢涓垪璁剧疆涓 NULL锛銆
鐢变簬鍦ㄩ珮鑱氶泦琛屼腑鐨 NULL鍊煎湪闂澶勭悊闃舵琚斁鍏ョ粨鏋滈泦鍚堜腑锛屼綘鏃犳硶灏嗗畠浠湪闂鏈韩涓綔涓NULL鍊兼楠屻備緥濡傦紝浣犳棤娉曞皢 HAVING product IS NULL 娣诲姞鍒伴棶璇腑锛屼粠鑰屽湪杈撳嚭缁撴灉涓垹鍘婚櫎浜嗛珮鑱氶泦琛屼互澶栫殑閮ㄥ垎銆
鍙︿竴鏂归潰, NULL鍊煎湪瀹㈡埛绔笉浠 NULL 鐨勫舰寮忓嚭鐜帮紝 鍥犺屽彲浠ヤ娇鐢ㄤ换浣MySQL瀹㈡埛绔紪绋嬫帴鍙h繘琛屾楠屻
MySQL 鎵╁睍浜 GROUP BY鐨勭敤閫旓紝鍥犳浣犲彲浠ヤ娇鐢SELECT 鍒楄〃涓笉鍑虹幇鍦GROUP BY璇彞涓殑鍒楁垨杩愮畻銆傝繖浠h〃 鈥滃璇ョ粍鐨勪换浣曞彲鑳藉 鈥濄備綘鍙互閫氳繃閬垮厤鎺掑簭鍜屽涓嶅繀瑕侀」鍒嗙粍鐨勫姙娉曞緱鍒板畠鏇村ソ鐨勬ц兘銆備緥濡傦紝鍦ㄤ笅鍒楅棶璇腑锛屼綘鏃犻』瀵customer.name 杩涜鍒嗙粍锛
mysql> SELECT order.custid, customer.name, MAX(payments)
-> FROM order,customer
-> WHERE order.custid = customer.custid
-> GROUP BY order.custid;
鍦ㄦ爣鍑SQL涓, 浣犲繀椤诲皢 customer.name娣诲姞鍒 GROUP BY瀛愬彞涓傚湪MySQL涓, 鍋囧浣犱笉鍦ANSI妯″紡涓繍琛岋紝鍒欒繖涓悕瀛楀氨鏄浣欑殑銆
鍋囧浣犱粠 GROUP BY 閮ㄥ垎鐪佺暐鐨勫垪鍦ㄨ缁勪腑涓嶆槸鍞竴鐨勶紝閭d箞涓嶈浣跨敤杩欎釜鍔熻兘! 浣犱細寰楀埌闈為娴嬫х粨鏋溿
鍦ㄦ湁浜涙儏鍐典笅,浣犲彲浠ヤ娇鐢MIN()鍜MAX() 鑾峰彇涓涓壒娈婄殑鍒楀硷紝鍗充娇浠栦笉鏄敮涓鐨勩備笅闈㈢粰鍑轰簡鏉ヨ嚜鍖呭惈鎺掑簭鍒椾腑鏈灏忓肩殑鍒椾腑鐨勫硷細
SUBSTR(MIN(CONCAT(RPAD(sort,6,' '),column)),7)
See 3.6.4鑺傦紝鈥滄嫢鏈夋煇涓瓧娈电殑缁勯棿鏈澶у肩殑琛屸.
娉ㄦ剰锛屽亣濡備綘姝e湪灏濊瘯閬靛惊鏍囧噯 SQL, 浣犱笉鑳戒娇鐢GROUP BY鎴 ORDER BY瀛愬彞涓殑琛ㄨ揪寮忋備綘鍙互閫氳繃浣跨敤琛ㄨ揪寮忕殑鍒悕缁曡繃杩欎竴闄愬埗锛
mysql> SELECT id,FLOOR(value/100) AS val
-> FROM tbl_name
-> GROUP BY id, val ORDER BY val;
鐒惰, MySQL鍏佽浣犱娇鐢GROUP BY 鍙 ORDER BY 瀛愬彞涓殑琛ㄨ揪寮忋備緥濡傦細
mysql> SELECT id, FLOOR(value/100) FROM tbl_name ORDER BY RAND();杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆 鍘熷鍙傝冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆