站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > MySQL 5.1参考手册中文版

Chapter聽12.聽Functions and Operators - MySQL 5.1参考手册中文版

绗12绔狅細鍑芥暟鍜屾搷浣滅

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

12.1. 鎿嶄綔绗

12.1.1. 鎿嶄綔绗︿紭鍏堢骇

浠ヤ笅鍒楄〃鏄剧ず浜嗘搷浣滅浼樺厛绾х殑鐢变綆鍒伴珮鐨勯『搴忋傛帓鍒楀湪鍚屼竴琛岀殑鎿嶄綔绗﹀叿鏈夌浉鍚岀殑浼樺厛绾с

:=

||, 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鏈嶅姟鍣ㄦā寮忊

12.1.2. 鍦嗘嫭鍙

  • ( ... )

    浣跨敤鎷姬鏉ヨ瀹氳〃杈惧紡鐨勮繍绠楅『搴忥紝渚嬪锛

    mysql> SELECT 1+2*3;
            -> 7
    mysql> SELECT (1+2)*3;
            -> 9
    

12.1.3. 姣旇緝鍑芥暟鍜屾搷浣滅

姣旇緝杩愮畻浜х敓鐨勭粨鏋滀负1(TRUE)0 (FALSE) NULL銆傝繖浜涜繍绠楀彲鐢ㄤ簬鏁板瓧鍜屽瓧绗︿覆銆傛牴鎹渶瑕侊紝瀛楃涓插彲鑷姩杞崲涓烘暟瀛楋紝鑰屾暟瀛椾篃鍙嚜鍔ㄨ浆鎹负瀛楃涓层

鏈珷涓殑涓浜涘嚱鏁 (LEAST()GREATEST()) 鐨勬墍寰楀间笉鍖呮嫭 1 (TRUE) 0 (FALSE) NULL銆傜劧鑰岋紝鍏舵墍寰楀间箖鏄熀浜庢寜鐓т笅杩拌鍒欒繍琛岀殑姣旇緝杩愮畻锛  

MySQL鎸夌収浠ヤ笅瑙勫垯杩涜鏁板兼瘮杈冿細

  • 鑻ユ湁涓涓垨涓や釜鍙傛暟 NULL锛岄櫎闈NULL-safe <=> 绛夌畻绗︼紝鍒欐瘮杈冭繍绠楃殑缁撴灉涓NULL
  • 鑻ュ悓涓涓瘮杈冭繍绠椾腑鐨勪袱涓鍙傛暟閮芥槸瀛楃涓诧紝鍒欐寜鐓у瓧绗︿覆杩涜姣旇緝銆
  • 鑻ヤ袱涓鍙傛暟鍧囦负鏁存暟锛屽垯鎸夌収鏁存暟杩涜姣旇緝銆 
  • 鍗佸叚杩涘埗鍊煎湪涓嶉渶瑕佷綔涓烘暟瀛楄繘琛屾瘮杈冩椂锛屽垯鎸夌収浜岃繘鍒跺瓧绗︿覆杩涜澶勭悊銆
  • 鍋囧鍙傛暟涓殑涓涓负 TIMESTAMP DATETIME 鍒楋紝鑰屽叾瀹鍙傛暟鍧囦负甯告暟锛 鍒欏湪杩涜姣旇緝鍓嶅皢甯告暟杞负 timestamp銆傝繖鏍峰仛鐨勭洰鐨勬槸涓轰簡浣ODBC鐨勮繘琛屾洿鍔犻『鍒┿ 娉ㄦ剰锛岃繖涓嶉傚悎IN()涓殑鍙傛暟!涓轰簡鏇村姞鍙潬锛屽湪杩涜瀵规瘮鏃堕氬父浣跨敤瀹屾暣鐨 datetime/date/time瀛楃涓层
  • 鍦ㄥ叾瀹冩儏鍐典笅锛鍙傛暟浣滀负娴偣鏁拌繘琛屾瘮杈冦  

鍦ㄩ粯璁ょ姸鎬佷笅锛屽瓧绗︿覆姣旇緝涓嶅尯鍒嗗ぇ灏忓啓锛屽苟浣跨敤鐜版湁瀛楃闆(榛樿涓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

  • IS boolean_value IS NOT boolean_value

鏍规嵁涓涓竷灏斿兼潵妫楠屼竴涓硷紝鍦ㄨ繖閲岋紝甯冨皵鍊煎彲浠ユ槸TRUEFALSEUNKNOWN

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

  • IS NULL IS NOT NULL

妫楠屼竴涓兼槸鍚︿负 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鏀寔涓涓嬮澶栫壒鎬э細

    • 鍦ㄤ竴涓间骇鐢熷悗锛岀珛鍗宠繍琛屼竴涓互涓嬫牸寮忕殑璇彞锛屼粠鑰屾壘鍒版渶鏂AUTO_INCREMENT 鍊肩殑鎵鍦ㄨ锛

o                     SELECT * FROM tbl_name WHERE auto_col IS NULL

褰撹缃SQL_AUTO_IS_NULL=0鏃讹紝杩欓」鎿嶄綔鏃犳硶杩愯銆傝鍙傝13.5.3鑺傦紝鈥淪ET璇硶鈥

    • 瀵逛簬鍛藉悕涓NOT NULL DATE DATETIME鍒楋紝鍙氳繃浣跨敤濡備笅鐨 璇彞鎵惧埌鐗瑰畾鏃ユ湡 '0000-00-00'

o                     SELECT * FROM tbl_name WHERE date_column IS NULL

杩愯杩欎竴姝ラ渶瑕佷娇鐢ㄤ竴浜 ODBC 搴旂敤杞欢锛屽洜涓 ODBC鏈韩涓嶆敮鎸 涓涓 '0000-00-00'鐨勬椂闂村笺 

  • expr BETWEEN min AND max

鍋囧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

  • expr NOT BETWEEN min AND max

杩欑浉褰撲簬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,...)

  • expr IN鍒楄〃涓殑浠绘剰涓涓硷紝鍒欏叾杩斿洖鍊间负 1 , 鍚﹀垯杩斿洖鍊间负0銆傚亣濡傛墍鏈夌殑鍊奸兘鏄父鏁帮紝鍒欏叾璁$畻鍜屽垎绫绘牴鎹 expr 鐨勭被鍨嬭繘琛屻傝繖鏃讹紝浣跨敤浜屽垎鎼滅储鏉ユ悳绱俊鎭傚IN鍊煎垪琛ㄥ叏閮ㄧ敱甯告暟缁勬垚锛屽垯鎰忓懗鐫IN 鐨勯熷害闈炲父涔嬪揩銆傚expr 鏄竴涓尯鍒嗗ぇ灏忓啓鐨勫瓧绗︿覆琛ㄨ揪寮忥紝鍒欏瓧绗︿覆姣旇緝涔熸寜鐓у尯鍒嗗ぇ灏忓啓鐨勬柟寮忚繘琛屻 

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,...))鐩稿悓銆

  • ISNULL(expr)

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,...)

鍦ㄦ湁涓や釜鎴栧涓鍙傛暟鐨勬儏鍐典笅锛 杩斿洖鍊间负鏈灏 (鏈灏忓) 鍙傛暟銆傜敤涓涓嬭鍒欏皢鑷彉閲忚繘琛屽姣旓細

    • 鍋囧杩斿洖鍊艰鐢ㄥ湪涓涓 INTEGER 璇涓紝鎴栨槸鎵鏈鍙傛暟鍧囦负鏁存暟鍊硷紝鍒欏皢鍏朵綔涓烘暣鏁板艰繘琛屾瘮杈冦
    • 鍋囧杩斿洖鍊艰鐢ㄥ湪涓涓 REAL璇涓紝鎴栨墍鏈鍙傛暟鍧囦负瀹炲硷紝鍒 灏嗗叾浣滀负瀹炲艰繘琛屾瘮杈冦
    • 鍋囧浠绘剰涓涓鍙傛暟鏄竴涓尯鍒嗗ぇ灏忓啓鐨勫瓧绗︿覆锛屽垯灏鍙傛暟鎸夌収鍖哄垎澶у皬鍐欑殑瀛楃涓茶繘琛屾瘮杈冦
    • 鍦ㄥ叾瀹冩儏鍐典笅锛屽皢鍙傛暟浣滀负鍖哄垎澶у皬鍐欑殑瀛楃涓茶繘琛屾瘮杈冦

鍋囧浠绘剰涓涓嚜鍙橀噺涓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銆傛暣鏁拌〃绀烘硶涓嶅埄浜庝繚瀛樻暟鍊硷紝鍥犳瀹冨寘鎷竴涓甫绗﹀彿鏁存暟銆

12.1.4. 閫昏緫鎿嶄綔绗

SQL涓紝鎵鏈夐昏緫 鎿嶄綔绗︾殑姹傚兼墍寰楃粨鏋滃潎涓 TRUEFALSE NULL (UNKNOWN)銆傚湪 MySQL涓紝瀹冧滑浣撶幇涓  1 (TRUE) 0 (FALSE) NULL銆傚叾澶у鏁伴兘涓庝笉鍚岀殑鏁版嵁搴SQL閫氱敤锛岀劧鑰屼竴浜涙湇鍔″櫒瀵TRUE鐨勮繑鍥炲煎彲鑳芥槸浠绘剰涓涓潪闆跺笺

  • NOT !

閫昏緫 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 &&

閫昏緫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 ||

閫昏緫 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

閫昏緫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鑺傦紝鈥滄搷浣滅浼樺厛绾р

12.2. 鎺у埗娴佺▼鍑芥暟

  • CASE value WHEN [compare-value] THEN result [WHEN [compare-value] THEN result ...] [ELSE result] END CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END

鍦ㄧ涓涓柟妗堢殑杩斿洖缁撴灉涓紝 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琛ㄨ揪寮忕殑榛樿杩斿洖鍊肩被鍨嬫槸浠讳綍杩斿洖鍊肩殑鐩稿闆嗗悎绫诲瀷锛屼絾鍏蜂綋鎯呭喌瑙嗗叾鎵鍦ㄨ澧冭屽畾銆傚鏋滅敤鍦ㄥ瓧绗︿覆璇涓紝鍒欒繑鍥炵粨鏋滃懗瀛楃涓层傚鏋滅敤鍦ㄦ暟瀛楄澧冧腑锛屽垯杩斿洖缁撴灉涓哄崄杩涘埗鍊笺佸疄鍊兼垨鏁存暟鍊笺 

  • IF(expr1,expr2,expr3)

濡傛灉 expr1 TRUE (expr1 <> 0 and expr1 <> NULL)锛屽垯 IF()鐨勮繑鍥炲间负expr2; 鍚﹀垯杩斿洖鍊煎垯涓 expr3IF() 鐨勮繑鍥炲间负鏁板瓧鍊兼垨瀛楃涓插硷紝鍏蜂綋鎯呭喌瑙嗗叾鎵鍦ㄨ澧冭屽畾銆

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 閮芥槸瀛楃涓诧紝涓斿叾涓换浣曚竴涓瓧绗︿覆鍖哄垎澶у皬鍐欙紝鍒欒繑鍥炵粨鏋滄槸鍖哄垎澶у皬鍐欍

  • IFNULL(expr1,expr2)

鍋囧expr1 涓嶄负 NULL锛屽垯 IFNULL() 鐨勮繑鍥炲间负 expr1; 鍚﹀垯鍏惰繑鍥炲间负 expr2IFNULL()鐨勮繑鍥炲兼槸鏁板瓧鎴栨槸瀛楃涓诧紝鍏蜂綋鎯呭喌鍙栧喅浜庡叾鎵浣跨敤鐨勮澧冦

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)

  • NULLIF(expr1,expr2)

濡傛灉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

12.3. 瀛楃涓插嚱鏁

鍋囧缁撴灉鐨勯暱搴﹀ぇ浜 max_allowed_packet 绯荤粺鍙橀噺鐨勬渶澶у兼椂锛屽瓧绗︿覆鍊煎嚱鏁扮殑杩斿洖鍊间负NULL銆傝鍙傝7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥

瀵逛簬鍦ㄥ瓧绗︿覆浣嶇疆鎿嶄綔鐨勫嚱鏁帮紝绗竴涓綅缃殑缂栧彿涓 1

  • ASCII(str)

杩斿洖鍊间负瀛楃涓str 鐨勬渶宸﹀瓧绗︾殑鏁板笺傚亣濡str涓虹┖瀛楃涓诧紝鍒欒繑鍥炲间负 0 銆傚亣濡str NULL锛屽垯杩斿洖鍊间负 NULL ASCII()鐢ㄤ簬甯︽湁浠 0255鐨勬暟鍊肩殑瀛楃銆

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鐞嗚В涓轰竴涓暣鏁帮紝鍏惰繑鍥炲间负涓涓寘鍚繖浜涙暣鏁扮殑浠g爜鍊兼墍缁欏嚭鐨勫瓧绗︾殑瀛楃涓层NULL鍊艰鐪佺暐銆

mysql> SELECT CHAR(77,121,83,81,'76');

        -> 'MySQL'

mysql> SELECT CHAR(77,77.3,'77.3');

        -> 'MMM'

澶т簬 255CHAR()鍙傛暟琚浆鎹负澶氱粨鏋滃瓧绗︺ 渚嬪锛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鐨勫帇缂╁簱鍘嬬缉杩囥   鍚﹀垯锛岃繑鍥炲煎缁堟槸NULLUNCOMPRESS() 鍙皢鍘嬬缉杩囩殑瀛楃涓茶繘琛岃В鍘嬬缉銆

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

鍘嬬缉鍚庣殑瀛楃涓茬殑鍐呭鎸夌収浠ヤ笅鏂瑰紡瀛樺偍锛

    • 绌哄瓧绗︿覆鎸夌収绌哄瓧绗︿覆瀛樺偍銆
    • 闈炵┖瀛楃涓叉湭鍘嬬缉瀛楃涓茬殑鍥涘瓧鑺傞暱搴﹁繘琛屽瓨鍌(棣栧厛涓轰綆瀛楄妭),鍚庨潰鏄帇缂╁瓧绗︿覆銆傚鏋滃瓧绗︿覆浠ョ┖鏍肩粨灏撅紝灏变細鍦ㄥ悗鍔犱竴".",浠ラ槻姝㈠綋缁撴灉鍊兼槸瀛樺偍鍦CHARVARCHAR绫诲瀷鐨勫瓧娈靛垪鏃,鍑虹幇鑷姩鎶婄粨灏剧┖鏍煎幓鎺夌殑鐜拌薄銆(涓嶆帹鑽愪娇鐢 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() 浠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

  • CONV(N,from_base,to_base)

涓嶅悓鏁板熀闂磋浆鎹㈡暟瀛椼傝繑鍥炲间负鏁板瓧鐨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'

  • 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銆 杩欎釜鍑芥暟鍦ㄧ涓涓弬鏁板寘鍚竴涓楀彿(,)鏃跺皢鏃犳硶姝e父杩愯銆 

mysql> SELECT FIND_IN_SET('b','a,b,c,d');

        -> 2

  • FORMAT(X,D)

number X璁剧疆涓烘牸寮 '#,###,###.##', 浠ュ洓鑸嶄簲鍏ョ殑鏂瑰紡淇濈暀鍒板皬鏁扮偣鍚D, 鑰岃繑鍥炵粨鏋滀负涓涓瓧绗︿覆銆傝瑙12.9.4鑺傦紝鈥滃叾浠栧嚱鏁扳.

  • 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 鐨勯暱搴︼紝鍗曚綅涓哄瓧鑺傘備竴涓瀛楄妭瀛楃绠椾綔澶氬瓧鑺傘傝繖鎰忓懗鐫 瀵逛簬涓涓寘鍚52瀛楄妭瀛楃鐨勫瓧绗︿覆锛 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

杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冿紝骞朵笖鍙湁褰撹嚦灏戞湁涓涓弬鏁版槸浜岃繘鍒跺瓧绗︿覆鏃跺尯鍒嗗ぇ灏忓啓銆 

  • LOWER(str)

杩斿洖瀛楃涓 str 浠ュ強鎵鏈夋牴鎹渶鏂扮殑瀛楃闆嗘槧灏勮〃鍙樹负灏忓啓瀛楁瘝鐨勫瓧绗 (榛樿涓  cp1252 Latin1)

mysql> SELECT LOWER('QUADRATICALLY');

        -> 'quadratically'

杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦

  • LPAD(str,len,padstr)

杩斿洖瀛楃涓 str, 鍏跺乏杈圭敱瀛楃涓padstr 濉ˉ鍒len 瀛楃闀垮害銆傚亣濡str 鐨勯暱搴﹀ぇ浜len, 鍒欒繑鍥炲艰缂╃煭鑷 len 瀛楃銆

mysql> SELECT LPAD('hi',4,'??');

        -> '??hi'

mysql> SELECT LPAD('hi',1,'??');

        -> 'h'

  • LTRIM(str)

杩斿洖瀛楃涓 str 锛屽叾寮曞绌烘牸瀛楃琚垹闄ゃ

mysql> SELECT LTRIM('  barbar');

        -> 'barbar'

杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦

  • MAKE_SET(bits,str1,str2,...)

杩斿洖涓涓瀹氬 (涓涓寘鍚鈥,鈥欏彿鍒嗗紑鐨勫瓧瀛楃涓茬殑瀛楃涓) 锛岀敱鍦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)

MID(str,pos,len) SUBSTRING(str,pos,len)鐨勫悓涔夎瘝銆

  • OCT(N)

杩斿洖涓涓 N鐨勫叓杩涘埗鍊肩殑瀛楃涓茶〃绀猴紝鍏朵腑 N 鏄竴涓longlong (BIGINT)鏁般傝繖绛夊悓浜CONV(N,10,8)銆傝嫢N NULL 锛屽垯杩斿洖鍊间负NULL

mysql> SELECT OCT(12);

        -> '14'

  • OCTET_LENGTH(str)

OCTET_LENGTH() LENGTH()鐨勫悓涔夎瘝銆

  • ORD(str)

鑻ュ瓧绗︿覆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)

POSITION(substr IN str) LOCATE(substr,str)鍚屼箟璇嶃

  • QUOTE(str)

寮曡瘉涓涓瓧绗︿覆锛岀敱姝や骇鐢熶竴涓湪SQL璇彞涓彲鐢ㄤ綔瀹屽叏杞箟鏁版嵁鍊肩殑缁撴灉銆  杩斿洖鐨勫瓧绗︿覆鐢卞崟寮曞彿鏍囨敞锛屾瘡渚嬮兘甯︽湁鍗曞紩鍙 (')銆 鍙嶆枩绾跨鍙 (\) ASCII NUL浠ュ強鍓嶉潰鏈夊弽鏂滅嚎绗﹀彿鐨Control-Z 銆傚鏋滆嚜鍙橀噺鐨勫间负NULL, 鍒欒繑鍥炰笉甯﹀崟寮曞彿鐨勫崟璇 鈥NULL鈥濄

mysql> SELECT QUOTE('Don\'t!');

        -> 'Don\'t!'

mysql> SELECT QUOTE(NULL);

        -> NULL

  • REPEAT(str,count)

杩斿洖涓涓敱閲嶅鐨勫瓧绗︿覆str 缁勬垚鐨勫瓧绗︿覆锛屽瓧绗︿覆str鐨勬暟鐩瓑浜count 銆 鑻 count <= 0,鍒欒繑鍥炰竴涓┖瀛楃涓层傝嫢str count NULL锛屽垯杩斿洖 NULL

mysql> SELECT REPEAT('MySQL', 3);

        -> 'MySQLMySQLMySQL'

  • REPLACE(str,from_str,to_str)

杩斿洖瀛楃涓str 浠ュ強鎵鏈夎瀛楃涓to_str鏇夸唬鐨勫瓧绗︿覆from_str

mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');

        -> 'WwWwWw.mysql.com'

杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦

  • REVERSE(str)

杩斿洖瀛楃涓 str 锛岄『搴忓拰瀛楃椤哄簭鐩稿弽銆

mysql> SELECT REVERSE('abc');

        -> 'cba'

杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦

  • RIGHT(str,len)

浠庡瓧绗︿覆str 寮濮嬶紝杩斿洖鏈鍙len 瀛楃銆

mysql> SELECT RIGHT('foobarbar', 4);

        -> 'rbar'

杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦

  • RPAD(str,len,padstr)

杩斿洖瀛楃涓str, 鍏跺彸杈硅瀛楃涓 padstr濉ˉ鑷len 瀛楃闀垮害銆傚亣濡傚瓧绗︿覆str 鐨勯暱搴﹀ぇ浜 len,鍒欒繑鍥炲艰缂╃煭鍒颁笌 len 瀛楃鐩稿悓闀垮害銆

mysql> SELECT RPAD('hi',5,'?');

        -> 'hi???'

mysql> SELECT RPAD('hi',1,'?');

        -> 'h'

杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦

  • RTRIM(str)

杩斿洖瀛楃涓 str 锛岀粨灏剧┖鏍煎瓧绗﹁鍒犲幓銆

mysql> SELECT RTRIM('barbar   ');

        -> 'barbar'

杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦

  • SOUNDEX(str)

str杩斿洖涓涓soundex瀛楃涓层 涓や釜鍏锋湁鍑犱箮鍚屾牱鎺㈡祴鐨勫瓧绗︿覆搴旇鍏锋湁鍚屾牱鐨 soundex 瀛楃涓层備竴涓爣鍑嗙殑soundex 瀛楃涓茬殑闀垮害涓4涓瓧绗︼紝鐒惰SOUNDEX() 鍑芥暟浼氳繑鍥炰竴涓汉浠ラ暱搴︾殑瀛楃涓层 鍙娇鐢ㄧ粨鏋滀腑鐨SUBSTRING() 鏉ュ緱鍒颁竴涓爣鍑 soundex 瀛楃涓层傚湪str浼氬拷鐣ユ墍鏈夋湭鎸夌収瀛楁瘝椤哄簭鎺掑垪鐨勫瓧绗︺ 鎵鏈変笉鍦A-Z鑼冨洿涔嬪唴鐨勫浗闄呭瓧姣嶇鍙疯瑙嗕负鍏冮煶瀛楁瘝銆

mysql> SELECT SOUNDEX('Hello');

        -> 'H400'

mysql> SELECT SOUNDEX('Quadratically');

        -> 'Q36324'

娉ㄦ剰锛杩欎釜鍑芥暟鎵ц鍘熷鐨Soundex绠楁硶锛岃岄潪鏇村姞娴佽鐨勫姞寮虹増鏈(D. Knuth鎵杩)銆傚叾鍖哄埆鍦ㄤ簬鍘熷鐗堟湰棣栧厛浼氬垹鍘诲厓闊筹紝鍏舵鏄噸澶嶏紝鑰屽姞寮虹増鍒欓鍏堝垹鍘婚噸澶嶏紝鑰屽悗鍒犲幓鍏冮煶銆

  • expr1 SOUNDS LIKE expr2

杩欑浉褰撲簬SOUNDEX(expr1) = SOUNDEX(expr2)

  • SPACE(N)

杩斿洖涓涓敱N 闂撮殧绗﹀彿缁勬垚鐨勫瓧绗︿覆銆

mysql> SELECT SPACE(6);

        -> '      '

  • SUBSTRING(str,pos) , SUBSTRING(str FROM pos) SUBSTRING(str,pos,len) , SUBSTRING(str FROM pos FOR len)

涓嶅甫鏈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()鐨勫悓涔夎瘝銆

  • SUBSTRING_INDEX(str,delim,count)

鍦ㄥ畾鐣岀 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'

杩欎釜鍑芥暟鏀寔澶氬瓧鑺傚瓧鍏冦

  • TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str) TRIM(remstr FROM] str)

杩斿洖瀛楃涓 str 锛 鍏朵腑鎵鏈remstr 鍓嶇紑鍜/鎴栧悗缂閮藉凡琚垹闄ゃ傝嫢鍒嗙被绗BOTHLEADINTRAILING涓病鏈変竴涓槸缁欏畾鐨,鍒欏亣璁句负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(str)

UCASE()UPPER()鐨勫悓涔夎瘝銆

  • UNCOMPRESS(string_to_uncompress)

瀵圭粡COMPRESS()鍑芥暟鍘嬬缉鍚庣殑瀛楃涓茶繘琛岃В鍘嬬缉銆傝嫢鍙傛暟涓哄帇缂╁硷紝鍒欑粨鏋滀负 NULL銆傝繖涓嚱鏁拌姹  MySQL 宸茶璇稿zlib 涔嬬被鐨勫帇缂╁簱缂栬瘧杩囥傚惁鍒, 杩斿洖鍊煎皢濮嬬粓鏄 NULL

mysql> SELECT UNCOMPRESS(COMPRESS('any string'));

        -> 'any string'

mysql> SELECT UNCOMPRESS('any string');

        -> NULL

  • UNCOMPRESSED_LENGTH(compressed_string)

杩斿洖鍘嬬缉瀛楃涓插帇缂╁墠鐨勯暱搴︺

mysql> SELECT UNCOMPRESSED_LENGTH(COMPRESS(REPEAT('a',30)));

        -> 30

  • UNHEX(str)

鎵ц浠HEX(str)鐨勫弽鍚戞搷浣溿傚氨鏄锛屽畠灏嗗弬鏁颁腑鐨勬瘡涓瀵瑰崄鍏繘鍒舵暟瀛楃悊瑙d负涓涓暟瀛楋紝骞跺皢鍏惰浆鍖栦负璇ユ暟瀛椾唬琛ㄧ殑瀛楃銆傜粨鏋滃瓧绗︿互浜岃繘鍒跺瓧绗︿覆鐨勫舰寮忚繑鍥炪

mysql> SELECT UNHEX('4D7953514C');

        -> 'MySQL'

mysql> SELECT 0x4D7953514C;

        -> 'MySQL'

mysql> SELECT UNHEX(HEX('string'));

        -> 'string'

mysql> SELECT HEX(UNHEX('1267'));

        -> '1267'

  • UPPER(str)

杩斿洖瀛楃涓str 浠ュ強鏍规嵁鏈鏂板瓧绗﹂泦鏄犲皠杞寲涓哄ぇ鍐欏瓧姣嶇殑瀛楃 (榛樿涓cp1252 Latin1).

mysql> SELECT UPPER('Hej');

        -> 'HEJ'

璇ュ嚱鏁版敮鎸佸瀛楄妭瀛楀厓銆

12.3.1. 瀛楃涓叉瘮杈冨嚱鏁

鏍规嵁锛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() 姣旇緝鍙彇銆.

鑻ュ凡缁忓涓涓瓧绗︿覆鍑芥暟缁欏畾涓涓簩杩涘埗瀛楃涓蹭綔涓哄弬鏁帮紝 鍒欐墍寰楀埌鐨勭粨鏋滃瓧绗︿覆涔熸槸涓涓簩杩涘埗瀛楃涓层備竴涓浆鍖栦负瀛楃涓茬殑鏁板瓧琚綔涓轰簩杩涘埗瀛楃涓插寰呫傝繖浠呬細瀵规瘮杈冪粨鏋滀骇鐢熷奖鍝嶃

涓鑸岃█, 鑻ュ瓧绗︿覆姣旇緝涓换鎰忎竴涓〃杈惧紡鏄尯鍒嗗ぇ灏忓啓鐨勶紝鍒欐墽琛屾瘮杈冩椂涔熷尯鍒嗗ぇ灏忓啓銆 

  • expr LIKE pat [ESCAPE 'escape-char']

妯″紡鍖归厤锛屼娇鐢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 \\\\;鍘熷洜鏄弽鏂滅嚎绗﹀彿浼氳璇硶鍒嗘瀽绋嬪簭鍓ョ涓娆★紝鍦ㄨ繘琛屾ā寮忓尮閰嶆椂锛屽張浼氳鍓ョ涓娆★紝鏈鍚庝細鍓╀笅涓涓弽鏂滅嚎绗﹀彿鎺ュ彈鍖归厤銆

  • expr NOT LIKE pat [ESCAPE 'escape-char']

杩欑浉褰撲簬 NOT (expr LIKE pat [ESCAPE 'escape-char'])

  • expr NOT REGEXP pat expr NOT RLIKE pat

杩欑浉褰撲簬NOT (expr REGEXP pat)

  • expr REGEXP pat expr RLIKE 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(expr1,expr2)

鑻ユ墍鏈夌殑瀛楃涓插潎鐩稿悓锛屽垯杩斿洖STRCMP()锛岃嫢鏍规嵁褰撳墠鍒嗙被娆″簭锛岀涓涓弬鏁板皬浜庣浜屼釜锛屽垯杩斿洖  -1锛屽叾瀹冩儏鍐佃繑鍥 1

mysql> SELECT STRCMP('text', 'text2');

        -> -1

mysql> SELECT STRCMP('text2', 'text');

        -> 1

mysql> SELECT STRCMP('text', 'text');

        -> 0

鍦ㄦ墽琛屾瘮杈冩椂锛STRCMP() 浣跨敤褰撳墠瀛楃闆嗐傝繖浣垮緱榛樿鐨勬瘮杈冨尯鍒嗗ぇ灏忓啓锛屽綋鎿嶄綔鏁颁腑鐨勪竴涓垨涓や釜閮芥槸浜岃繘鍒跺瓧绗︿覆鏃堕櫎澶栥

12.4. 鏁板煎嚱鏁

12.4.1. 绠楁湳鎿嶄綔绗

鍙娇鐢ㄥ父瑙佺殑绠楁湳鎿嶄綔绗︺傛敞鎰忓氨 - + *鑰岃█, 鑻ヤ袱涓弬鏁板潎涓烘鏁帮紝鍒欏叾璁$畻缁撴灉鐨勭簿纭害涓 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 绠楁硶涓璧蜂娇鐢ㄣ

  • DIV

鏁存暟闄ゆ硶銆 绫讳技浜 FLOOR()锛岀劧鑰屼娇鐢BIGINT 绠楁硶涔熸槸鍙潬鐨勩

mysql> SELECT 5 DIV 2;

        -> 2

12.4.2. 鏁板鍑芥暟

鑻ュ彂鐢熼敊璇紝鎵鏈夋暟瀛﹀嚱鏁颁細杩斿洖 NULL

  • ABS(X)

杩斿洖X 鐨勭粷瀵瑰笺

mysql> SELECT ABS(2);

        -> 2

mysql> SELECT ABS(-32);

        -> 32

璇ュ嚱鏁版敮鎸佷娇鐢BIGINT鍊笺

  • ACOS(X)

杩斿洖X 鍙嶄綑寮, , 浣欏鸡鏄X鐨勫笺傝嫢X 涓嶅湪-1 1鐨勮寖鍥翠箣鍐咃紝鍒欒繑鍥 NULL

mysql> SELECT ACOS(1);

        -> 0

mysql> SELECT ACOS(1.0001);

        -> NULL

mysql> SELECT ACOS(0);

        -> 1.5707963267949

  • ASIN(X)

杩斿洖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' |
+---------+------+-----------------------------------------+
  • ATAN(X)

杩斿洖X 鐨勫弽姝e垏锛屽嵆锛屾鍒囦负X 鐨勫笺

mysql> SELECT ATAN(2);

        -> 1.1071487177941

mysql> SELECT ATAN(-2);

        -> -1.1071487177941

  • ATAN(Y,X) , ATAN2(Y,X)

杩斿洖涓や釜鍙橀噺X Y鐨勫弽姝e垏銆 瀹冪被浼间簬 Y X鐨勫弽姝e垏璁$畻 闄ら潪涓や釜鍙傛暟鐨勭鍙峰潎鐢ㄤ簬纭畾缁撴灉鎵鍦ㄨ薄闄愩

mysql> SELECT ATAN(-2,2);

        -> -0.78539816339745

mysql> SELECT ATAN2(PI(),0);

        -> 1.5707963267949

  • CEILING(X) CEIL(X)

杩斿洖涓嶅皬浜X 鐨勬渶灏忔暣鏁板笺

mysql> SELECT CEILING(1.23);

        -> 2

mysql> SELECT CEIL(-1.23);

        -> -1

杩欎袱涓嚱鏁扮殑鎰忎箟鐩稿悓銆傛敞鎰忚繑鍥炲间細琚浆鍖栦负涓涓BIGINT

  • COS(X)

杩斿洖X 鐨勪綑寮︼紝鍏朵腑X鍦ㄥ姬搴︿笂宸茬煡銆 

mysql> SELECT COS(PI());

        -> -1

  • COT(X)

杩斿洖X 鐨勪綑鍒囥

mysql> SELECT COT(12);

        -> -1.5726734063977

mysql> SELECT COT(0);

        -> NULL

  • CRC32(expr)

璁$畻寰幆鍐椾綑鐮佹牎楠鍊煎苟杩斿洖涓涓 32姣旂壒鏃犵鍙峰笺傝嫢鍙傛暟涓NULL 锛屽垯缁撴灉涓 NULL銆傝鍙傛暟搴斾负涓涓瓧绗︿覆锛岃屼笖鍦ㄤ笉鏄瓧绗︿覆鐨勬儏鍐典笅浼氳浣滀负瀛楃涓插鐞嗭紙鑻ユ湁鍙兘锛夈

mysql> SELECT CRC32('MySQL');

        -> 3259397556

mysql> SELECT CRC32('mysql');

        -> 2501908538

  • DEGREES(X)

杩斿洖鍙傛暟 X, 璇ュ弬鏁扮敱寮у害琚浆鍖栦负搴︺

mysql> SELECT DEGREES(PI());

        -> 180

mysql> SELECT DEGREES(PI() / 2);

        -> 90

  • EXP(X)

杩斿洖eX涔樻柟鍚庣殑鍊(鑷劧瀵规暟鐨勫簳)

mysql> SELECT EXP(2);

        -> 7.3890560989307

mysql> SELECT EXP(-2);

        -> 0.13533528323661

mysql> SELECT EXP(0);

        -> 1

  • FLOOR(X)

杩斿洖涓嶅ぇ浜X鐨勬渶澶ф暣鏁板 銆

mysql> SELECT FLOOR(1.23);

        -> 1

mysql> SELECT FLOOR(-1.23);

        -> -2

娉ㄦ剰锛岃繑鍥炲间細琚浆鍖栦负涓涓 BIGINT

  • FORMAT(X,D)

灏嗘暟瀛X 鐨勬牸寮忓啓鎴'#,###,###.##'鏍煎紡, 鍗充繚鐣欏皬鏁扮偣鍚 D浣嶏紝鑰岀D浣嶇殑淇濈暀鏂瑰紡涓哄洓鑸嶄簲鍏ワ紝鐒跺悗灏嗙粨鏋滀互瀛楃涓茬殑褰㈠紡杩斿洖銆璇﹁12.9.4鑺傦紝鈥滃叾浠栧嚱鏁扳

  • LN(X)

杩斿洖X 鐨勮嚜鐒跺鏁,, X 鐩稿浜庡熀鏁e 鐨勫鏁般

mysql> SELECT LN(2);

        -> 0.69314718055995

mysql> SELECT LN(-2);

        -> NULL

杩欎釜鍑芥暟鍚LOG(X)鍏锋湁鐩稿悓鎰忎箟銆

  • LOG(X) LOG(B,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)

  • LOG2(X)

杩斿洖X 鐨勫熀鏁颁负2鐨勫鏁般

mysql> SELECT LOG2(65536);

        -> 16

mysql> SELECT LOG2(-100);

        -> NULL

瀵逛簬鏌ュ嚭瀛樺偍涓涓暟瀛楅渶瑕佸灏戜釜姣旂壒锛LOG2()闈炲父鏈夋晥銆傝繖涓嚱鏁扮浉褰撲簬琛ㄨ揪寮 LOG(X) / LOG(2)

  • LOG10(X)

杩斿洖X鐨勫熀鏁颁负10鐨勫鏁般

mysql> SELECT LOG10(2);

        -> 0.30102999566398

mysql> SELECT LOG10(100);

        -> 2

mysql> SELECT LOG10(-100);

        -> NULL

LOG10(X)鐩稿綋浜LOG(10,X)

  • MOD(N,M) , N % M N MOD M

妯℃搷浣溿傝繑鍥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()

杩斿洖 (pi)鐨勫笺傞粯璁ょ殑鏄剧ず灏忔暟浣嶆暟鏄7,鐒惰 MySQL鍐呴儴浼氫娇鐢ㄥ畬鍏ㄥ弻绮惧害鍊笺

mysql> SELECT PI();

        -> 3.141593

mysql> SELECT PI()+0.000000000000000000;

        -> 3.141592653589793116

  • POW(X,Y) , POWER(X,Y)

杩斿洖X Y涔樻柟鐨勭粨鏋滃笺

mysql> SELECT POW(2,2);

        -> 4

mysql> SELECT POW(2,-2);

        -> 0.25

  • RADIANS(X)

杩斿洖鐢卞害杞寲涓哄姬搴︾殑鍙傛暟 X,  (娉ㄦ剰 寮у害绛変簬180搴︼級

mysql> SELECT RADIANS(90);

        -> 1.5707963267949

  • RAND() RAND(N)

杩斿洖涓涓殢鏈烘诞鐐瑰 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 鐨勮寖鍥达紙鍖呮嫭712锛夊唴寰楀埌涓涓殢鏈烘暣鏁, 鍙娇鐢ㄤ互涓嬭鍙ワ細

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闅忔満鏁扮殑蹇熸柟寮忋

  • ROUND(X) ROUND(X,D)

杩斿洖鍙傛暟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() 浣跨敤绮惧瘑鏁板棰樺簱锛

    • 瀵逛簬鍑嗙‘鍊兼暟瀛, ROUND() 浣跨敤鈥滃洓鑸嶄簲鍏モ 鎴栤滆垗鍏ユ垚鏈鎺ヨ繎鐨勬暟鈥 鐨勮鍒:瀵逛簬涓涓垎鏁伴儴鍒嗕负 .5鎴栧ぇ浜 .5鐨勫硷紝姝f暟鍒欎笂鑸嶅叆鍒伴偦杩戠殑鏁存暟鍊硷紝 璐熸暟鍒欎笅鑸嶅叆涓磋繎鐨勬暣鏁板笺(鎹㈣█涔, 鍏惰垗鍏ョ殑鏂瑰悜鏄暟杞翠笂杩滅闆剁殑鏂瑰悜锛瀵逛簬涓涓垎鏁伴儴鍒嗗皬浜.5 鐨勫硷紝姝f暟鍒欎笅鑸嶅叆涓嬩竴涓暣鏁板硷紝璐熸暟鍒欎笅鑸嶅叆閭昏繎鐨勬暣鏁板硷紝鑰屾鏁板垯涓婅垗鍏ラ偦杩戠殑鏁存暟鍊笺
    • 瀵逛簬杩戜技鍊兼暟瀛楋紝鍏剁粨鏋滄牴鎹C 搴撹屽畾銆傚湪寰堝绯荤粺涓紝杩欐剰鍛崇潃 ROUND()鐨勪娇鐢ㄩ伒寰滆垗鍏ユ垚鏈鎺ヨ繎鐨勫伓鏁扳濈殑瑙勫垯锛 涓涓甫鏈変换浣曞皬鏁伴儴鍒嗙殑鍊间細琚垗鍏ユ垚鏈鎺ヨ繎鐨勫伓鏁版暣鏁般

浠ヤ笅涓句緥璇存槑鑸嶅叆娉曞浜庣簿纭煎拰杩戜技鍊肩殑涓嶅悓涔嬪锛

mysql> SELECT ROUND(2.5), ROUND(25E-1);

+------------+--------------+

| ROUND(2.5) | ROUND(25E-1) |

+------------+--------------+

| 3          |            2 |

+------------+--------------+

璇﹁绗24绔狅細绮惧害鏁板

  • SIGN(X)

杩斿洖鍙傛暟浣滀负-1 01鐨勭鍙凤紝璇ョ鍙峰彇鍐充簬X 鐨勫间负璐熴侀浂鎴栨銆

mysql> SELECT SIGN(-32);

        -> -1

mysql> SELECT SIGN(0);

        -> 0

mysql> SELECT SIGN(234);

        -> 1

  • SIN(X)

杩斿洖X 姝e鸡锛屽叾涓 X 鍦ㄥ姬搴︿腑琚粰瀹氥

mysql> SELECT SIN(PI());

        -> 1.2246063538224e-16

mysql> SELECT ROUND(SIN(PI()));

        -> 0

  • SQRT(X)

杩斿洖闈炶礋鏁X 鐨勪簩娆℃柟鏍广

mysql> SELECT SQRT(4);

        -> 2

mysql> SELECT SQRT(20);

        -> 4.4721359549996

mysql> SELECT SQRT(-16);

        -> NULL       

  • TAN(X)

杩斿洖X 鐨勬鍒囷紝鍏朵腑X 鍦ㄥ姬搴︿腑琚粰瀹氥

mysql> SELECT TAN(PI());

        -> -1.2246063538224e-16

mysql> SELECT TAN(PI()+1);

        -> 1.5574077246549

  • TRUNCATE(X,D)

杩斿洖琚垗鍘昏嚦灏忔暟鐐瑰悗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

鎵鏈夋暟瀛楃殑鑸嶅叆鏂瑰悜閮芥帴杩戜簬闆躲

12.5. 鏃ユ湡鍜屾椂闂村嚱鏁

鏈珷璁鸿堪浜嗕竴浜涘彲鐢ㄤ簬鎿嶄綔鏃堕棿鍊肩殑鍑芥暟銆傚叧浜庢瘡涓椂闂村拰鏃ユ湡绫诲瀷鍏锋湁鐨勫煎煙鍙婃寚瀹氬肩殑鏈夋晥鏍煎紡锛岃鍙傝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

  • ADDDATE(date,INTERVAL expr type) ADDDATE(expr,days)

褰撹绗簩涓弬鏁扮殑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(expr,expr2)

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)

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鏈嶅姟鍣ㄦ椂鍖烘敮鎸佲銆 

  • CURDATE()

灏嗗綋鍓嶆棩鏈熸寜鐓'YYYY-MM-DD' YYYYMMDD 鏍煎紡鐨勫艰繑鍥烇紝鍏蜂綋鏍煎紡鏍规嵁鍑芥暟鐢ㄥ湪瀛楃涓叉垨鏄暟瀛楄澧冧腑鑰屽畾銆

mysql> SELECT CURDATE();

        -> '1997-12-15'

mysql> SELECT CURDATE() + 0;

        -> 19971215

  • CURRENT_DATE CURRENT_DATE()

CURRENT_DATECURRENT_DATE()鏄殑鍚屼箟璇.

  • CURTIME()

灏嗗綋鍓嶆椂闂翠互'HH:MM:SS' HHMMSS 鐨勬牸寮忚繑鍥烇紝 鍏蜂綋鏍煎紡鏍规嵁鍑芥暟鐢ㄥ湪瀛楃涓叉垨鏄暟瀛楄澧冧腑鑰屽畾銆 

mysql> SELECT CURTIME();

        -> '23:50:26'

mysql> SELECT CURTIME() + 0;

        -> 235026

  • CURRENT_TIME, CURRENT_TIME()

CURRENT_TIME CURRENT_TIME() CURTIME()鐨勫悓涔夎瘝銆

  • CURRENT_TIMESTAMP, CURRENT_TIMESTAMP()

CURRENT_TIMESTAMP CURRENT_TIMESTAMP()NOW()鐨勫悓涔夎瘝銆

  • DATE(expr)

鎻愬彇鏃ユ湡鎴栨椂闂存棩鏈熻〃杈惧紡expr涓殑鏃ユ湡閮ㄥ垎銆

mysql> SELECT DATE('2003-12-31 01:02:03');

        -> '2003-12-31'

  • DATEDIFF(expr,expr2)

DATEDIFF() 杩斿洖璧峰鏃堕棿 expr鍜岀粨鏉熸椂闂expr2涔嬮棿鐨勫ぉ鏁般Exprexpr2 涓烘棩鏈熸垨 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_ADD(date,INTERVAL expr type) DATE_SUB(date,INTERVAL expr type)

杩欎簺鍑芥暟鎵ц鏃ユ湡杩愮畻銆 date 鏄竴涓 DATETIME DATE鍊硷紝鐢ㄦ潵鎸囧畾璧峰鏃堕棿銆 expr 鏄竴涓〃杈惧紡锛岀敤鏉ユ寚瀹氫粠璧峰鏃ユ湡娣诲姞鎴栧噺鍘荤殑鏃堕棿闂撮殧鍊笺  Expr鏄竴涓瓧绗︿覆;瀵逛簬璐熷肩殑鏃堕棿闂撮殧锛屽畠鍙互浠ヤ竴涓 鈥-鈥欏紑澶淬 type 涓哄叧閿瘝锛屽畠鎸囩ず浜嗚〃杈惧紡琚В閲婄殑鏂瑰紡銆 

鍏抽敭璇INTERVAtype 鍒嗙被绗﹀潎涓嶅尯鍒嗗ぇ灏忓啓銆

浠ヤ笅琛ㄦ樉绀轰簡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 鍊硷紝鑰屼綘鐨勮绠楀彧浼氬寘鎷 YEARMONTHDAY閮ㄥ垎(, 娌℃湁鏃堕棿閮ㄥ垎), 鍏剁粨鏋滄槸涓涓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銆傝繖鐩稿綋浜 MySQLTIME 鍊艰В閲婁负鎵鑰楄垂鐨勬椂闂磋屼笉鏄棩鏃剁殑瑙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銆傚亣濡備綘娣诲姞浜  MONTHYEAR_MONTHYEAR 锛岃岀粨鏋滄棩鏈熶腑鏈変竴澶╃殑鏃ユ湡澶т簬娣诲姞鐨勬湀浠界殑鏃ユ湡鏈澶ч檺搴︼紝鍒欒繖涓棩鏈熻嚜鍔ㄨ璋冩暣涓烘坊鍔犳湀浠界殑鏈澶ф棩鏈燂細

mysql> SELECT DATE_ADD('1998-01-30', INTERVAL 1 MONTH);

        -> '1998-02-28'

  • DATE_FORMAT(date,format)

鏍规嵁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 鍚庡姞 AMPM)

%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(date)

DAY() DAYOFMONTH()鐨勬剰涔夌浉鍚屻

  • DAYNAME(date)

杩斿洖date 瀵瑰簲鐨勫伐浣滄棩鍚嶇О銆

mysql> SELECT DAYNAME('1998-02-05');

        -> '鍛ㄥ洓'

  • DAYOFMONTH(date)

杩斿洖date 瀵瑰簲鐨勮鏈堟棩鏈燂紝鑼冨洿鏄粠 131

mysql> SELECT DAYOFMONTH('1998-02-03');

        -> 3

  • DAYOFWEEK(date)

杩斿洖date (1 = 鍛ㄦ棩, 2 = 鍛ㄤ竴, ..., 7 = 鍛ㄥ叚)瀵瑰簲鐨勫伐浣滄棩绱㈠紩銆傝繖浜涚储寮曞肩鍚 ODBC鏍囧噯銆

mysql> SELECT DAYOFWEEK('1998-02-03');

        -> 3

  • DAYOFYEAR(date)

杩斿洖date 瀵瑰簲鐨勪竴骞翠腑鐨勫ぉ鏁帮紝鑼冨洿鏄粠 1366

mysql> SELECT DAYOFYEAR('1998-02-03');

        -> 34

  • EXTRACT(type FROM date)

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

  • FROM_DAYS(N)

缁欏畾涓涓ぉ鏁  N, 杩斿洖涓涓DATE鍊笺

mysql> SELECT FROM_DAYS(729669);

        -> '1997-10-07'

浣跨敤 FROM_DAYS()澶勭悊鍙よ佹棩鏈熸椂锛屽姟蹇呰皑鎱庛備粬涓嶇敤浜庡鐞嗛槼鍘嗗嚭鐜板墠鐨勬棩鏈(1582)銆傝鍙傝12.6鑺傦紝鈥淢ySQL浣跨敤浠涔堟棩鍘嗭紵鈥

  • FROM_UNIXTIME(unix_timestamp) , FROM_UNIXTIME(unix_timestamp,format)

杩斿洖'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'

  • GET_FORMAT(DATE|TIME|DATETIME, 'EUR'|'USA'|'JIS'|'ISO'|'INTERNAL')

杩斿洖涓涓牸寮忓瓧绗︿覆銆傝繖涓嚱鏁板湪鍚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'

鎴栬13.5.3鑺傦紝鈥淪ET璇硶鈥

  • HOUR(time)

杩斿洖time 瀵瑰簲鐨勫皬鏃舵暟銆傚浜庢棩鏃跺肩殑杩斿洖鍊艰寖鍥存槸浠 0 23

mysql> SELECT HOUR('10:05:03');

        -> 10

鐒惰,  TIME 鍊肩殑鑼冨洿瀹為檯涓婇潪甯稿ぇ, 鎵浠HOUR鍙互杩斿洖澶т簬23鐨勫笺

mysql> SELECT HOUR('272:59:59');

        -> 272

  • LAST_DAY(date)

鑾峰彇涓涓棩鏈熸垨鏃ユ湡鏃堕棿鍊硷紝杩斿洖璇ユ湀鏈鍚庝竴澶╁搴旂殑鍊笺傝嫢鍙傛暟鏃犳晥锛屽垯杩斿洖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()

LOCALTIME LOCALTIME()NOW()鍏锋湁鐩稿悓鎰忎箟銆

  • LOCALTIMESTAMP, LOCALTIMESTAMP()

LOCALTIMESTAMPLOCALTIMESTAMP()NOW()鍏锋湁鐩稿悓鎰忎箟銆

  • MAKEDATE(year,dayofyear)

缁欏嚭骞翠唤鍊煎拰涓骞翠腑鐨勫ぉ鏁鍊硷紝杩斿洖涓涓棩鏈熴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

  • MAKETIME(hour,minute,second)

杩斿洖鐢hourminutesecond 鍙傛暟璁$畻寰楀嚭鐨勬椂闂村笺

mysql> SELECT MAKETIME(12,15,30);

        -> '12:15:30'

  • MICROSECOND(expr)

浠庢椂闂存垨鏃ユ湡鏃堕棿琛ㄨ揪寮expr杩斿洖寰鍊硷紝鍏舵暟瀛楄寖鍥翠粠 0 999999

mysql> SELECT MICROSECOND('12:00:00.123456');

        -> 123456

mysql> SELECT MICROSECOND('1997-12-31 23:59:59.000010');

        -> 10

  • MINUTE(time)

杩斿洖 time 瀵瑰簲鐨勫垎閽熸暟,鑼冨洿鏄粠 0 59

mysql> SELECT MINUTE('98-02-03 10:05:03');

        -> 5

  • MONTH(date)

杩斿洖date 瀵瑰簲鐨勬湀浠斤紝鑼冨洿鏃朵粠 1 12

mysql> SELECT MONTH('1998-02-03');

        -> 2

  • MONTHNAME(date)

杩斿洖date 瀵瑰簲鏈堜唤鐨勫叏鍚嶃

mysql> SELECT MONTHNAME('1998-02-05');

        -> 'February '

  • NOW()

杩斿洖褰撳墠鏃ユ湡鍜屾椂闂村硷紝鍏舵牸寮忎负 'YYYY-MM-DD HH:MM:SS' YYYYMMDDHHMMSS 锛 鍏蜂綋鏍煎紡鍙栧喅浜庤鍑芥暟鏄惁鐢ㄥ湪瀛楃涓蹭腑鎴栨暟瀛楄澧冧腑銆

mysql> SELECT NOW();

        -> '1997-12-15 23:50:26'

mysql> SELECT NOW() + 0;

        -> 19971215235026

鍦ㄤ竴涓瓨鍌ㄧ▼搴忔垨瑙﹀彂鍣ㄥ唴, NOW() 杩斿洖涓涓父鏁版椂闂达紝璇ュ父鏁版寚绀轰簡璇ョ▼搴忔垨瑙﹀彂璇彞寮濮嬫墽琛岀殑鏃堕棿銆傝繖鍚SYSDATE()鐨勮繍琛屾湁鎵涓嶅悓銆

  • PERIOD_ADD(P,N)

娣诲姞 N 涓湀鑷冲懆鏈P (鏍煎紡涓YYMM YYYYMM)锛岃繑鍥炲肩殑鏍煎紡涓 YYYYMM銆傛敞鎰忓懆鏈熷弬鏁 P 涓嶆槸鏃ユ湡鍊笺 

mysql> SELECT PERIOD_ADD(9801,2);

        -> 199803

  • PERIOD_DIFF(P1,P2

杩斿洖鍛ㄦ湡P1P2 涔嬮棿鐨勬湀浠芥暟銆P1 P2 鐨勬牸寮忓簲璇ヤ负YYMMYYYYMM銆傛敞鎰忓懆鏈熷弬鏁 P1P2 涓嶆槸鏃ユ湡鍊笺

mysql> SELECT PERIOD_DIFF(9802,199703);

        -> 11

  • QUARTER(date)

杩斿洖date 瀵瑰簲鐨勪竴骞翠腑鐨勫搴﹀硷紝鑼冨洿鏄粠 1 4

mysql> SELECT QUARTER('98-04-01');

        -> 2

  • SECOND(time)

杩斿洖time 瀵瑰簲鐨勭鏁, 鑼冨洿鏄粠 059

mysql> SELECT SECOND('10:05:03');

        -> 3

  • SEC_TO_TIME(seconds)

杩斿洖琚浆鍖栦负灏忔椂銆 鍒嗛挓鍜岀鏁扮殑seconds鍙傛暟鍊, 鍏舵牸寮忎负 'HH:MM:SS' HHMMSS锛屽叿浣撴牸寮忔牴鎹鍑芥暟鏄惁鐢ㄥ湪瀛楃涓叉垨鏁板瓧璇涓屽畾銆

mysql> SELECT SEC_TO_TIME(2378);

        -> '00:39:38'

mysql> SELECT SEC_TO_TIME(2378) + 0;

        -> 3938

  • STR_TO_DATE(str,format)

杩欐槸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'

  • SUBDATE(date,INTERVAL expr type) SUBDATE(expr,days)

褰撹绗簩涓弬鏁扮殑 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)

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'

  • SYSDATE()

杩斿洖褰撳墠鏃ユ湡鍜屾椂闂村硷紝鏍煎紡涓'YYYY-MM-DD HH:MM:SS' YYYYMMDDHHMMSS锛 鍏蜂綋鏍煎紡鏍规嵁鍑芥暟鏄惁鐢ㄥ湪瀛楃涓叉垨鏁板瓧璇鑰屽畾銆

鍦ㄤ竴涓瓨鍌ㄧ▼搴忔垨瑙﹀彂鍣ㄤ腑, SYSDATE()杩斿洖鍏舵墽琛岀殑鏃堕棿, 鑰岄潪瀛樺偍鎴愰兘鎴栬Е鍙戣鍙ュ紑濮嬫墽琛岀殑鏃堕棿銆傝繖涓NOW()鐨勮繍浣滄湁鎵涓嶅悓銆

  • TIME(expr)

鎻愬彇涓涓椂闂存垨鏃ユ湡鏃堕棿琛ㄨ揪寮忕殑鏃堕棿閮ㄥ垎锛屽苟灏嗗叾浠ュ瓧绗︿覆褰㈠紡杩斿洖銆

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)

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'

  • TIMESTAMP(expr) , TIMESTAMP(expr,expr2)

瀵逛簬涓涓崟鍙傛暟,璇ュ嚱鏁板皢鏃ユ湡鎴栨棩鏈熸椂闂磋〃杈惧紡 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'

  • TIMESTAMPADD(interval,int_expr,datetime_expr)

灏嗘暣鍨嬭〃杈惧紡int_expr 娣诲姞鍒版棩鏈熸垨鏃ユ湡鏃堕棿琛ㄨ揪寮 datetime_expr涓 int_expr 鐨勫崟浣嶈鏃堕棿闂撮殧鍙傛暟缁欏畾锛岃鍙傛暟蹇呴』鏄互涓嬪肩殑鍏朵腑涓涓細 FRAC_SECONDSECOND MINUTE HOUR DAY WEEK MONTH QUARTER YEAR

鍙娇鐢ㄦ墍鏄剧ず鐨勫叧閿瘝鎸囧畾Interval鍊硷紝鎴栦娇鐢SQL_TSI_鍓嶇紑銆備緥濡, DAYSQL_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'

  • TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2)

杩斿洖鏃ユ湡鎴栨棩鏈熸椂闂磋〃杈惧紡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

  • TIME_FORMAT(time,format)

鍏朵娇鐢ㄥ拰 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_TO_SEC(time)

杩斿洖宸茶浆鍖栦负绉掔殑time鍙傛暟銆

mysql> SELECT TIME_TO_SEC('22:23:00');

        -> 80580

mysql> SELECT TIME_TO_SEC('00:39:38');

        -> 2378

  • TO_DAYS(date)

缁欏畾涓涓棩鏈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(), UNIX_TIMESTAMP(date)

鑻ユ棤鍙傛暟璋冪敤锛屽垯杩斿洖涓涓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锛 鏈堜唤浠0112,鏃ユ湡浠  01 31)

鍋囧浣犳兂瑕佸噺鍘 UNIX_TIMESTAMP() , 浣犳垨璁稿笇鏈涘垹鍘诲甫绗﹀彿鏁存暟鐨勭粨鏋溿傝鍙傝12.8鑺傦紝鈥淐ast鍑芥暟鍜屾搷浣滅鈥

  • UTC_DATE, UTC_DATE()

杩斿洖褰撳墠 UTC鏃ユ湡鍊硷紝鍏舵牸寮忎负 'YYYY-MM-DD' YYYYMMDD锛屽叿浣撴牸寮忓彇鍐充簬鍑芥暟鏄惁鐢ㄥ湪瀛楃涓叉垨鏁板瓧璇涓 

mysql> SELECT UTC_DATE(), UTC_DATE() + 0;

        -> '2003-08-14', 20030814

  • UTC_TIME, UTC_TIME()

杩斿洖褰撳墠 UTC 鍊硷紝鍏舵牸寮忎负  'HH:MM:SS' HHMMSS锛屽叿浣撴牸寮忔牴鎹鍑芥暟鏄惁鐢ㄥ湪瀛楃涓叉垨鏁板瓧璇鑰屽畾銆

mysql> SELECT UTC_TIME(), UTC_TIME() + 0;

        -> '18:07:53', 180753

  • UTC_TIMESTAMP, UTC_TIMESTAMP()

杩斿洖褰撳墠UTC鏃ユ湡鍙婃椂闂村硷紝鏍煎紡涓 'YYYY-MM-DD HH:MM:SS' YYYYMMDDHHMMSS锛屽叿浣撴牸寮忔牴鎹鍑芥暟鏄惁鐢ㄥ湪瀛楃涓叉垨鏁板瓧璇鑰屽畾銆

mysql> SELECT UTC_TIMESTAMP(), UTC_TIMESTAMP() + 0;

        -> '2003-08-14 18:08:04', 20030814180804

  • WEEK(date[,mode])

璇ュ嚱鏁拌繑鍥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

娉ㄦ剰锛屽亣濡傛湁涓涓棩鏈熶綅浜庡墠涓骞寸殑鏈鍚庝竴鍛紝 鑻ヤ綘涓嶄娇鐢2367浣滀负mode 鍙傛暟閫夋嫨锛屽垯MySQL杩斿洖 0

mysql> SELECT YEAR('2000-01-01'), WEEK('2000-01-01',0);

        -> 2000, 0

鏈変汉鎴栬浼氭彁鍑烘剰瑙侊紝璁や负 MySQL 瀵逛簬WEEK() 鍑芥暟搴旇杩斿洖 52 锛屽師鍥犳槸缁欏畾鐨勬棩鏈熷疄闄呬笂鍙戠敓鍦1999骞寸殑绗52鍛ㄣ傛垜浠喅瀹氳繑鍥0浣滀负浠f浛鐨勫師鍥犳槸鎴戜滑甯屾湜璇ュ嚱鏁拌兘杩斿洖鈥滅粰瀹氬勾浠界殑鏄熸湡鏁扳濄傝繖浣垮緱WEEK() 鍑芥暟鍦ㄥ悓鍏跺畠浠庢棩鏈熶腑鎶藉彇鏃ユ湡閮ㄥ垎鐨勫嚱鏁扮粨鍚堟椂鐨勪娇鐢ㄦ洿鍔犲彲闈犮

鍋囧浣犳洿甯屾湜鎵璁$畻鐨勫叧浜庡勾浠界殑缁撴灉鍖呮嫭缁欏畾鏃ユ湡鎵鍦ㄥ懆鐨勭涓澶╋紝鍒欏簲浣跨敤 025 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'

  • WEEKDAY(date)

杩斿洖date (0 = 鍛ㄤ竴, 1 = 鍛ㄤ簩, ... 6 = 鍛ㄦ棩)瀵瑰簲鐨勫伐浣滄棩绱㈠紩  weekday index for

mysql> SELECT WEEKDAY('1998-02-03 22:23:00');

        -> 1

mysql> SELECT WEEKDAY('1997-11-05');

        -> 2

  • WEEKOFYEAR(date)

灏嗚鏃ユ湡鐨勯槼鍘嗗懆浠ユ暟瀛楀舰寮忚繑鍥烇紝鑼冨洿鏄粠153銆傚畠鏄竴涓吋瀹瑰害鍑芥暟锛岀浉褰撲簬WEEK(date,3)

mysql> SELECT WEEKOFYEAR('1998-02-20');

        -> 8

  • YEAR(date)

杩斿洖date 瀵瑰簲鐨勫勾浠,鑼冨洿鏄粠10009999

mysql> SELECT YEAR('98-02-03');

        -> 1998

  • YEARWEEK(date), YEARWEEK(date,start)

杩斿洖涓涓棩鏈熷搴旂殑骞存垨鍛ㄣstart鍙傛暟鐨勫伐浣滃悓 start鍙傛暟瀵 WEEK()鐨勫伐浣滅浉鍚屻傜粨鏋滀腑鐨勫勾浠藉彲浠ュ拰璇ュ勾鐨勭涓鍛ㄥ拰鏈鍚庝竴鍛ㄥ搴旂殑鏃ユ湡鍙傛暟鏈夋墍涓嶅悓銆

mysql> SELECT YEARWEEK('1987-01-01');

        -> 198653

娉ㄦ剰锛屽懆鏁板拰WEEK()鍑芥暟闃熷彲閫夊弬鏁0 1鍙兘浼氳繑鍥炵殑(0) w鏈夋墍涓嶅悓锛屽師鍥犳槸姝ゆ椂 WEEK() 杩斿洖缁欏畾骞翠唤鐨勮澧冧腑鐨勫懆銆

  •         -> '1997-10-07'
    

12.6. MySQL浣跨敤浠涔堟棩鍘嗭紵

MySQL 浣跨敤閫氬父鎵璇寸殑 proleptic 闃冲巻

姣忎釜灏嗘棩鍘嗙敱鏈卞埄瀹鏀逛负闃冲巻鐨勫浗瀹跺湪鏀瑰彉鏃ュ巻鏈熼棿閮戒笉寰椾笉鍒犻櫎鑷冲皯10澶┿ 涓轰簡浜嗚В鍏惰繍浣滐紝璁╂垜浠湅鐪158210鏈堬紝杩欐槸鐢鏈卞埄瀹鏃ュ巻杞崲涓洪槼鍘嗙殑绗竴娆:

鍛ㄤ竴

鍛ㄤ簩

鍛ㄤ笁

鍛ㄥ洓

鍛ㄤ簲

鍛ㄥ叚

鍛ㄦ棩

1

2

3

4

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

104 鏃ュ埌1015鏃ヤ箣闂寸殑鏃ユ湡涓虹┖鐧姐傝繖涓腑鏂绉颁负鎺ュ叆銆傛帴鍏ュ墠鐨勬棩鏈熷潎浣跨敤鏈卞埄瀹鏃ュ巻, 鑰屾帴鍏ュ悗鐨勬棩鏈熷潎浣跨敤闃冲巻銆傛帴鍏ユ湡闂寸殑鏃ユ湡鏄笉瀛樺湪鐨勩

褰撲竴涓敤浜庢棩鏈熺殑鏃ュ巻骞朵负寰楀埌瀹為檯浣跨敤鏃惰绉颁负 proleptic銆傚洜姝, 鑻ユ垜浠亣璁句粠鏉ユ病鏈夋帴鍏ユ湡鐨勫瓨鍦紝鑰岄槼鍘嗗巻娉曞垯濮嬬粓琚娇鐢紝鎴戜滑浼氭湁涓涓鏈熺殑闃冲巻 銆傝繖灏辨槸MySQL 鎵浣跨敤鐨勶紝姝e鏍囧噯SQL鎵瑕佹眰鐨勩 閴翠簬杩欎釜鍘熷洜锛屼綔涓MySQL DATE DATETIME鍊艰岃鍌ㄥ瓨鐨勬帴鍏ュ墠鐨勬棩鏈熷繀椤昏皟鏁磋繖涓樊寮傘傛垜浠繀椤绘槑鐧斤紝鎺ュ叆鐨勫彂鐢熸椂闂村湪涓嶅悓鐨勫浗瀹舵湁鎵涓嶅悓锛岃屾帴鍏ョ殑鏃堕棿瓒婃櫄锛岄仐澶辩殑鏃ユ湡瓒婂銆備緥濡傦紝鍦ㄥぇ涓嶅垪棰, 鎺ュ叆鍙戠敓鍦 1752,杩欐椂92鏃ワ紝鍛ㄤ笁鍚庣殑绗簩澶╀负914鏃ワ紝鍛ㄤ簩锛 淇勭綏鏂粨鏉熶娇鐢鏈卞埄瀹鏃ュ巻鐨勬椂闂翠负1918,鎺ュ叆杩囩▼涓仐澶卞ぉ鏁颁负 13, 鏍规嵁闃冲巻锛屽叾鏅亶琚О涓衡10鏈堥潻鍛解濈殑鍙戠敓鏃堕棿瀹為檯涓婃槸11鏈堛

12.7. 鍏ㄦ枃鎼滅储鍔熻兘

12.7.1. 甯冨皵鍏ㄦ枃鎼滅储
12.7.2. 鍏ㄦ枃鎼滅储甯︽煡璇㈡墿灞
12.7.3. 鍏ㄦ枃鍋滄瀛
12.7.4. 鍏ㄦ枃闄愬畾鏉′欢
12.7.5. 寰皟MySQL鍏ㄦ枃鎼滅储

MySQL鏀寔鍏ㄦ枃绱㈠紩鍜屾悳绱㈠姛鑳姐MySQL涓殑鍏ㄦ枃绱㈠紩绫诲瀷FULLTEXT鐨勭储寮曘  FULLTEXT 绱㈠紩浠呭彲鐢ㄤ簬 MyISAM 琛紱浠栦滑鍙互浠CHAR VARCHARTEXT鍒椾腑浣滀负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()鍑芥暟瀵逛簬涓涓瓧绗︿覆鎵ц璧勬枡搴撳唴鐨勮嚜鐒惰瑷鎼滅储銆備竴涓祫鏂欏簱灏辨槸11涓垨2涓寘鍚湪FULLTEXT鍐呯殑鍒椼傛悳绱㈠瓧绗︿覆浣滀负瀵AGAINST()鐨勫弬鏁拌岃缁欏畾銆傚浜庤〃涓殑姣忎竴琛, MATCH() 杩斿洖涓涓浉鍏冲硷紝鍗, 鎼滅储瀛楃涓插拰 MATCH()琛ㄤ腑鎸囧畾鍒椾腑璇ヨ鏂囧瓧涔嬮棿鐨勪竴涓浉浼兼у害閲忋

鍦ㄩ粯璁ょ姸鎬佷笅, 鎼滅储鐨勬墽琛屾柟寮忎负涓嶅尯鍒嗗ぇ灏忓啓鏂瑰紡銆傜劧鑰,浣犲彲浠ラ氳繃瀵圭紪鍏ョ储寮曠殑鍒椾娇鐢ㄤ簩杩涘埗鎺掑簭鏂瑰紡鎵ц鍖哄垎澶у皬鍐欑殑鍏ㄦ枃鎼滅储銆 渚嬪锛屽彲浠ュ悜涓涓娇鐢latin1瀛楃闆嗙殑鍒楃粰瀹latin1_bin 鐨勬帓搴忔柟寮忥紝瀵逛簬鍏ㄦ枃鎼滅储鍖哄垎澶у皬鍐欍

濡備笂杩版墍涓句緥瀛愶紝褰MATCH()琚敤鍦ㄤ竴涓 WHERE 璇彞涓椂锛岀浉鍏冲兼槸闈炶礋娴偣鏁般傞浂鐩稿叧鐨勬剰鎬濇槸娌℃湁鐩镐技鎬с傜浉鍏虫х殑璁$畻鏄熀浜庤琛屼腑鍗曡瘝鐨勬暟鐩, 璇ヨ涓嫭鐗瑰瓙鐨勬暟鐩紝璧勬枡搴撲腑鍗曡瘝鐨勬绘暟锛屼互鍙婂寘鍚壒娈婅瘝鐨勬枃浠()鏁扮洰銆

瀵逛簬鑷劧璇█鍏ㄦ枃鎼滅储锛岃姹MATCH() 鍑芥暟涓懡鍚嶇殑鍒楀拰浣犵殑琛ㄤ腑涓浜FULLTEXT绱㈠紩涓寘鍚殑鍒楃浉鍚屻傚浜庡墠杩伴棶璁紝 娉ㄦ剰锛MATCH()鍑芥暟(棰樼洰鍙婂叏鏂)涓墍鍛藉悕鐨勫垪鍜屾枃绔犺〃鐨FULLTEXT绱㈠紩涓殑鍒楃浉鍚屻傝嫢瑕佸垎鍒悳绱㈤鐩拰鍏ㄦ枃锛屽簲璇ュ姣忎釜鍒楀垱寤FULLTEXT绱㈠紩銆

鎴栬呬篃鍙互杩愯甯冨皵鎼滅储鎴栦娇鐢ㄦ煡璇㈡墿灞曡繘琛屾悳绱€傚叧浜庤繖浜涙悳绱㈢被鍨嬬殑璇存槑瑙12.7.1鑺傦紝鈥滃竷灏斿叏鏂囨悳绱⑩12.7.2鑺傦紝鈥滃叏鏂囨悳绱㈠甫鏌ヨ鎵╁睍鈥

涓婇潰鐨勪緥瀛愬熀鏈笂灞曠ず浜嗘庢牱浣跨敤杩斿洖琛岀殑鐩稿叧鎬ч『搴忔笎寮辩殑MATCH()鍑芥暟銆傝屼笅闈㈢殑渚嬪瓙鍒欏睍绀轰簡鎬庢牱鏄庣‘鍦版绱㈢浉鍏冲笺傝繑鍥炶鐨勯『搴忔槸涓嶅畾鐨勶紝鍘熷洜鏄  SELECT 璇彞涓嶅寘鍚 WHEREORDER 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 绱㈠紩娣诲姞鍗曡瘝鎴栧叾瀹冪紪鍏ョ储寮曠殑鏈锛屼綘蹇呴』瀵瑰畠浠繘琛岄澶勭悊锛屼娇鍏惰涓浜涜濡"涔嬬被鐨勪换鎰忓垎闅旂鍒嗛殧寮銆

涓浜涜瘝鍦ㄥ叏鏂囨悳绱腑浼氳蹇界暐锛 

  • 浠讳綍杩囦簬鐭殑璇嶉兘浼氳蹇界暐銆 鍏ㄦ枃鎼滅储鎵鑳芥壘鍒扮殑璇嶇殑榛樿鏈灏忛暱搴︿负 4涓瓧绗︺
  • 鍋滄瀛椾腑鐨勮瘝浼氳蹇界暐銆傜鐢ㄨ瘝灏辨槸涓涓儚鈥the鈥 鎴栤some鈥 杩欐牱杩囦簬骞冲父鑰岃璁や负鏄笉鍏疯涔夌殑璇嶃傚瓨鍦ㄤ竴涓唴缃殑鍋滄瀛楋紝 浣嗗畠鍙互閫氳繃鐢ㄦ埛鑷畾涔夊垪琛ㄨ鏀瑰啓銆傝鍙傝12.7.5鑺傦紝鈥滃井璋僊ySQL鍏ㄦ枃鎼滅储鈥

榛樿鐨勫仠姝㈠瓧鍦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% 鐨勫煙鍊兼彁渚涢噸瑕佺殑钑存兜鎿嶄綔锛氳嫢浣犲垱寤轰簡涓涓〃锛屽苟涓斿彧灏嗘枃绔犵殑12琛屾彃鍏ュ叾涓紝 鑰屾枃涓殑姣忎釜鍗曡瘝鍦ㄦ墍鏈夎涓嚭鐜扮殑鏈虹巼鑷冲皯涓  50% 銆傞偅涔堢粨鏋滄槸浣犱粈涔堜篃涓嶄細鎼滅储鍒般備竴瀹氳鎻掑叆鑷冲皯3琛岋紝骞朵笖澶氬鐩婂杽銆傞渶瑕佺粫杩囪50% 闄愬埗鐨勭敤鎴峰彲浣跨敤甯冨皵鎼滅储浠g爜锛涜12.7.1鑺傦紝鈥滃竷灏斿叏鏂囨悳绱⑩

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鈥濈殑琛屻

甯冨皵鍏ㄦ枃鎼滅储鍏锋湁浠ヤ笅鐗圭偣锛

  • 瀹冧滑涓嶄娇鐢 50% 鍩熷笺.
  • 瀹冧滑涓嶄細鎸夌収鐩稿叧鎬ф笎寮辩殑椤哄簭灏嗚杩涜鍒嗙被銆備綘鍙互浠庝笂杩伴棶璇㈢粨鏋滀腑鐪嬪埌杩欎竴鐐癸細鐩稿叧鎬ф渶楂樼殑琛屾槸涓涓寘鍚袱涓MySQL鈥 鐨勮锛屼絾瀹冭鍒楀湪鏈鍚庣殑浣嶇疆锛岃屼笉鏄紑澶翠綅缃
  • 鍗充娇娌℃湁FULLTEXT锛屽畠浠粛鐒跺彲浠ュ伐浣滐紝灏界杩欑鏂瑰紡鐨勬悳绱㈡墽琛岀殑閫熷害闈炲父涔嬫參銆
  • 鏈灏忓崟璇嶉暱搴﹀叏鏂囧弬鏁板拰鏈澶у崟璇嶉暱搴﹀叏鏂囧弬鏁板潎閫傜敤銆
  • 鍋滄瀛楅傜敤銆

甯冨皵鍏ㄦ枃鎼滅储鐨勬ц兘鏀寔浠ヤ笅鎿嶄綔绗︼細 

  • +

涓涓墠瀵肩殑鍔犲彿琛ㄧず璇ュ崟璇蹇呴』 鍑虹幇鍦ㄨ繑鍥炵殑姣忎竴琛岀殑寮澶翠綅缃

  • -

涓涓墠瀵肩殑鍑忓彿琛ㄧず璇ュ崟璇涓瀹氫笉鑳鍑虹幇鍦ㄤ换浣曡繑鍥炵殑琛屼腑銆

  • (鏃犳搷浣滅)

鍦ㄩ粯璁ょ姸鎬佷笅(褰撴病鏈夋寚瀹 + 鎴栤撶殑鎯呭喌涓)锛岃鍗曡瘝鍙湁鍙棤锛屼絾鍚湁璇ュ崟璇嶇殑琛岀瓑绾ц緝楂樸傝繖鍜MATCH() ... AGAINST()涓嶄娇鐢IN BOOLEAN MODE淇敼绋嬪簭鏃剁殑杩愪綔寰堢被浼笺  

  • > <

杩欎袱涓搷浣滅鐢ㄦ潵鏀瑰彉涓涓崟璇嶅璧嬩簣鏌愪竴琛岀殑鐩稿叧鍊肩殑褰卞搷銆 > 鎿嶄綔绗﹀寮哄叾褰卞搷锛岃 <鎿嶄綔绗﹀垯鍑忓急鍏跺奖鍝嶃傝鍙傝涓嬮潰鐨勪緥瀛愩 

  • ( )

鎷彿鐢ㄦ潵灏嗗崟璇嶅垎鎴愬瓙琛ㄨ揪寮忋傛嫭鍏ユ嫭鍙风殑閮ㄥ垎鍙互琚祵濂椼

  • ~

涓涓墠瀵肩殑浠e瓧鍙风敤浣滃惁瀹氱锛 鐢ㄦ潵鍚﹀畾鍗曡瘝瀵硅琛岀浉鍏虫х殑褰卞搷銆 杩欏浜庢爣璁扳noise锛堟棤鐢ㄤ俊鎭級鈥濈殑鍗曡瘝寰堟湁鐢ㄣ傚寘鍚繖绫诲崟璇嶇殑琛岃緝鍏跺畠琛岀瓑绾т綆锛屼絾鍥犲叾鍙兘浼氬拰-鍙峰悓鏃朵娇鐢紝鍥犺屼笉浼氬湪浠讳綍鏃跺欓兘娲惧嚭鎵鏈夋棤鐢ㄤ俊鎭銆

  • *

鏄熷彿鐢ㄤ綔鎴柇绗︺備簬鍏跺畠绗﹀彿涓嶅悓鐨勬槸锛屽畠搴斿綋琚杩藉姞鍒拌鎴柇鐨勮瘝涓娿

  • "

涓涓鎷叆鍙屽紩鍙风殑鐭 (") 鍙拰瀛楅潰涓鍖呭惈璇ョ煭璇杈撳叆鏍煎紡鐨勮杩涜鍖归厤銆傚叏鏂囧紩鎿庡皢鐭鎷嗗垎鎴愬崟璇嶏紝鍦FULLTEXT绱㈠紩涓悳绱㈣鍗曡瘝銆   闈炲崟璇嶅瓧绗︿笉闇瑕佷弗瀵嗙殑鍖归厤锛氱煭璇悳绱㈠彧瑕佹眰绗﹀悎鎼滅储鐭鍖呭惈鐨勫崟璇嶄笖鍗曡瘝鐨勬帓鍒楅『搴忕浉鍚岀殑鍐呭銆備緥濡傦紝 "test phrase" 绗﹀悎 "test, phrase"

鑻ョ储寮曚腑涓嶅瓨鍦ㄨ鐭鍖呭惈鐨勫崟璇嶏紝鍒欑粨鏋滀负绌恒備緥濡傦紝鑻ユ墍鏈夊崟璇嶉兘鏄鐢ㄨ瘝锛屾垨鏄暱搴﹂兘灏忎簬缂栧叆绱㈠紩鍗曡瘝鐨勬渶灏忛暱搴︼紝鍒欑粨鏋滀负绌恒

浠ヤ笅渚嬪瓙灞曠ず浜嗕竴浜涗娇鐢ㄥ竷灏斿叏鏂囩鍙风殑鎼滅储瀛楃涓诧細

  • 'apple banana'

瀵绘壘鍖呭惈鑷冲皯涓や釜鍗曡瘝涓殑涓涓殑琛屻

  • '+apple +juice'

瀵绘壘涓や釜鍗曡瘝閮藉寘鍚殑琛屻

  • '+apple macintosh'

瀵绘壘鍖呭惈鍗曡瘝鈥apple鈥濈殑琛岋紝鑻ヨ繖浜涜涔熷寘鍚崟璇嶁macintosh鈥濓紝 鍒欏垪涓烘洿楂樼瓑绾с

  • '+apple -macintosh'

瀵绘壘鍖呭惈鍗曡瘝鈥apple鈥 浣嗕笉鍖呭惈鍗曡瘝 鈥macintosh鈥濈殑琛屻

  • '+apple +(>turnover <strudel)'

瀵绘壘鍖呭惈鍗曡瘝鈥apple鈥濆拰鈥turnover鈥 鐨勮锛屾垨鍖呭惈鈥apple鈥 鍜屸strudel鈥濈殑琛 (鏃犲厛鍚庨『搴),鐒惰屽寘鍚 鈥apple turnover鈥濈殑琛岃緝鍖呭惈鈥apple strudel鈥濈殑琛屾帓鍒楃瓑绾ф洿涓洪珮銆

  • 'apple*'

瀵绘壘鍖呭惈鈥apple鈥濄佲apples鈥濄佲applesauce鈥濇垨鈥applet鈥濈殑琛屻

  • '"some words"'

瀵绘壘鍖呭惈鍘熺煭璇some words鈥濈殑琛 (渚嬪,鍖呭惈鈥some words of wisdom鈥 鐨勮锛岃岄潪鍖呭惈  some noise words鈥濈殑琛)銆傛敞鎰忓寘鍥磋瘝缁勭殑鈥"鈥 绗﹀彿鏄晫瀹氱煭璇殑鎿嶄綔绗﹀瓧绗︺傚畠浠笉鏄寘鍥存悳绱㈠瓧绗︿覆鏈韩鐨勫紩鍙枫

12.7.2. 鍏ㄦ枃鎼滅储甯︽煡璇㈡墿灞

鍏ㄦ枃鎼滅储鏀寔鏌ヨ鎵╁睍鍔熻兘 (鐗瑰埆鏄叾澶氬彉鐨勨滅洸鏌ヨ鎵╁睍鍔熻兘鈥 )銆傝嫢鎼滅储鐭鐨勯暱搴﹁繃鐭, 閭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鈥濅竴璇嶇殑鎵鏈変功鍚嶃

娉ㄩ噴锛  鐩叉煡璇㈡墿灞曞姛鑳藉緢瀹规槗杩斿洖闈炵浉鍏虫枃浠惰屽鍔犳棤鐢ㄤ俊鎭紝鍥犳鍙湁鍦ㄦ煡璇竴涓暱搴﹀緢鐭殑鐭鏃舵墠鏈夊繀瑕佷娇鐢ㄨ繖椤瑰姛鑳姐

12.7.3. 鍏ㄦ枃鍋滄瀛

浠ヤ笅琛ㄥ垪鍑轰簡榛樿鐨勫叏鏂囧仠姝㈠瓧锛 

a'sableaboutaboveaccording
accordinglyacrossactuallyafterafterwards
againagainstain'tallallow
allowsalmostalonealongalready
alsoalthoughalwaysamamong
amongstanandanotherany
anybodyanyhowanyoneanythinganyway
anywaysanywhereapartappearappreciate
appropriatearearen'taroundas
asideaskaskingassociatedat
availableawayawfullybebecame
becausebecomebecomesbecomingbeen
beforebeforehandbehindbeingbelieve
belowbesidebesidesbestbetter
betweenbeyondbothbriefbut
byc'monc'scamecan
can'tcannotcantcausecauses
certaincertainlychangesclearlyco
comcomecomesconcerningconsequently
considerconsideringcontaincontainingcontains
correspondingcouldcouldn'tcoursecurrently
definitelydescribeddespitediddidn't
differentdodoesdoesn'tdoing
don'tdonedowndownwardsduring
eacheduegeighteither
elseelsewhereenoughentirelyespecially
etetceveneverevery
everybodyeveryoneeverythingeverywhereex
exactlyexampleexceptfarfew
fifthfirstfivefollowedfollowing
followsforformerformerlyforth
fourfromfurtherfurthermoreget
getsgettinggivengivesgo
goesgoinggonegotgotten
greetingshadhadn'thappenshardly
hashasn'thavehaven'thaving
hehe'shellohelphence
herherehere'shereafterhereby
hereinhereuponhersherselfhi
himhimselfhishitherhopefully
howhowbeithoweveri'di'll
i'mi'veieifignored
immediateininasmuchincindeed
indicateindicatedindicatesinnerinsofar
insteadintoinwardisisn't
itit'dit'llit'sits
itselfjustkeepkeepskept
knowknowsknownlastlately
laterlatterlatterlyleastless
lestletlet'slikeliked
likelylittlelooklookinglooks
ltdmainlymanymaymaybe
memeanmeanwhilemerelymight
moremoreovermostmostlymuch
mustmymyselfnamenamely
ndnearnearlynecessaryneed
needsneitherneverneverthelessnew
nextninenonobodynon
nonenoonenornormallynot
nothingnovelnownowhereobviously
ofoffoftenohok
okayoldononceone
onesonlyontoorother
othersotherwiseoughtourours
ourselvesoutoutsideoveroverall
ownparticularparticularlyperperhaps
placedpleasepluspossiblepresumably
probablyprovidesquequiteqv
ratherrdrereallyreasonably
regardingregardlessregardsrelativelyrespectively
rightsaidsamesawsay
sayingsayssecondsecondlysee
seeingseemseemedseemingseems
seenselfselvessensiblesent
seriousseriouslysevenseveralshall
sheshouldshouldn'tsincesix
sosomesomebodysomehowsomeone
somethingsometimesometimessomewhatsomewhere
soonsorryspecifiedspecifyspecifying
stillsubsuchsupsure
t'staketakentelltends
ththanthankthanksthanx
thatthat'sthatsthetheir
theirsthemthemselvesthenthence
therethere'sthereaftertherebytherefore
thereintheresthereuponthesethey
they'dthey'llthey'rethey'vethink
thirdthisthoroughthoroughlythose
thoughthreethroughthroughoutthru
thustotogethertootook
towardtowardstriedtriestruly
trytryingtwicetwoun
underunfortunatelyunlessunlikelyuntil
untoupuponususe
usedusefulusesusingusually
valuevariousveryviaviz
vswantwantswaswasn't
waywewe'dwe'llwe're
we'vewelcomewellwentwere
weren'twhatwhat'swhateverwhen
whencewheneverwherewhere'swhereafter
whereaswherebywhereinwhereuponwherever
whetherwhichwhilewhitherwho
who'swhoeverwholewhomwhose
whywillwillingwishwith
withinwithoutwon'twonderwould
wouldwouldn'tyesyetyou
you'dyou'llyou'reyou'veyour
yoursyourselfyourselveszero

12.7.4. 鍏ㄦ枃闄愬畾鏉′欢

  • 鍏ㄦ枃鎼滅储鍙傜敤浜 MyISAM 琛ㄣ
  • 鍏ㄦ枃鎼滅储鍙互鍚屽ぇ澶氭暟澶氬瓧鑺傚瓧绗﹂泦涓璧蜂娇鐢ㄣUnicode灞炰簬渚嬪鎯呭喌鍙娇鐢utf8 瀛楃闆 , 鑰岄潪ucs2瀛楃闆嗐
  • 璇稿姹夎鍜屾棩璇繖鏍风殑琛ㄦ剰璇█娌℃湁鑷畾鐣岀銆傚洜姝 FULLTEXT鍒嗘瀽绋嬪簭涓嶈兘纭畾鍦ㄨ繖浜涙垨鍏跺畠鐨勮繖绫昏瑷涓瘝鐨勮捣濮嬪拰缁撴潫鐨勪綅缃鍏堕殣鍚搷浣滃強璇ラ棶棰樼殑涓浜涘伐浣滃尯鍦12.7鑺傦紝鈥滃叏鏂囨悳绱㈠姛鑳解鏈夎缁嗚杩般
  • 鑻ユ敮鎸佸湪涓涓崟鐙〃涓娇鐢ㄥ瀛楃闆嗭紝鍒欐墍鏈 FULLTEXT绱㈠紩涓殑鍒 蹇呴』浣跨敤鍚屾牱鐨勫瓧绗﹂泦鍜屽簱銆
  • MATCH()鍒楀垪琛ㄥ繀椤诲悓璇ヨ〃涓竴浜 FULLTEXT绱㈠紩瀹氫箟涓殑鍒楀垪琛ㄥ畬鍏ㄧ鍚,闄ら潪MATCH()IN BOOLEAN MODE
  • AGAINST() 鐨勫弬鏁板繀椤绘槸涓涓父鏁板瓧绗︿覆銆 

12.7.5. 寰皟MySQL鍏ㄦ枃鎼滅储

MySQL鐨勫叏鏂囨悳绱㈠閲忓嚑涔庝笉鍏锋湁鐢ㄦ埛璋冭妭鍙傛暟銆傚亣濡備綘鎷ユ湁涓涓 MySQL婧愬垎甯冿紝浣犲氨鑳藉鍏ㄦ枃鎼滅储鎬ц兘琛屼娇鏇村鎺у埗锛屽師鍥犳槸涓浜涘彉鍖栭渶瑕佹簮浠g爜淇敼銆傝鍙傝2.8鑺傦紝鈥滀娇鐢ㄦ簮鐮佸垎鍙戠増瀹夎MySQL鈥

娉ㄦ剰锛屼负浜嗘洿鍔犳湁鏁堬紝闇瑕佸鍏ㄦ枃鎼滅储璋ㄦ厧璋冭妭銆傚疄闄呬笂锛屽湪澶у鏁版儏鍐典笅淇敼榛樿鎬ц兘鍙兘闄嶄綆鍏舵ц兘銆 闄ら潪浣犵煡閬撹嚜宸卞湪鍋氫粈涔堬紝鍚﹀垯涓嶈鏀瑰彉 MySQL婧愩 

涓嬭堪鐨勫ぇ澶氭暟鍏ㄦ枃鍙橀噺蹇呴』鍦ㄦ湇鍔″櫒鍚姩鏃惰璁剧疆銆備负浜嗘敼鍙樺畠浠紝杩樿閲嶆柊鍚姩鏈嶅姟鍣紱鍦ㄦ湇鍔″櫒姝e湪杩愯鏈熼棿锛屼粬浠笉浼氳鏀瑰彉銆 

涓浜涘彉閲忕殑鏀瑰彉闇瑕佷綘閲嶅缓琛ㄤ腑鐨 FULLTEXT 绱㈠紩銆傛湰绔犵粨灏鹃儴鍒嗙粰鍑轰簡鍏舵湁鍏虫搷浣滆鏄庛

  • ft_min_word_len and ft_max_word_len绯荤粺鑷彉閲忚瀹氫簡琚紪鍏ョ储寮曞崟璇嶇殑鏈灏忛暱搴﹀拰鏈澶ч暱搴︺(5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥.) 榛樿鐨勬渶灏忓间负鍥涗釜瀛楃锛涢粯璁ょ殑鏈澶у煎彇鍐充簬浣跨敤鐨 MySQL 鐗堟湰銆傚亣濡備綘鏀瑰彉浠绘剰涓涓硷紝閭d箞浣犲繀椤婚噸寤轰綘鐨 FULLTEXT绱㈠紩銆 渚嬪锛岃嫢浣犲笇鏈涗竴涓3瀛楃鐨勫崟璇嶅彉涓哄彲鏌ユ壘椤癸紝鍒欏彲浠ラ氳繃灏嗕互涓嬭绉诲姩鍒颁竴涓緵閫夋嫨鏂囦欢閲岋紝浠庤岃缃 ft_min_word_len 鍙橀噺锛

                [mysqld]

                ft_min_word_len=3

鐒跺悗閲嶆柊鍚姩鏈嶅姟鍣紝閲嶅缓浣犵殑 FULLTEXT绱㈠紩銆傚悓鏃惰繕瑕佺壒鍒敞鎰忚琛ㄥ悗闈㈢殑璇存槑涓殑鍏充簬myisamchk鐨勬敞閲娿

  • 鑻ヨ瑕嗙洊榛樿鍋滄瀛楋紝鍒欏彲璁剧疆 ft_stopword_file 绯荤粺鍙橀噺銆 (5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥鍙橀噺鍊煎簲涓哄寘鍚仠姝㈠瓧鐨勬枃浠惰矾寰勫悕, 鎴栨槸鐢ㄦ潵鎴绂佺敤璇嶈繃婊ょ殑绌哄瓧绗︿覆銆傚湪鏀瑰彉浜嗚繖涓彉閲忕殑鍊兼垨绂佺敤璇嶆枃浠剁殑鍐呭鍚, 閲嶅缓浣犵殑 FULLTEXT绱㈠紩銆

鍋滄瀛楁槸鑷敱褰㈡佺殑锛屾崲瑷涔嬶紝浣犲彲浣跨敤浠讳綍璇稿newline spacecomma杩欐牱鐨勯潪瀛楁瘝鏁板瓧瀛楃鏉ュ垎闅旂鐢ㄨ瘝銆 涓嬪垝绾垮瓧绗(_) 鍜岃瑙嗕负鍗曡瘝鐨勪竴閮ㄥ垎鐨勫崟寮曞彿 (')渚嬪銆傚仠姝㈠瓧瀛楃闆嗕负鏈嶅姟鍣ㄩ粯璁ゅ瓧绗﹂泦锛涜10.3.1鑺傦紝鈥滄湇鍔″櫒瀛楃闆嗗拰鏍″鈥.

  •  鑷劧璇█鏌ヨ鐨50%闃堝肩敱鎵閫夋嫨鐨勭壒鍒潈琛℃柟妗堟墍鍐冲畾銆傝嫢瑕侀樆姝㈠畠锛myisam/ftdefs.h 涓鎵句互涓嬭锛

                #define GWS_IN_USE GWS_PROB

灏嗚琛屾敼涓猴細

#define GWS_IN_USE GWS_FREQ

鐒跺悗閲嶆柊缂栬瘧 MySQL銆傛鏃朵笉闇瑕侀噸寤虹储寮曘傛敞閲杩欐牱鍋氫綘浼涓ラ噸鐨By 闄嶄綆 MySQLMATCH()鍑芥暟鎻愪緵鍚堥傜殑鐩稿叧鍊煎緱鑳藉姏銆傚亣濡備綘浜夊緱闇瑕佹悳绱㈣繖鏍风殑鏅氳瘝锛岃屼娇鐢IN BOOLEAN MODE浠f浛鐨勬晥鏋滄洿濂斤紝鍥犱负瀹冧笉閬靛惊 50% 闃堝笺

  • 瑕佹敼鍙樼敤浜庡竷灏斿叏鏂囨悳绱㈢殑鎿嶄綔绗︼紝璁剧疆 ft_boolean_syntax 绯荤粺鍙橀噺銆 杩欎釜鍙橀噺涔熷彲浠ュ湪鏈嶅姟鍣ㄨ繍琛屾椂琚敼鍙橈紝浣嗕綘蹇呴』鏈SUPER 鐗规潈鎵嶈兘杩欎箞鍋氥傚湪杩欑鎯呭喌涓嬩笉闇瑕侀噸寤虹储寮曘 瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥, 瀹冨悜鎴戜滑璇存槑浜嗘庢牱浣跨敤杩欎釜鍙橀噺鐨勮鍒欍

鍋囧浣犳敼鍙樹簡褰卞搷绱㈠紩鐨勫叏鏂囧彉閲 (ft_min_word_len ft_max_word_lenft_stopword_file),鎴栧亣濡備綘鏀瑰彉浜嗙鐢ㄨ瘝鏂囦欢鏈韩锛屽垯浣犲繀椤诲湪鏀瑰彉鍜岄噸鏂板惎鍔ㄦ湇鍔″櫒鍚庨噸寤轰綘鐨 FULLTEXT绱㈠紩銆傝繖鏃讹紝瑕侀噸寤虹储寮曪紝 鍙渶杩涜涓涓 QUICK 淇悊鎿嶄綔锛 

mysql> REPAIR TABLE tbl_name QUICK;

娉ㄦ剰锛屽亣濡備綘浣跨敤  myisamchk 鏉ユ墽琛屼竴椤逛慨鏀硅〃绱㈠紩鐨勬搷浣 (璇稿淇悊鎴栧垎鏋 ), 鍒欎娇鐢ㄦ渶灏忓崟璇嶉暱搴﹀拰鏈澶у崟璇嶉暱搴︿互鍙婂仠姝㈠瓧鐨勯粯璁ゅ叏鏂囧弬鏁板奸噸寤FULLTEXT绱㈠紩锛岄櫎闈炰綘宸插彟澶栨寚瀹氥傝繖浼氬鑷撮棶璇㈠け璐ャ

鍙戠敓杩欎釜闂鐨勫師鍥犳槸鍙湁鏈嶅姟鍣ㄨ璇嗚繖浜涘弬鏁般傚畠浠殑瀛樺偍浣嶇疆涓嶅湪  MyISAM 绱㈠紩鏂囦欢涓傝嫢浣犲凡缁忎慨鏀逛簡鏈灏忓崟璇嶉暱搴︽垨鏈澶у崟璇嶉暱搴︽垨鏈嶅姟鍣ㄤ腑鐨勫仠姝㈠瓧锛屼负閬垮厤杩欎釜闂锛屼负浣犲mysqld鎵浣跨敤鐨myisamchk 鎸囧畾鍚屾牱鐨 ft_min_word_len ft_max_word_lenft_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 TABLEOPTIMIZE TABLEALTER TABLE鏉ヤ唬鏇夸娇鐢 myisamchk 銆傝繖浜涜鍙ラ氳繃鏈嶅姟鍣ㄦ潵鎵ц锛屾湇鍔″櫒鐭ラ亾浣跨敤鍝釜鍏ㄦ枃鍙傛暟鍊兼洿鍔犲悎閫傘

12.8. Cast鍑芥暟鍜屾搷浣滅

  • BINARY

BINARY鎿嶄綔绗﹀皢鍚庨潰鐨勫瓧绗︿覆鎶涚粰涓涓簩杩涘埗瀛楃涓层杩欐槸涓绉嶇畝鍗曠殑鏂瑰紡鏉ヤ績浣块愬瓧鑺傝屼笉鏄愬瓧绗︾殑杩涜鍒楁瘮杈冦傝繖浣垮緱姣旇緝鍖哄垎澶у皬鍐欙紝鍗充娇璇ュ垪涓嶈瀹氫箟涓 BINARY BLOBBINARY涔熶細浜х敓缁撳熬绌虹櫧锛屼粠鑰屾洿鍔犳樉鐪笺

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(expr AS type), CONVERT(expr,type) , CONVERT(expr USING transcoding_name)

CAST() CONVERT() 鍑芥暟鍙敤鏉ヨ幏鍙栦竴涓被鍨嬬殑鍊硷紝骞朵骇鐢熷彟涓涓被鍨嬬殑鍊笺

杩欎釜绫诲瀷 鍙互鏄互涓嬪煎叾涓殑 涓涓細 

    • BINARY[(N)]
    • CHAR[(N)]
    • DATE
    • DATETIME
    • DECIMAL
    • SIGNED [INTEGER]
    • TIME
    • UNSIGNED [INTEGER]

BINARY 浜х敓涓涓簩杩涘埗瀛楃涓层傚叧浜庡畠鎬庢牱褰卞搷姣旇緝缁撴灉鐨勮鏄庤鏈珷涓 BINARY鎿嶄綔绗﹂」銆

鍋囧缁欏畾浜嗛殢鎰忛暱搴N锛屽垯 BINARY[N] cast浣跨敤璇ュ弬鏁扮殑涓嶅浜 N 涓瓧鑺傘傚悓鏍风殑锛 CHAR[N]浼氫娇 cast 浣跨敤璇ュ弬鏁扮殑涓嶅浜N 涓瓧绗︺

CAST() and CONVERT(... USING ...) 鏄爣鍑 SQL璇硶銆CONVERT()鐨勯潪USING 鏍煎紡鏄ofis ODBC璇硶銆

甯︽湁USINGCONVERT() 琚敤鏉ュ湪涓嶅悓鐨勫瓧绗﹂泦涔嬮棿杞寲鏁版嵁銆傚湪 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

鑻ヤ綘鍦ㄤ竴涓畻鏈繍绠椾腑浣跨敤浜嗕竴涓瓧绗︿覆锛屽畠浼氳杞寲涓轰竴涓诞鐐规暟銆 

12.9. 鍏朵粬鍑芥暟

12.9.1. 浣嶅嚱鏁

瀵逛簬姣旂壒杩愮畻锛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姣旂壒鏃犵鍙锋暣鏁般

  • BIT_COUNT(N)

杩斿洖鍙傛暟N 涓墍璁剧疆鐨勬瘮鐗规暟

mysql> SELECT BIT_COUNT(29);

        -> 4

12.9.2. 鍔犲瘑鍑芥暟

鏈妭浠嬬粛浜嗗姞瀵嗗拰鍔犲瘑鍊笺傝嫢浣犳兂瑕佸偍瀛樹竴浜涚敱鍙兘鍖呭惈浠绘剰瀛楄妭鍊肩殑鍔犲瘑鍑芥暟杩斿洖鐨勭粨鏋滐紝浣跨敤BLOB鍒楄屼笉鏄 CHAR VARCHAR 鍒楋紝浠庤岄伩鍏嶇敱浜庣粨灏剧┖鏍肩殑鍒犻櫎鑰屾敼鍙樹竴浜涙暟鎹肩殑娼滃湪闂銆

  • AES_ENCRYPT(str,key_str) , AES_DECRYPT(crypt_str,key_str)

杩欎簺鍑芥暟鍏佽浣跨敤瀹樻柟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涓櫘閬嶉氱敤鐨勫瘑鐮佹渶瀹夊叏鐨勫姞瀵嗗嚱鏁般

  • DECODE(crypt_str,pass_str)

浣跨敤 pass_str 浣滀负瀵嗙爜锛岃В瀵嗗姞瀵嗗瓧绗︿覆 crypt_str crypt_str 搴旇鏄敱ENCODE()杩斿洖鐨勫瓧绗︿覆銆

  • ENCODE(str,pass_str)

浣跨敤pass_str 浣滀负瀵嗙爜锛岃В瀵 str 銆 浣跨敤DECODE()瑙e瘑缁撴灉銆

缁撴灉鏄竴涓拰str闀垮害鐩稿悓鐨勪簩杩涘埗瀛楃涓层傝嫢浣犳兂瑕佸皢鍏朵繚鐣欏湪涓涓垪涓紝鍙娇鐢 BLOB 鍒楃被鍨嬨

  • DES_DECRYPT(crypt_str[,key_str])

浣跨敤DES_ENCRYPT()鍔犲瘑涓涓瓧绗︿覆銆傝嫢鍑虹幇閿欒锛岃繖涓嚱鏁颁細杩斿洖 NULL

娉ㄦ剰锛岃繖涓嚱鏁板彧鏈夊綋MySQLSSL 鐨勬敮鎸佷笅閰嶇疆瀹屾瘯鏃舵墠浼氳繍浣溿傝鍙傝5.8.7鑺傦紝鈥滀娇鐢ㄥ畨鍏ㄨ繛鎺モ.

鍋囧娌℃湁缁欏畾 key_str 鍙傛暟锛  DES_DECRYPT() 浼氶鍏堟鏌ュ姞瀵嗗瓧绗︿覆鐨勭涓涓瓧鑺傦紝 浠庤岀‘瀹氱敤鏉ュ姞瀵嗗師濮嬪瓧绗︿覆鐨DES瀵嗙爜鍏抽敭瀛楁暟瀛楋紝涔嬪悗浠DES鍏抽敭瀛楁枃浠朵腑璇诲彇鍏抽敭瀛椾粠鑰岃В瀵嗕俊鎭備负浣垮叾杩愯锛岀敤鎴峰繀椤讳韩鏈 SUPER 鐗规潈銆傚彲浠ラ夋嫨--des-key-file鏈嶅姟鍣ㄦ寚瀹氬叧閿瓧鏂囦欢銆

鍋囧浣犲悜杩欎釜鍑芥暟浼犻掍竴涓key_str 鍙傛暟锛岃瀛楃涓茶鐢ㄤ綔瑙e瘑淇℃伅鐨勫叧閿瓧銆

crypt_str 鍙傛暟鐪嬭捣鏉ヤ笉鏄竴涓姞瀵嗗瓧绗︿覆锛 MySQL 浼氳繑鍥炵粰瀹氱殑 crypt_str

  • DES_ENCRYPT(str[,(key_num|key_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 蹇呴』鏄竴涓粠00鑼冨洿鍐呯殑鏁板瓧銆傛枃浠朵腑琛岀殑鎺掑垪椤哄簭鏄换鎰忕殑銆 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');

  • ENCRYPT(str[,salt])

浣跨敤Unix crypt() 绯荤粺璋冪敤鍔犲瘑 str salt 鍙傛暟搴斾负涓涓嚦灏戝寘鍚2涓瓧绗︾殑瀛楃涓层傝嫢娌℃湁缁欏嚭 salt 鍙傛暟锛屽垯浣跨敤浠绘剰鍊笺

mysql> SELECT ENCRYPT('hello');

        -> 'VxuFAJXVARROc'

鑷冲皯鍦ㄤ竴浜涚郴缁熶腑锛ENCRYPT()闄や簡str鐨勫墠鍏綅瀛楃涔嬪浼氬拷鐣ユ墍鏈夊唴瀹广傝繖涓涓虹敱涓嬪垝绾跨殑crypt() 绯荤粺璋冪敤鐨勬墽琛屾墍鍐冲畾銆

鍋囧crypt()鍦ㄤ綘鐨勭郴缁熶腑涓嶅彲鐢(姝e鍦 Windows绯荤粺), ENCRYPT() 鍒欎細濮嬬粓杩斿洖NULL銆傞壌浜庤繖涓師鍥狅紝鎴戜滑鍚戜綘鎺ㄨ崘浣跨敤 MD5() SHA1() 鏉ヤ唬鏇,鍥犱负杩欎袱涓嚱鏁伴傚悎鎵鏈夌殑骞冲彴銆

  • MD5(str)

涓哄瓧绗︿覆绠楀嚭涓涓 MD5 128姣旂壒妫鏌ュ拰銆傝鍊间互32浣嶅崄鍏繘鍒舵暟瀛楃殑浜岃繘鍒跺瓧绗︿覆鐨勫舰寮忚繑鍥, 鑻ュ弬鏁颁负 NULL 鍒欎細杩斿洖 NULL銆備緥濡傦紝杩斿洖鍊煎彲琚敤浣滄暎鍒楀叧閿瓧銆

mysql> SELECT MD5('testing');

        -> 'ae2b1fca515949e5d54fb22b8ed95575'

杩欐槸"RSA Data Security, Inc. MD5 Message-Digest Algorithm."

鍋囧浣犳兂瑕佸皢杩欎釜鍊艰浆鍖栦负澶у啓瀛楁瘝锛屽弬瑙12.8鑺傦紝鈥淐ast鍑芥暟鍜屾搷浣滅鈥BINARY鎿嶄綔绗﹂」涓粰鍑虹殑浜岃繘鍒跺瓧绗︿覆杞崲銆

  • OLD_PASSWORD(str)

PASSWORD()鐨勬墽琛屽彉涓烘敼鍠勫畨鍏ㄦф椂锛OLD_PASSWORD()浼氳娣诲姞鍒 MySQLOLD_PASSWORD()杩斿洖浠庡墠鐨PASSWORD()鎵ц鍊( 4.1涔嬪墠)锛屽悓鏃跺厑璁镐綘涓轰换浣4.1 涔嬪墠鐨勯渶瑕佽繛鎺ュ埌浣犵殑5.1 鐗堟湰MySQL鏈嶅姟鍣ㄥ墠瀹㈡埛绔缃瘑鐮侊紝浠庤屼笉鑷充簬灏嗗畠浠垏鏂傝鍙傝5.7.9鑺傦紝鈥淢ySQL 4.1涓殑瀵嗙爜鍝堝笇澶勭悊鈥

  • PASSWORD(str)

浠庡師鏂囧瘑鐮str 璁$畻骞惰繑鍥炲瘑鐮佸瓧绗︿覆锛屽綋鍙傛暟涓 NULL 鏃惰繑鍥 NULL銆傝繖涓嚱鏁扮敤浜庣敤鎴锋巿鏉冭〃鐨Password鍒椾腑鐨勫姞瀵MySQL瀵嗙爜瀛樺偍

mysql> SELECT PASSWORD('badpwd');

        -> '7f84554057dd964b'

PASSWORD() 鍔犲瘑鏄崟鍚戠殑 (涓嶅彲閫 )

PASSWORD() 鎵ц瀵嗙爜鍔犲瘑涓Unix 瀵嗙爜琚姞瀵嗙殑鏂瑰紡涓嶅悓銆傝鍙傝ENCRYPT()

娉ㄩ噴锛 PASSWORD()鍑芥暟鍦MySQL鏈嶅姟鍣ㄤ腑鐨勯壌瀹氱郴缁熶娇鐢紱浣搴斿皢瀹冪敤鍦ㄤ綘涓汉鐨勫簲鐢ㄧ▼搴忎腑銆備负杈惧埌鍚屾牱鐩殑锛屽彲浣跨敤 MD5()SHA1() 浠f浛銆    鏇村鍏充簬鍦ㄦ偍鐨勫簲鐢ㄧ▼搴忎腑澶勭悊瀵嗙爜鍙婂畨鍏ㄩ壌瀹氱殑淇℃伅瑙RFC 2195 

  • SHA1(str) SHA(str)

涓哄瓧绗︿覆绠楀嚭涓涓 SHA1 160姣旂壒妫鏌ュ拰锛屽RFC 3174 (瀹夊叏鏁e垪绠楁硶 )涓墍杩般傝鍊艰浣滀负40浣嶅崄鍏繘鍒舵暟瀛楄繑鍥烇紝鑰屽綋鍙傛暟涓NULL 鏃跺垯杩斿洖 NULL銆傝繖涓嚱鏁扮殑涓涓彲鑳界殑鐢ㄥ灏卞湪浜庡叾浣滀负鏁e垪鍏抽敭瀛椼備綘涔熷彲浠ュ皢鍏朵綔涓哄瓨鍌ㄥ瘑鐮佺殑瀵嗙爜瀹夊叏鍑芥暟浣跨敤銆

mysql> SELECT SHA1('abc');

        -> 'a9993e364706816aba3e25717850c26c9cd0d89d'

SHA1()鍙互琚涓轰竴涓瘑鐮佹洿鍔犲畨鍏ㄧ殑鍑芥暟锛岀浉褰撲簬  MD5() SHA() SHA1()鍏锋湁鐩稿悓鐨勬剰涔夈

12.9.3. 淇℃伅鍑芥暟

  • BENCHMARK(count,expr)

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鎴栦竴涓〃杈惧紡

 

 

涓嬫柟鍊煎緱浼樺厛绾ц緝楂樸

  • COLLATION(str)

杩斿洖鎯犲瓧绗︿覆鍙傛暟鐨勬帓搴忔柟寮忋

mysql> SELECT COLLATION('abc');

        -> 'latin1_swedish_ci'

mysql> SELECT COLLATION(_utf8'abc');

        -> 'utf8_general_ci'

  • CONNECTION_ID()

杩斿洖瀵逛簬杩炴帴鐨勮繛鎺ID (绾跨▼ID)銆傛瘡涓繛鎺ラ兘鏈夊悇鑷殑鍞竴 ID

mysql> SELECT CONNECTION_ID();

        -> 23786

  • CURRENT_USER, CURRENT_USER()

杩斿洖褰撳墠璇濊矾琚獙璇佺殑鐢ㄦ埛鍚嶅拰涓绘満鍚嶇粍鍚堛傝繖涓肩鍚堢‘瀹氫綘鐨勫瓨鍙栨潈闄愮殑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鐢ㄦ埛鍚 (姝eUSER()鍑芥暟鐨勫兼墍鎸囩ず鐨), 鏈嶅姟鍣ㄥ嵈浣跨敤涓涓尶鍚嶇殑鐢ㄦ埛璐︽埛纭璇ュ鎴风 (CURRENT_USER()鍊煎緱绌虹敤鎴峰悕閮ㄥ垎 )銆傝繖绉嶆儏鍐靛彂鐢熺殑涓涓師鍥犳槸 One 鍦ㄥ悜 davida鐨勬巿鏉冨垪琛ㄤ腑娌℃湁瓒冲鐨勮处鎴枫 

 CURRENT_USER() 杩斿洖鐨勫瓧绗︿覆浣跨敤 utf8瀛楃闆嗐

  • DATABASE()

杩斿洖浣跨敤 utf8 瀛楃闆嗙殑榛樿(褰撳墠)鏁版嵁搴撳悕銆傚湪瀛樺偍绋嬪簭閲岋紝榛樿鏁版嵁搴撴槸鍚岃绋嬪簭鍚戝叧鑱旂殑鏁版嵁搴擄紝浣嗗苟涓嶄竴瀹氫笌璋冪敤璇鐨勯粯璁ゆ暟鎹簱鐩稿悓銆 

mysql> SELECT DATABASE();

        -> 'test'

鑻ユ病鏈夐粯璁ゆ暟鎹簱, DATABASE()杩斿洖 NULL

  • FOUND_ROWS()

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 涓鍚屼娇鐢ㄧ殑鏉′欢鏄細

    •  SQL_CALC_FOUND_ROWS 鍏抽敭璇嶅繀椤诲嚭鐜板湪UNION鐨勭涓涓 SELECT涓
    •  FOUND_ROWS()鐨勫煎彧鏈夊湪浣跨敤 UNION ALL鏃舵墠鏄簿纭殑銆傝嫢浣跨敤涓嶅甫ALLUNION锛屽垯浼氬彂鐢熶袱娆″垹闄わ紝 鑰  FOUND_ROWS() 鐨勬寚鍙渶杩戜技鐨勩
    • 鍋囪嫢UNION 涓病鏈夊嚭鐜  LIMIT 锛屽垯SQL_CALC_FOUND_ROWS 琚拷鐣ワ紝杩斿洖涓存椂琛ㄤ腑鐨勫垱寤虹殑鐢ㄦ潵澶勭悊UNION鐨勮鏁般
  • LAST_INSERT_ID() LAST_INSERT_ID(expr)

鑷姩杩斿洖鏈鍚庝竴涓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()

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 )

  • SCHEMA()

杩欎釜鍑芥暟鍜 DATABASE()鍏锋湁鐩稿悓鐨勬剰涔夈

  • SESSION_USER()

SESSION_USER() USER()鍏锋湁鐩稿悓鐨勬剰涔夈

  • SYSTEM_USER()

SYSTEM_USER() 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'

  • VERSION()

杩斿洖鎸囩ず MySQL 鏈嶅姟鍣ㄧ増鏈殑瀛楃涓层傝繖涓瓧绗︿覆浣跨敤 utf8 瀛楃闆嗐

mysql> SELECT VERSION();

        -> '5.1.2-alpha-standard'

娉ㄦ剰锛屽亣濡備綘鐨勭増鏈瓧绗︿覆浠-log缁撳熬锛岃繖璇存槑鐧诲綍宸茶婵娲汇 

12.9.4. 鍏朵粬鍑芥暟

  • DEFAULT(col_name)

杩斿洖涓涓〃鍒楃殑榛樿鍊笺傝嫢璇ュ垪娌℃湁榛樿鍊煎垯浼氫骇鐢熼敊璇

mysql> UPDATE t SET i = DEFAULT(i)+1 WHERE id < 100;

  • FORMAT(X,D)

灏嗘暟瀛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'

  • GET_LOCK(str,timeout)

璁炬硶浣跨敤瀛楃涓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紑銆

  • INET_ATON(expr)

缁欏嚭涓涓綔涓哄瓧绗︿覆鐨勭綉缁滃湴鍧鐨勭偣鍦板潃琛ㄧず锛岃繑鍥炰竴涓唬琛ㄨ鍦板潃鏁板肩殑鏁存暟銆傚湴鍧鍙互鏄48姣旂壒鍦板潃銆

mysql> SELECT INET_ATON('209.207.224.40');

        -> 3520061480

浜х敓鐨勬暟瀛楁绘槸鎸夌収缃戠粶瀛楄妭椤哄簭銆傚涓婇潰鐨勪緥瀛愶紝鏁板瓧鎸夌収 2092563 + 2072562 + 224256 + 40 杩涜璁$畻銆

INET_ATON() 涔熻兘鐞嗚В鐭牸寮 IP 鍦板潃锛

mysql> SELECT INET_ATON('127.0.0.1'), INET_ATON('127.1');

        -> 2130706433, 2130706433

娉ㄩ噴 鍦ㄥ瓨鍌ㄧ敱INET_ATON() 浜х敓鐨勫兼椂锛屾帹鑽愪綘浣跨敤 INT UNSIGNED 鍒椼傚亣濡備綘浣跨敤 (甯︾鍙) INT, 鍒欑浉搴旂殑绗竴涓叓浣嶇粍澶т簬127IP 鍦板潃鍊间細琚埅鑷  2147483647 (, INET_ATON('127.255.255.255') 鎵杩斿洖鐨勫)銆傝鍙傝11.2鑺傦紝鈥滄暟鍊肩被鍨嬧

  • INET_NTOA(expr)

缁欏畾涓涓暟瀛楃綉缁滃湴鍧 (4 8 姣旂壒),杩斿洖浣滀负瀛楃涓茬殑璇ュ湴鍧鐨勭數鍦板潃琛ㄧず銆

mysql> SELECT INET_NTOA(3520061480);

        -> '209.207.224.40'

  • IS_FREE_LOCK(str)

妫鏌ュ悕涓str鐨勯攣鏄惁鍙互浣跨敤 (鎹㈣█涔,娌℃湁琚皝閿)銆傝嫢閿佸彲浠ヤ娇鐢紝鍒欒繑鍥  1  (娌℃湁浜哄湪鐢ㄨ繖涓攣), 鑻ヨ繖涓攣姝e湪琚娇鐢紝鍒欒繑鍥0 锛屽嚭鐜伴敊璇垯杩斿洖 NULL  (璇稿涓嶆纭殑鍙傛暟 )

  • IS_USED_LOCK(str)

妫鏌ュ悕涓str鐨勯攣鏄惁姝e湪琚娇鐢(鎹㈣█涔,琚皝閿)銆傝嫢琚皝閿侊紝鍒欒繑鍥炰娇鐢ㄨ閿佺殑瀹㈡埛绔殑杩炴帴鏍囪瘑绗︺傚惁鍒欒繑鍥 NULL

  • MASTER_POS_WAIT(log_name,log_pos[,timeout])

璇ュ嚱鏁板浜庢帶鍒朵富浠庡悓姝ュ緢鏈夌敤澶勩傚畠浼氭寔缁皝閿侊紝鐩村埌浠庤澶囬槄璇诲拰搴旂敤涓绘満璁板綍涓墍鏈夎ˉ鍏呰祫鏂欏埌鎸囧畾鐨勪綅缃傝繑鍥炲兼槸鍏朵负鍒拌揪鎸囧畾浣嶇疆鑰屽繀椤荤瓑寰呯殑璁板綍浜嬩欢鐨勬暟鐩傝嫢浠庤澶SQL绾跨▼娌℃湁琚惎鍔ㄣ佷粠璁惧涓绘満淇℃伅灏氭湭鍒濆鍖栥佸弬鏁颁笉姝g‘鎴栧嚭鐜颁换浣曢敊璇紝鍒欒鍑芥暟杩斿洖 NULL銆傝嫢瓒呮椂鏃堕棿琚瓒呰繃锛屽垯杩斿洖-1MASTER_POS_WAIT() 绛夊緟鏈熼棿锛屼粠璁惧SQL绾跨▼涓锛屽垯璇ュ嚱鏁拌繑鍥 NULL銆傝嫢浠庤澶囩敱鎸囧畾浣嶇疆閫氳繃锛屽垯鍑芥暟浼氱珛鍗宠繑鍥炵粨鏋溿

鍋囧宸茬粡鎸囧畾浜嗕竴涓瓒呮椂鏃堕棿鍊硷紝褰 瓒呮椂鏃堕棿 绉掓暟缁忚繃鍚MASTER_POS_WAIT()浼氬仠姝㈢瓑寰呫瓒呮椂鏃堕棿 蹇呴』澶т簬 0锛涗竴涓负闆舵垨涓鸿礋鍊肩殑 瓒呮椂鏃堕棿 琛ㄧず娌℃湁瓒呭競鏃堕棿銆 

  • NAME_CONST(name,value)

杩斿洖缁欏畾鍊笺 褰撶敤鏉ヤ骇鐢熶竴涓粨鏋滈泦鍚堝垪鏃, NAME_CONST()淇冧娇璇ュ垪浣跨敤缁欏畾鍚嶇О銆 

mysql> SELECT NAME_CONST('myname', 14);

+--------+

| myname |

+--------+

|     14 |

+--------+

杩欎釜鍑芥暟琚坊鍔犺繘 MySQL 5.0.12銆傚畠鍙仛鍐呴儴浣跨敤銆 鏈嶅姟鍣ㄥ湪涔﹀啓鏉ヨ嚜鍖呭惈灞閮ㄧ▼搴忓彉閲忕殑瀛樺偍绋嬪簭鐨勮鍙ユ椂浼氱敤鍒板畠锛岃瑙20.4鑺傦紝鈥滃瓨鍌ㄥ瓙绋嬪簭鍜岃Е鍙戠▼搴忕殑浜岃繘鍒舵棩蹇楀姛鑳解銆備綘鍙兘浼氬湪mysqlbinlog 鐨勪功姗变腑鐪嬪埌杩欎釜鍑芥暟銆

  • RELEASE_LOCK(str)

瑙e紑琚GET_LOCK()鑾峰彇鐨勶紝鐢ㄥ瓧绗︿覆str 鎵鍛藉悕鐨勯攣銆傝嫢閿佽瑙e紑锛屽垯杩斿洖  1锛岃嫢鏀圭嚎绋嬪皻鏈垱寤洪攣锛屽垯杩斿洖0 (姝ゆ椂閿佹病鏈夎瑙e紑 ), 鑻ュ懡鍚嶇殑閿佷笉瀛樺湪锛屽垯杩斿洖 NULL銆傝嫢璇ラ攣浠庢湭琚GET_LOCK()鐨勮皟鐢ㄨ幏鍙栵紝鎴栭攣宸茬粡琚彁鍓嶈В寮锛屽垯璇ラ攣涓嶅瓨鍦ㄣ

DO 璇彞鍜RELEASE_LOCK()鍚屾椂浣跨敤寰堟柟渚裤傝鍙傝13.2.2鑺傦紝鈥淒O璇硶鈥

  • SLEEP(duration)

鐫$湢(鏆傚仠) 鏃堕棿涓duration 鍙傛暟缁欏畾鐨勭鏁帮紝鐒跺悗杩斿洖 0銆傝嫢 SLEEP() 琚腑鏂,瀹冧細杩斿洖 1 duration 鎴栬鎴栧寘鎷竴涓粰瀹氱殑浠ュ井绉掍负鍗曚綅鐨勫垎鏁伴儴鍒嗐

  • UUID()

杩斿洖涓涓氱敤鍞竴鏍囪瘑绗(UUID) 锛屽叾浜х敓鐨勬牴鎹槸銆DCE 1.1: 杩滅▼杩囩▼璋冪敤 (闄勫綍A) CAE (鍏叡搴旂敤杞欢鐜) 鐨勮鏄庯紝璇ヤ綔鍝佷簬199710鏈堢敱 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

    • 3涓暟瀛椾粠涓涓椂闂存埑浜х敓銆 
    • 4 涓暟瀛椾繚鎸佹殏鏃跺敮涓鎬э紝浠ラ槻鏃堕棿鎴冲煎け鍘诲崟涓鎬 (渚嬪, 鐢变簬缁忔祹鏃)
    • 5涓暟瀛楁槸涓涓 IEEE 802 鑺傜偣鍙凤紝瀹冩彁渚涚┖闂村敮涓鎬с傝嫢鍚庤呬笉鍙敤锛屽垯鐢ㄤ竴涓殢鏈烘暟瀛楁浛鎹€ (渚嬪, 鐢变簬涓绘満娌℃湁浠ュお缃戝崱锛屾垨鎴戜滑涓嶇煡閬撴庢牱鍦ㄤ綘鐨勬搷浣滅郴缁熶笂鎵惧埌鐣岄潰鐨勬満鍣ㄥ湴鍧 )銆傚亣鑻ヨ繖鏍凤紝绌洪棿鍞竴鎬у氨涓嶈兘寰楀埌淇濊瘉銆傚敖绠″姝わ紝涓涓啿绐佺殑鍙戠敓鏈虹巼杩樻槸闈炲父浣庣殑銆

鐩墠,涓涓晫闈㈢殑 MAC 鍦板潃灏借FreeBSD Linux鑰冭檻鍒般傚湪鍏跺畠鎿嶄綔绯荤粺涓, MySQL浣跨敤闅忔満浜х敓鐨 48姣旂壒鏁板瓧銆

mysql> SELECT UUID();

        -> '6ccd780c-baba-1026-9564-0040f4311e29'

娉ㄦ剰锛 UUID() 涓嶆敮鎸佸鍒跺姛鑳姐

  • VALUES(col_name)

鍦ㄤ竴涓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);

12.10. 涓嶨ROUP BY瀛愬彞鍚屾椂浣跨敤鐨勫嚱鏁板拰淇敼绋嬪簭

12.10.1.聽GROUP BY锛堣仛鍚堬級鍑芥暟

鏈珷璁鸿堪浜嗙敤浜庝竴缁勬暟鍊兼搷浣滅殑 group (闆嗗悎)鍑芥暟銆傞櫎闈炲彟浣滆鏄庯紝 group 鍑芥暟浼氬拷鐣 NULL 鍊笺

鍋囧浣犲湪涓涓笉鍖呭惈 ROUP BY瀛愬彞鐨勮鍙ヤ腑浣跨敤涓涓 group鍑芥暟 锛屽畠鐩稿綋浜庡鎵鏈夎杩涜鍒嗙粍銆

  • AVG([DISTINCT] expr)

杩斿洖expr 鐨勫钩鍧囧笺 DISTINCT 閫夐」鍙敤浜庤繑鍥 expr鐨勪笉鍚屽肩殑骞冲潎鍊笺

鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯AVG()杩斿洖 NULL

mysql> SELECT student_name, AVG(test_score)

    ->        FROM student

    ->        GROUP BY student_name;

  • BIT_AND(expr)

杩斿洖expr涓墍鏈夋瘮鐗圭殑 bitwise AND 銆傝绠楁墽琛岀殑绮剧‘搴︿负64姣旂壒(BIGINT)

鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯杩欎釜鍑芥暟杩斿洖 18446744073709551615 (杩欐槸鏃犵鍙 BIGINT 鍊硷紝鎵鏈夋瘮鐗硅璁剧疆涓 1

  • BIT_OR(expr)

杩斿洖expr 涓墍鏈夋瘮鐗圭殑bitwise OR銆傝绠楁墽琛岀殑绮剧‘搴︿负64姣旂壒(BIGINT)

鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯鍑芥暟杩斿洖 0

  • BIT_XOR(expr)

杩斿洖expr 涓墍鏈夋瘮鐗圭殑bitwise XOR銆傝绠楁墽琛岀殑绮剧‘搴︿负64姣旂壒(BIGINT)

鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯鍑芥暟杩斿洖 0

  • COUNT(expr)

杩斿洖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), 瀛樺偍涓涓簿纭鏁扮殑闂姣旇緝澶氾紝鍘熷洜鏄彲鑳戒細鍙戠敓澶氶噸浜嬬墿澶勭悊, 鑰屾瘡涓兘鍙兘浼氬琛屾暟浜х敓褰卞搷銆

  • COUNT(DISTINCT expr,[expr...])

杩斿洖涓嶅悓鐨勯潪NULL鍊兼暟鐩

鑻ユ壘涓嶅埌鍖归厤鐨勯」锛屽垯COUNT(DISTINCT)杩斿洖 0

mysql> SELECT COUNT(DISTINCT results) FROM student;

MySQL, 浣犻氳繃缁欏畾涓涓〃杈惧紡鍒楄〃鑰岃幏鍙栦笉鍖呭惈NULL 涓嶅悓琛ㄨ揪寮忕粍鍚堢殑鏁扮洰銆傚湪鏍囧噯 SQL,浣犲皢蹇呴』鍦COUNT(DISTINCT ...)涓繛鎺ユ墍鏈夎〃杈惧紡銆

  • GROUP_CONCAT(expr)

璇ュ嚱鏁拌繑鍥炲甫鏈夋潵鑷竴涓粍鐨勮繛鎺ョ殑闈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;

鑻ュ凡缁忚缃簡鏈澶ч暱搴︼紝 鍒欑粨鏋滆鎴嚦杩欎釜鏈澶ч暱搴︺

  • MIN([DISTINCT] expr), MAX([DISTINCT] expr)

杩斿洖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鐨勬湭鏉ョ増鏈腑寰楀埌鏀瑰杽銆 

  • STD(expr) STDDEV(expr)

杩斿洖expr 鐨勬讳綋鏍囧噯鍋忓樊銆傝繖鏄爣鍑 SQL 鐨勫欢浼搞傝繖涓嚱鏁扮殑STDDEV() 褰㈠紡鐢ㄦ潵鎻愪緵鍜Oracle 鐨勫吋瀹规с傚彲浣跨敤鏍囧噯SQL鍑芥暟 STDDEV_POP() 杩涜浠f浛銆

鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯杩欎簺鍑芥暟杩斿洖 NULL

  • STDDEV_POP(expr)

杩斿洖expr 鐨勬讳綋鏍囧噯鍋忓樊(VAR_POP()鐨勫钩鏂规牴)銆備綘涔熷彲浠ヤ娇鐢  STD() STDDEV(), 瀹冧滑鍏锋湁鐩稿悓鐨勬剰涔夛紝鐒惰屼笉鏄爣鍑嗙殑 SQL

鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯STDDEV_POP()杩斿洖 NULL

  • STDDEV_SAMP(expr)

杩斿洖expr 鐨勬牱鏈爣鍑嗗樊 ( VAR_SAMP()鐨勫钩鏂规牴)

鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯STDDEV_SAMP() 杩斿洖 NULL

  • SUM([DISTINCT] expr)

杩斿洖expr 鐨勬绘暟銆 鑻ヨ繑鍥為泦鍚堜腑鏃犱换浣曡锛屽垯 SUM() 杩斿洖NULLDISTINCT 鍏抽敭璇嶅彲鐢ㄤ簬 MySQL 5.1 涓紝姹傚緱expr 涓嶅悓鍊肩殑鎬诲拰銆

鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯SUM()杩斿洖 NULL

  • VAR_POP(expr)

杩斿洖expr 鎬讳綋鏍囧噯鏂瑰樊銆傚畠灏嗚瑙嗕负鎬讳綋锛岃屼笉鏄竴涓牱鏈紝 鎵浠ュ畠灏嗚鏁颁綔涓哄垎姣嶃備綘涔熷彲浠ヤ娇鐢 VARIANCE(),瀹冨叿鏈夌浉鍚岀殑鎰忎箟鐒惰屼笉鏄 鏍囧噯鐨 SQL

鑻ユ壘涓嶅埌鍖归厤鐨勯」锛屽垯VAR_POP()杩斿洖NULL

  • VAR_SAMP(expr)

杩斿洖expr 鐨勬牱鏈柟宸傛洿纭垏鐨勮锛屽垎姣嶇殑鏁板瓧鏄鏁板噺鍘1  

鑻ユ壘涓嶅埌鍖归厤鐨勮锛屽垯VAR_SAMP()杩斿洖NULL

  • VARIANCE(expr)

杩斿洖expr 鐨勬讳綋鏍囧噯鏂瑰樊銆傝繖鏄爣鍑SQL 鐨勫欢浼搞傚彲浣跨敤鏍囧噯SQL 鍑芥暟 VAR_POP() 杩涜浠f浛銆

鑻ユ壘涓嶅埌鍖归厤鐨勯」锛屽垯VARIANCE()杩斿洖NULL

12.10.2.聽GROUP BY淇敼绋嬪簭

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杈撳嚭锛

  • 涓缁勭粰瀹氱殑骞翠唤鍜屽浗瀹剁殑姣忕粍浜у搧琛屽悗闈, 浼氫骇鐢熶竴涓澶栫殑鎬昏琛岋紝 鏄剧ず鎵鏈変骇鍝佺殑鎬诲笺傝繖浜涜灏嗕骇鍝佸垪璁剧疆涓 NULL
  • 涓缁勭粰瀹氬勾浠界殑琛屽悗闈紝浼氫骇鐢熶竴涓澶栫殑鎬昏琛岋紝鏄剧ず鎵鏈夊浗瀹跺拰浜у搧鐨勬诲笺傝繖浜涜灏嗗浗瀹跺拰浜у搧鍒楄缃负  NULL
  • 鏈鍚, 鍦ㄦ墍鏈夊叾瀹冭鍚庨潰锛屼細浜х敓涓涓澶栫殑鎬昏鍒楋紝鏄剧ず鎵鏈夊勾浠姐佸浗瀹跺強浜у搧鐨勬诲笺 杩欎竴琛屽皢骞翠唤銆佸浗瀹跺拰浜у搧鍒楄缃负 NULL

浣跨敤ROLLUP 鏃剁殑鍏跺畠娉ㄦ剰浜嬮」

浠ヤ笅鍚勯」鍒楀嚭浜嗕竴浜MySQL鎵цROLLUP鐨勭壒娈婄姸鎬侊細 

褰撲綘浣跨敤 ROLLUP, 浣犱笉鑳藉悓鏃朵娇鐢 ORDER BY瀛愬彞杩涜缁撴灉鎺掑簭銆傛崲瑷涔嬶紝 ROLLUP ORDER BY 鏄簰鐩告帓鏂ョ殑銆傜劧鑰岋紝浣犱粛鍙互瀵规帓搴忚繘琛屼竴浜涙帶鍒躲傚湪 MySQL涓紝 GROUP BY 鍙互瀵圭粨鏋滆繘琛屾帓搴忥紝鑰屼笖浣犲彲浠ュ湪GROUP BY鍒楄〃鎸囧畾鐨勫垪涓娇鐢ㄦ槑纭殑 ASCDESC鍏抽敭璇嶏紝浠庤屽涓埆鍒楄繘琛屾帓搴忋 (涓嶈濡備綍鎺掑簭琚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繘琛屾楠屻

12.10.3.聽鍏锋湁闅愬惈瀛楁鐨凣ROUP BY

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銆 鍘熷鍙傝冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆