找回密码
 立即注册
查看: 2127|回复: 0
打印 上一主题 下一主题

mysql常用函数大全

[复制链接]
跳转到指定楼层
楼主
发表于 2018-3-21 16:07:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1、字符串函数
& N* q! G  B3 x: i" \: [, S2 Pascii(str)   & P' f6 v! q' z  T% {* p; n
返回字符串str的第一个字符的ascii值(str是空串时返回0)  + c2 W! v. S0 J& p' T! `; e: }! J
mysql> select ascii('2');  7 z1 Q2 k' O2 A4 p
  -> 50  
. z0 n( Y* C2 }' _5 O5 cmysql> select ascii(2);  
: H; p# }; S) ?# B% |  -> 50  5 u; q6 }5 C; Y% t
mysql> select ascii('dete');  
9 G/ l" D! h% d: ?: {& p- O$ A  -> 100

" C/ E5 K& A2 h3 m# gord(str)   
, h1 I  v% `: X5 p5 E# K如果字符串str句首是单字节返回与ascii()函数返回的相同值。3 X4 g6 r. ^/ {( M: V! T2 q" m
3 y. y. c- a0 a$ O
如果是一个多字节字符,以格式返回((first byte ascii code)*256+(second byte ascii code))[*256+third byte asciicode...]  - Q3 F$ i  t2 m- m, |
mysql> select ord('2');  
& Y! G+ a! _5 C$ }* T/ L8 |, z& e  -> 50  ( i1 W+ k7 s$ w8 @
   - ]+ O/ u; ?' W, I$ F
conv(n,from_base,to_base)   
" A, ?0 K3 x. z3 z" q2 H! _& k/ I3 ^对数字n进制转换,并转换为字串返回(任何参数为null时返回null,进制范围为2-36进制,当to_base是负数时n作为有符号数否则作无符号数,conv以64位点精度工作)  
& h" s7 i+ F& [- gmysql> select conv("a",16,2);  
% i6 K9 r# |* k- y7 I1 j  -> '1010'   N, h' e6 y( D. w, h
mysql> select conv("6e",18,8);  ; W$ j& Z+ x. G5 Y6 h/ v& s
  -> '172' & `/ t: V2 x8 {6 P
mysql> select conv(-17,10,-18);  # o# K2 f& |" f: H+ X4 ?" q/ e  [
  -> '-h' + R3 {7 i0 |( g' J
mysql> select conv(10+"10"+'10'+0xa,10,10);  
* `! G2 d$ r+ r' ~  -> '40' * |) R4 O2 ~5 X+ a% m! ?- v
   0 B( z( Z0 i9 w6 o4 k% r
bin(n)   
7 H% [% J8 _9 A2 K8 C把n转为二进制值并以字串返回(n是bigint数字,等价于conv(n,10,2))  ) i- r0 v( L3 y! \% [3 k
mysql> select bin(12);  
& X; Z3 g5 E2 `6 d" Z  x  -> '1100' ) U' d# p# m0 l( C

* e* {7 S, L6 Y2 `oct(n)   ' M8 M/ H$ P/ e
把n转为八进制值并以字串返回(n是bigint数字,等价于conv(n,10,8))  ; k6 N2 l, ?- I9 u; W8 k3 p5 w! a
mysql> select oct(12);  9 x/ Y; e7 i% t" U4 v  w* ]
  -> '14' ! T6 G- m8 V2 p. B; Z
   ! s* E( l+ ^  p3 s* E( R  S' E
hex(n)   1 j# e% b+ F9 E# C* y
把n转为十六进制并以字串返回(n是bigint数字,等价于conv(n,10,16))  
' B; |% Q! ]$ Lmysql> select hex(255);  
% z7 z# K; ^. d" Y. q0 u  -> 'ff' 7 h( F9 R5 \+ C9 Y- o5 L* g7 w
   , O) e' f- q$ z
char(n,...)   , ~0 y! Z  \0 o  G4 g
返回由参数n,...对应的ascii代码字符组成的一个字串(参数是n,...是数字序列,null值被跳过)   
; A4 N% `( g" `  F" Kmysql> select char(77,121,83,81,'76');  # Y% E) @) P8 X$ v1 ~
  -> 'mysql'
! y4 c( ~, v0 a, \% wmysql> select char(77,77.3,'77.3');  
  i+ n* V3 P# f% B* c  -> 'mmm' 9 q5 k. y( h  q( c
   7 I' T6 S' i7 B
concat(str1,str2,...)   7 b: {' P9 W1 w
把参数连成一个长字符串并返回(任何参数是null时返回null)  7 I9 C* q% G9 }* t
mysql> select concat('my', 's', 'ql');  8 ~% p* l1 G: D" I! ~" D$ {
  -> 'mysql' 8 l  l* p+ e: q. O
mysql> select concat('my', null, 'ql');  
6 N: I6 B- N* i0 E" ?  -> null 9 \0 u) w% D' w6 W/ ~
mysql> select concat(14.3);  . x& D$ O0 J+ k0 S% `/ E
  -> '14.3'
# n# l/ i1 x1 Q; }. p* P
% g" r% \. l3 K5 v% t$ Nlength(str)   ) ]. e. q3 d% ^- b9 v) L9 _
octet_length(str)  
" P5 ]. k- g$ D0 ochar_length(str)  
  J% R$ h1 k, X! k3 Tcharacter_length(str)  7 G8 b; n& g; |% N, X; n  {
返回字符串str的长度(对于多字节字符char_length仅计算一次)/ f2 b$ C" @' H( {( R+ i
mysql> select length('text');  
* |# M+ v8 v$ J, y  -> 4  
7 V* {2 w4 I2 K7 pmysql> select octet_length('text');  
8 T: S/ s, A3 G1 X% A  -> 4  
* q1 \7 p* q9 b' W1 v& M 0 Y$ s0 n" H' M  R; u% o5 {
locate(substr,str)   1 H% _( W. A6 I$ I" r" g9 Y5 J
position(substr in str)   
5 Q2 e) S1 G1 Z/ ~- C# E7 ]返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  
+ B% v+ X8 ?% q! X4 ]* Mmysql> select locate('bar', 'foobarbar');  
6 W8 k) d+ @8 u/ x' l  -> 4  * W7 L; J$ ^  b8 j% E. o4 q# J3 I
mysql> select locate('xbar', 'foobar');    z$ I$ D% e2 B' I. c6 N8 ?
  -> 0  " H8 m- y3 G2 q: F( t0 ~
   
/ P$ B7 T0 j- V, ?locate(substr,str,pos) % v" z2 T4 C! ?  Y, S3 G
返回字符串substr在字符串str的第pos个位置起第一次出现的位置(str不包含substr时返回0)  
+ x3 E) S3 t, a4 z. `+ p# ~( z7 s: Tmysql> select locate('bar', 'foobarbar',5);  
" w* h: B/ K: i+ K/ ]1 G0 _( w  -> 7  " ^# l  `0 F& G- l8 E6 H

: l( q. g" W9 p8 t  ~4 B6 k- @instr(str,substr)  
; U4 Z) p2 h# o/ N* d返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  ) t0 k4 v4 I7 Y5 Q7 X
mysql> select instr('foobarbar', 'bar');  
# h+ X5 q/ _$ I/ w) l$ [! O: \$ q, ?  -> 4  
0 z4 A* |, P# Q9 i: S3 i* A; _mysql> select instr('xbar', 'foobar');  3 y; `4 G7 A7 T" a7 G: p, l
  -> 0   . O. ^: ?6 I) ^0 ?3 c4 H4 I

; {( [3 N( ]+ ~+ K" k$ i; slpad(str,len,padstr)   0 B  Y% z3 U7 F) B# H4 F$ Z" z, O
用字符串padstr填补str左端直到字串长度为len并返回  % o5 m) M  H& B. j; l# |* N# B
mysql> select lpad('hi',4,'??');  
+ h, F( l3 S( O9 G- C  -> '??hi'
% O/ T) e) v! V, i" X+ s# e   
# k7 p. l# f, qrpad(str,len,padstr)   & P; Q) k0 l; j! v
用字符串padstr填补str右端直到字串长度为len并返回  . X8 i& r) ?0 A/ I8 `
mysql> select rpad('hi',5,'?');  
* B2 S- l2 j6 X1 b  -> 'hi???'
4 Y* q3 F5 ]2 [8 P& @0 U 5 \- Q1 ?: O$ O% R8 I$ Y
left(str,len)   
# J1 `& Z3 {" B+ W返回字符串str的左端len个字符  
+ [9 [( R2 X3 @  wmysql> select left('foobarbar', 5);  . |! N0 g% m3 G4 F
  -> 'fooba'
% g! Q7 e$ z+ p4 p: v) E * @; N' E" O# a( n
right(str,len)   2 r2 H: y! c0 H/ ]
返回字符串str的右端len个字符   
. X& F2 d3 k* {! L8 i' ?mysql> select right('foobarbar', 4);  
# z' R& Z" M& w7 _- Q  -> 'rbar' 7 |  m8 F8 D; ^2 O) ]* x5 C' w6 k

0 e, t" v/ S; l  n1 Fsubstring(str,pos,len)   0 {" w9 a: f) @* o! [% J
substring(str from pos for len)   
. [9 k  t( w  g6 t& ]1 P8 V! V- emid(str,pos,len)   % j* ]) u* R: k' B7 D
返回字符串str的位置pos起len个字符mysql> select substring('quadratically',5,6);  # G1 U* r9 q. A0 i& H: R8 n9 S
  -> 'ratica'
" ~! n7 m( o3 F- H! {9 {: B, O* w
; `4 M+ P' x# m, w) Hsubstring(str,pos)   
" n  z3 j8 l9 a0 X3 A9 Q, usubstring(str from pos)   
, K# o5 L' o. [; e" m5 ?) i返回字符串str的位置pos起的一个子串  
* L& c) p. D0 g. Mmysql> select substring('quadratically',5);  7 O0 l6 B# u$ K7 m
  -> 'ratically' " y8 {# o' F2 L; D& m! ?/ r5 B
mysql> select substring('foobarbar' from 4);  
  s# N( m5 M; a2 G" m  -> 'barbar' % I+ W( x5 ]' N* P2 W
3 p% L: w" Y3 a& ^% S6 U
substring_index(str,delim,count)   
6 n) d+ k$ S* R9 Q6 c0 R/ X返回从字符串str的第count个出现的分隔符delim之后的子串
/ b/ K& H+ Y8 X; k9 r0 _/ g(count为正数时返回左端,否则返回右端子串)  
- o* j' J  P4 u1 Y5 Emysql> select substring_index('www.mysql.com', '.', 2);  ' I: \1 C' A3 T& `8 O+ P
  -> 'www.mysql' $ Z1 r* C8 c' x, {/ g; d6 r+ n
mysql> select substring_index('www.mysql.com', '.', -2);  " ~& N" \; [# R( U2 B
  -> 'mysql.com' 7 e3 L3 q# q. F! x
9 H0 {( ?% b- ~5 n& r4 O& G3 G
ltrim(str)   
/ `+ N  S0 e# Y  B. [返回删除了左空格的字符串str  & W4 ]. H2 P0 Z  r% ?7 J
mysql> select ltrim('  barbar');  3 y; {! [) \2 L, z8 a1 r
  -> 'barbar' 0 x' Z$ Q  ^$ F6 M5 f& r' T
7 g# E. h: q8 p" J/ q  y, v: Z+ F
rtrim(str)   
/ E  k: I' N4 v, N" [返回删除了右空格的字符串str  
% \- m  ^, w) z% v- Nmysql> select rtrim('barbar   ');  7 ]9 p! v& t$ i4 a
  -> 'barbar' 0 k. Z4 _) I5 h  m0 k

2 q  n+ w' S3 m$ X/ K$ g8 Htrim([[both | leading | trailing] [remstr] from] str)   + I) ]5 |+ @/ @: P
返回前缀或后缀remstr被删除了的字符串str(位置参数默认both,remstr默认值为空格)  
( U; K5 h6 q+ r0 X; v9 A( [6 _& Wmysql> select trim('  bar   ');  0 n: ~# s  X* Z( E/ b- F
  -> 'bar' ' V1 K) f6 p+ y4 F1 F
mysql> select trim(leading 'x' from 'xxxbarxxx');  
3 V4 ~+ V/ C. e+ d' }7 i  -> 'barxxx' & d3 K+ Q+ u( Q( S4 W; _' d0 Q# D
mysql> select trim(both 'x' from 'xxxbarxxx');  $ d/ f; b* u% h& v/ T
  -> 'bar'
) F! e" O2 L; Y0 q+ Imysql> select trim(trailing 'xyz' from 'barxxyz');  
2 Z( |- u  P% g  K) e  -> 'barx'
) A' V; A/ g6 P: b9 r, Y3 @ # V. R" `) q" _2 z7 p
soundex(str)   1 _/ ?& k+ L* |
返回str的一个同音字符串(听起来“大致相同”字符串有相同的; Q& b! z0 j) p9 L6 w( W
同音字符串,非数字字母字符被忽略,在a-z外的字母被当作元音)  
7 }$ P* m. K  F+ u8 X3 d! U' [" i$ gmysql> select soundex('hello');  " R& O7 Y4 Q( A5 M0 D) c6 n
  -> 'h400'
1 A8 U0 Z) c5 x- N* `1 w) z2 `mysql> select soundex('quadratically');  5 |8 t' J7 {& `5 o. M* m
  -> 'q36324' - V8 j. m( f+ v7 \
   ) M4 J% @- G2 Q
space(n)   ; }3 r$ S  [# g; q8 G
返回由n个空格字符组成的一个字符串  ; v% _/ l$ H2 y+ s
mysql> select space(6);  8 O" C# A0 p3 ^, @
  -> '      ' . i0 W$ a' d7 t2 U/ ?: O; K
   1 G* V+ ]7 [! r+ A' S/ T
replace(str,from_str,to_str)   0 u2 a1 {- X4 A8 A% ]
用字符串to_str替换字符串str中的子串from_str并返回  1 C! n- f5 a/ Y. _  T- A
mysql> select replace('www.mysql.com', 'w', 'ww');  ) m5 h1 H3 a/ w  p8 P2 y
  -> 'wwwwww.mysql.com' ; l' a6 y$ z+ B( u1 {) p" U. p

4 I% ~" D+ k% h; o2 _repeat(str,count)  
# l" I3 W5 m! c. S8 G返回由count个字符串str连成的一个字符串(任何参数为null时
; Y0 d" b) n3 r, k返回null,count<=0时返回一个空字符串)  
& q, s" C$ U% ^& \. Qmysql> select repeat('mysql', 3);  
" s3 ~, z3 Z4 y4 @  -> 'mysqlmysqlmysql'
0 e8 |, y6 L+ ]. X. N   8 |; _0 O* f9 i* p6 T5 B' p6 d
reverse(str)   
% h( X  f$ h4 t/ v/ M( o颠倒字符串str的字符顺序并返回  
# h. n0 b+ J# c1 b& i1 o" R7 p8 mmysql> select reverse('abc');  
  A- ?! l( n( Q7 f  -> 'cba'
5 {) S/ |$ |8 ~( Q# g
1 Z* }+ D" ~; D+ j5 \/ finsert(str,pos,len,newstr)   
) M0 h  W/ j: D把字符串str由位置pos起len个字符长的子串替换为字符串
7 C5 I2 Q% B" X" [1 Unewstr并返回  
+ W6 r2 g; l1 z; g0 m" l# ~mysql> select insert('quadratic', 3, 4, 'what');  $ J- c& T9 W" b7 [. ^& x- ^9 U
  -> 'quwhattic'
; f8 G, g/ O% I. @5 O0 o9 N$ t
: U2 [) g7 a% N9 n2 \$ ielt(n,str1,str2,str3,...)   $ X, c4 h" p8 Z! j
返回第n个字符串(n小于1或大于参数个数返回null)    N8 N1 z  c, J" u$ y8 p6 X
mysql> select elt(1, 'ej', 'heja', 'hej', 'foo');  
! a% [* `( k% P$ [. T, U  -> 'ej'
& S  t2 c# Y9 O, x4 E; o: y. Vmysql> select elt(4, 'ej', 'heja', 'hej', 'foo');  
: L( h- }: `" A  -> 'foo'
: t+ _9 u% m$ K, P: D) S8 f
% R: `8 i+ J6 `, s  d% i7 bfield(str,str1,str2,str3,...)  
& g" M3 @1 U. H: n: F- n- N返回str等于其后的第n个字符串的序号(如果str没找到返回0)  / z7 @# R- [% D/ Z& ]3 k1 K
mysql> select field('ej', 'hej', 'ej', 'heja', 'hej',! u( C8 D% m% c* A
'foo');  
4 U: U! N% I/ L- N! F9 t8 E5 V1 p3 d1 E  -> 2  
* ?) f' e8 n- q4 C; ]& o; R$ ^mysql> select field('fo', 'hej', 'ej', 'heja', 'hej',
7 i, q5 m6 a& t; L2 \1 x8 V'foo');  
  [$ V1 a) E5 Y) t  -> 0  1 s) y  ?% v# s7 S
: {4 R" e* ]1 C: A2 w$ O
find_in_set(str,strlist)   
$ d0 d: Z9 `) q8 [3 A7 V0 z) V返回str在字符串集strlist中的序号(任何参数是null则返回
  ^1 c, g  B- ?4 a: C+ anull,如果str没找到返回0,参数1包含","时工作异常)  
4 T" ~7 [- A) S  Smysql> select find_in_set('b','a,b,c,d');  + W$ T1 `$ [2 u" r+ o/ }7 Y
  -> 2  
+ m2 S* Y5 M$ K: N& e% d   & p7 W& t; H3 D4 {0 u
make_set(bits,str1,str2,...)  ! h4 F& ^" E+ O- R2 \
把参数1的数字转为二进制,假如某个位置的二进制位等于1,对应
! i5 J2 D  J, @: q位置的字串选入字串集并返回(null串不添加到结果中)  8 Y" @* x' l8 o# ?' G" P
mysql> select make_set(1,'a','b','c');  % G' U' w# n' Z3 ]' O( `% R* \
  -> 'a'
3 V3 P' ~$ H  Xmysql> select make_set(1 | 4,'hello','nice','world');  4 i6 J; [9 W" K/ g$ y) W
  -> 'hello,world'
( v6 X& N& V# F0 umysql> select make_set(0,'a','b','c');  3 P% }; }4 l1 N: T& y
  -> '' % `4 L% q  Q% L0 N5 Y. S+ z

" S; h# {% @$ Y5 J9 fexport_set(bits,on,off,[separator,[number_of_bits]])   5 b; e4 D1 `/ x9 a3 [- w' o- H& U
按bits排列字符串集,只有当位等于1时插入字串on,否则插入1 l- T( X) u+ W1 _
off(separator默认值",",number_of_bits参数使用时长度不足补0
1 Q! o) f. G% S* ~# B5 {/ Q而过长截断)   
; `- k2 a- W+ Lmysql> select export_set(5,'y','n',',',4)  - }: S+ }4 M6 F3 ~7 J9 e. f) ^9 A8 Z
  -> y,n,y,n   
5 s) ~( q! I; [5 | 9 _: k0 v% K1 \& W
lcase(str)  ( U5 p" `, E. n# Q: ]
lower(str)   
$ V9 w& v( x4 s9 C5 V* p& R返回小写的字符串str  $ E( J8 v  d0 C: W4 n% d
mysql> select lcase('quadratically');  1 J% G- \! {, `2 U
  -> 'quadratically'
6 @2 Q9 v5 b0 V7 D3 K: v   
& ~1 f/ ^% A3 [2 s$ u! U2 Gucase(str)   7 a( B; ^3 @! C, U* A
upper(str)  
' Y' r. Q+ u/ \8 F( g& ~1 n, a' U返回大写的字符串str  
# a" e. l( a# dmysql> select ucase('quadratically');  2 @+ a9 X  \7 j' T! h( e5 {* F- T
  -> 'quadratically' 4 M$ i- t2 H) {2 V
% l, k+ N" m# A& `. t; I) Z
load_file(file_name)   
( k+ C; G: a& F9 d+ j; M9 t读入文件并且作为一个字符串返回文件内容(文件无法找到,路径7 W) \9 l% o4 Q( _0 b
不完整,没有权限,长度大于max_allowed_packet会返回null)  ' U! J2 \, N  Y" {* P  j
mysql> update table_name set blob_column=load_file
2 Y; N+ }, s( R$ B% c5 u("/tmp/picture") where id=1;  
6 z0 e1 W8 o/ e
+ o. a3 z2 |- K  H2、数学函数+ k4 x5 m( z7 r" W5 F! C! L" D- U
abs(n) # ~+ s+ S& @  T* Q" z  A2 }7 F# q
返回n的绝对值  8 k+ K3 Y* }* V+ J. i
mysql> select abs(2);    4 D$ D% d0 t" ^
  -> 2    4 |" s( F3 N  Z+ [
mysql> select abs(-32);   
% E( m" N8 y( U$ A0 T3 V! {  -> 32    9 U/ D9 L8 l0 [# A8 }# k" G# S) e
   3 y3 _8 v9 j# `; G
sign(n)  
& G$ g) a. E: B返回参数的符号(为-1、0或1)  7 S6 Y! ]4 n/ p2 v# G' S7 X
mysql> select sign(-32);   
2 N  @/ U3 {0 K3 R8 h; R  -> -1   
% `8 _7 H- ~. |  m. \7 B: j3 z% ymysql> select sign(0);   
# @6 p) s/ P% V: n% `4 _% ^  -> 0    # G# z/ t1 D5 M
mysql> select sign(234);   
7 B# w$ X2 B- W9 |5 f. n: J  -> 1   
* H3 P8 x8 o; j* t0 X9 T8 o( {
2 h) y% Y2 m0 n& {; q; R- F4 _mod(n,m)   
4 g( G, {  [! M5 K$ w8 ~- J取模运算,返回n被m除的余数(同%操作符)    * `+ b5 ?7 h- o) m% N" k) `
mysql> select mod(234, 10);   
  D& P" b% X- `. L! k7 d  -> 4   
! G& m/ T$ x$ U( R0 b3 P, Qmysql> select 234 % 10;    0 `* J/ [$ `  p0 y
  -> 4   
$ i4 m, u4 S# D& R2 N) hmysql> select mod(29,9);   
3 @5 G# i5 y' R4 M  -> 2    0 z' l$ w+ O8 P8 z

7 ^( b% J0 p4 X2 tfloor(n)  / B, q; ~; u* ~8 S- f
返回不大于n的最大整数值  7 u' \2 H9 k) L: e. p" C
mysql> select floor(1.23);   
, [' ?% a. f3 [1 s  -> 1    9 F" h6 T3 v( \7 I5 T( K, y
mysql> select floor(-1.23);    - u8 d- ]: E5 Q- c! Z: [5 g" l
  -> -2   
& P% t( Z. l, l! V3 { : `3 J" Q! A" h* z
ceiling(n)  / N. c4 n3 O9 H, S0 T* F
返回不小于n的最小整数值  
% \0 F( O% ~" X6 M+ o$ f3 R  ^5 ?% cmysql> select ceiling(1.23);   
+ D+ ^( m! M8 P# |  -> 2    / e9 i7 c3 w, x0 B( }
mysql> select ceiling(-1.23);      j6 e1 J" Y. c; p- W5 M, R  |
  -> -1   
* T2 n9 G% b/ Q# j4 |6 v
/ b; v$ E) k8 ^9 d. u' z7 k/ {round(n,d)  
5 j; w# I5 d' X7 k* P2 f$ V5 H: M返回n的四舍五入值,保留d位小数(d的默认值为0)  ; I$ Z% r) ]2 j0 n6 O. F
mysql> select round(-1.23);   
" l  Q1 y# U3 ?; U8 C2 D  -> -1    & ^$ l+ F+ M" G+ D) V
mysql> select round(-1.58);    ; w8 O/ S$ P) {/ h7 u" N( x
  -> -2    7 O" J1 @4 C# Q8 Z/ W: P; {  j
mysql> select round(1.58);    - _, p4 N+ p- u& l' {
  -> 2   
& t: n3 p0 \2 \' Z' D+ qmysql> select round(1.298, 1);    1 R+ m# y" Z( o0 t0 t
  -> 1.3   
1 J5 j3 C! u2 B; e4 `8 @mysql> select round(1.298, 0);   
: g! m5 _/ W0 A7 j  -> 1    . [2 y! r; G/ ?# \; L! \. h6 X7 ]1 p

; N: F8 A5 a- ^8 y. B) Q# s" Qexp(n)  
) J' M2 S  K' l2 q0 S# y  g返回值e的n次方(自然对数的底)  
5 p$ u4 U- u- Gmysql> select exp(2);    , F- t! C4 m' N3 c( Y% e6 j& U
  -> 7.389056   
+ V( e; \2 i6 O1 z! {" d2 xmysql> select exp(-2);   
0 p4 K5 v$ u+ Q5 \  L9 X  -> 0.135335    % K" |+ j/ l% B# O- q
' W0 m2 Z! f2 L6 x
log(n)  
" T. W) C2 L6 o8 l8 u返回n的自然对数  
" p1 Q/ z2 L( _mysql> select log(2);    6 f5 m" R8 ~: w, q# X
  -> 0.693147   
2 y, \3 d* X- }mysql> select log(-2);   
  o  E4 p+ i, u# }; `4 s  -> null    1 `, {: d. D) n- ?
, n/ Q( [. ~% d
log10(n)  
$ I: M  p. m( Q3 t) p返回n以10为底的对数  6 `  C# f1 p3 {* I: K' A0 o6 J
mysql> select log10(2);   
! e' I: A; r1 [: z& o( O- W2 f  -> 0.301030    $ f" q6 b/ s8 y4 Z% S
mysql> select log10(100);    7 W! Z. y* M1 Q0 [: o  f1 |3 n
  -> 2.000000    ! @) J* y& K# q& F) X8 R
mysql> select log10(-100);   
& a, Y7 A# l; U9 U) X6 J; e4 `4 @  -> null    9 ^) x) `. y/ y" G; E% G( L
0 g& f* i' Z6 H* V
pow(x,y)   
/ t! I$ f6 s9 e  V; J' @8 tpower(x,y)    * \! s/ D! z6 h+ F' |8 c
 返回值x的y次幂  
9 J+ w* ?, u0 Cmysql> select pow(2,2);   
1 Y! \! H& e6 b7 a$ e( z# q  -> 4.000000   
! F# A# O" S  H: Y: g- j; rmysql> select pow(2,-2);    9 }. Q, Y; n+ K2 j7 M( L
  -> 0.250000  
5 L: j/ j/ k# t( o# O
( q/ H1 V. A7 o1 x; ~2 xsqrt(n)  
# f; v0 n' @. G1 P) F 返回非负数n的平方根  
) X+ [! h; F3 ^: c+ @6 i' z2 mmysql> select sqrt(4);    9 D4 G% }& t% c# l8 z1 d
  -> 2.000000   
- m9 b$ \, _% ]& M1 Nmysql> select sqrt(20);    # u8 ^' k  x2 Q
  -> 4.472136   
9 V" _9 F- D7 n7 Z. E5 f' G3 l $ T4 K/ ]5 W5 }: y
pi()   
" ?/ ]$ i7 q3 F1 k2 [ 返回圆周率   ( ~# j3 M* I4 Q. O" j4 O
mysql> select pi();   
4 h. J* `6 ~" [3 A& `  -> 3.141593   
) q, j2 v& T. h# G& I, p6 ]) I* v7 ~8 I
- W: }0 i% e. m+ G/ k# Y* k& n2 kcos(n)  , P. N! w) b: V- n1 s, [
 返回n的余弦值  5 X, c& P8 q8 V* E1 u; r' r
mysql> select cos(pi());  
+ ~! _+ X8 i/ O: s/ R  -> -1.000000   
6 S7 Y5 i2 ^6 }! l+ t
# \' |" P% {5 m! asin(n)  ! Q% t, k. q# a% o( @# m1 P1 D4 R
 返回n的正弦值   + j4 X8 Z) v6 W8 f3 Q( O
mysql> select sin(pi());    3 `2 H1 U3 f3 F- o
  -> 0.000000   
  J( _! L0 k4 w- i2 X0 e 3 N6 |9 v& z# _% f
tan(n)  7 v, a  G' O9 R2 L: Q1 b
返回n的正切值  / _" ^8 ?# y  k  g- U2 s/ T9 G
mysql> select tan(pi()+1);   
8 W, W4 w- K  A! O$ a- A  -> 1.557408    , ^& i' A6 m) t" R% t
$ [+ i8 Y, P: n
acos(n)  
5 h4 ^/ L& a. Y% X! H 返回n反余弦(n是余弦值,在-1到1的范围,否则返回null)  1 ]+ i" e" K# Q/ f, d8 }6 l
mysql> select acos(1);    * H7 u1 m& x5 k- v2 L6 F+ r/ ]2 A
  -> 0.000000   
: X3 I# b. P6 _; J6 }6 vmysql> select acos(1.0001);   
! Y3 x+ G9 @  p, ~  -> null    , M# z' R/ K6 [; A
mysql> select acos(0);   
3 i# Y0 S- |+ A! B8 i4 m* h  -> 1.570796   
8 g0 W5 ?, r6 e: L9 j! ^. Z ( `! E( S9 ~& S& S
asin(n)  2 I4 ]9 f0 ~" F2 s
返回n反正弦值  
. g+ ^+ q+ T, G- S0 h6 s( hmysql> select asin(0.2);   
' }7 E: q* w* g4 q  -> 0.201358   
9 R& p% Y; y9 A. V  Tmysql> select asin('foo');   
2 B8 X: e) A+ x/ N1 e  -> 0.000000    5 D* X% j1 I( C. z

, J. s7 |7 u; y. Z! s6 p( ratan(n)  5 W! r" G9 M' O, ], \
返回n的反正切值  
) g2 Y4 m, B# s6 c2 I: W7 Bmysql> select atan(2);    ; u1 G$ b, P7 `$ N0 A" @) ]
  -> 1.107149   
( H( V- b. _% G$ X" Z; Y$ x1 N0 umysql> select atan(-2);    2 @( \& e& r) l4 A0 K- @
  -> -1.107149   
# E1 l) v9 N+ G( B. ?atan2(x,y)    % E( p3 g# j% ^. c. o, T* p' R, c0 Q
 返回2个变量x和y的反正切(类似y/x的反正切,符号决定象限)  # q7 g' }% K, l
mysql> select atan(-2,2);   
0 B. q* E4 L' r' d3 x  -> -0.785398    - t1 d4 V' B+ g' [  ^
mysql> select atan(pi(),0);    6 Z7 `1 o! C& \* Y
  -> 1.570796    * b0 M9 c5 G9 U6 I: k7 S8 E7 [

+ L$ ?3 u* N- ]8 e- U% f: y% Bcot(n)  - c. g! n) z( l& M2 n. P
返回x的余切  
0 N! M% ?* ]$ h1 S& [* V% U! Kmysql> select cot(12);    0 Y& z, _: A1 ~3 f4 d) b1 H7 M( B) {
  -> -1.57267341    4 x4 q1 n: E+ F
mysql> select cot(0);   
, N1 G: M& i, s; u  -> null   
" U# a  J( [+ t- p5 f" U% D  F 9 Z# ?& m7 `) d$ `+ b+ ^
rand()  % j3 t# |( d' {# T) @2 r" f1 h
rand(n)    $ x' S% z' y% y5 N
返回在范围0到1.0内的随机浮点值(可以使用数字n作为初始值)
/ ~& D9 B* [) q& Y0 i
8 X. l0 y! G& L8 |mysql> select rand();    9 ^, v+ x0 f7 a& O2 L- t1 u
  -> 0.5925   
+ Y2 b* y1 y# _' Z% w& e! cmysql> select rand(20);    : e' M: S) ^' f- H$ P& X
  -> 0.1811   
# k1 O$ m; A9 W) e: r: h  m& [5 V+ S0 U" lmysql> select rand(20);   
( u. Z( r$ i5 |& E  o9 w  p  -> 0.1811      V6 K( I  n6 S# x5 Y
mysql> select rand();   
2 K# A  Z$ p7 d& Z2 e  -> 0.2079   
( n1 m5 U: c- W' E/ d& s3 Qmysql> select rand();    5 U0 o. W9 j# A9 M+ G  @& [8 V! g
  -> 0.7888   
8 c9 R, C5 W; x4 X
6 Y3 [: q& Z4 j$ M% b7 Qdegrees(n)  
: Z, @, h6 L2 F; s/ c把n从弧度变换为角度并返回  
+ B$ f/ Z5 L  H: `# s  h$ |mysql> select degrees(pi());   
6 {9 q8 M0 \* z8 K8 j; `+ W  -> 180.000000   
) X8 U9 y/ H7 d6 {: W1 x
( _. |+ W5 q6 P3 w* h  Iradians(n)
* N5 g6 X3 ?1 V# Z& ~2 Y- X把n从角度变换为弧度并返回   & O' ]+ k" h. i7 `
mysql> select radians(90);    8 T" b- \; L& j( Y! j$ j
  -> 1.570796   
2 y! v: U! n4 Q3 {! Q. Z
# F8 d; T6 u5 E9 e9 A) C; u- r2 Q# ftruncate(n,d)    9 W" B1 f3 P2 a, r- \8 ]% K$ g
保留数字n的d位小数并返回  
8 h( x* m9 S, h4 [. Ymysql> select truncate(1.223,1);   
- n+ w0 a! r& P5 b  -> 1.2    3 l6 M2 p3 Y. m4 g* u: X; u
mysql> select truncate(1.999,1);   
* m: D3 L; W0 I, T  -> 1.9   
' i$ u! ?5 \& B8 s  y4 }mysql> select truncate(1.999,0);    3 {4 Z+ u; a3 N/ i
  -> 1   
5 r* b+ h3 o3 ~9 Y8 m- S" j ; F# c; [, J, v6 {, q4 S" w4 O3 f
least(x,y,...)   
: K& U& {+ T. s5 k8 \' w' K返回最小值(如果返回值被用在整数(实数或大小敏感字串)上下文或所有参数都是整数(实数或大小敏感字串)则他们作为整数(实数或大小敏感字串)比较,否则按忽略大小写的字符串被比较)  
$ x5 b* p7 L3 X4 T' R& A( t* P% }5 Omysql> select least(2,0);   
* U) i% [. u! W8 S. P, G  -> 0    9 e0 O7 d$ C% n0 {8 R0 q" g( j
mysql> select least(34.0,3.0,5.0,767.0);    4 ], d; j/ l3 [5 B! g# c
  -> 3.0    * o" |1 @8 N2 h: }8 m8 G2 ]
mysql> select least("b","a","c");      @; }) p. d, H7 [+ I, t3 I' y: p, i
  -> "a"    ' v7 w& l$ A7 G; O6 s
9 j1 @7 V" m6 }% z+ S7 t; L2 ^
greatest(x,y,...)   
( ]& L# w* q; E8 x4 ?3 d/ t, r6 s返回最大值(其余同least())  
7 s  K: \0 u) smysql> select greatest(2,0);   
: q( N+ ^- J, ^9 L# v/ n+ c  -> 2   
$ L- d4 a. }! z4 d$ @- U# ]' mmysql> select greatest(34.0,3.0,5.0,767.0);   
% R0 o& p* _# g+ w  ?5 \+ C  -> 767.0   
# M0 A+ Y3 k' B) Gmysql> select greatest("b","a","c");   
$ Y. b& d3 F; c- [3 ]0 R! y6 J  -> "c"     , N8 f/ i2 y+ `4 }3 P8 o3 X

+ z, f0 |& e) y4 l; @- v  \3、时期时间函数
  a2 l# E7 r5 a6 A. ?  Tdayofweek(date)    ) G' t! w) d/ @; I7 o
返回日期date是星期几(1=星期天,2=星期一,……7=星期六,odbc标准)  . O, T  q) P2 h* S. D
mysql> select dayofweek('1998-02-03');   
2 j/ N/ H0 I. @3 Q2 D+ J+ S2 z  -> 3   
+ T" S, N+ ^2 k3 D; \
# `, E0 c& Z* t' Xweekday(date)   
$ K0 g/ n: @9 V. T* q7 j返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。 ; ?% P: R& _4 e' T) i9 F
  
; m2 _; l* U" Z3 f& U' amysql> select weekday('1997-10-04 22:23:00');    ' H  f& L/ U! ^
  -> 5    ! _) f$ I% ?" {" S. \
mysql> select weekday('1997-11-05');    4 M( C7 N4 f( \. z7 l2 i; R4 w
  -> 2   
. O" N' ]7 p) X, m/ R$ X, @$ \' W
; @& k, y$ ]' o4 F7 i5 cdayofmonth(date)    
+ D% J* B% B- F* I  e4 @8 w# M返回date是一月中的第几日(在1到31范围内)   
5 ^6 }& ?6 O- N: Kmysql> select dayofmonth('1998-02-03');    7 W# Y1 \  I* d' S* c; c
  -> 3    2 f+ T) N: k# I
5 w0 d: g; v/ z, E
dayofyear(date)    $ C  [/ E7 A: |" e5 h
返回date是一年中的第几日(在1到366范围内)   
$ L; s! ^; }- emysql> select dayofyear('1998-02-03');   
- _* D2 t4 a8 A+ Z' j0 z  -> 34    ) F9 l- S( ~! m1 o( [
/ ?9 x2 L, Y% q1 X/ Q
month(date)    ; ~0 |/ q( D3 P/ p6 ~6 G
返回date中的月份数值   
% }7 r# B" t* M+ n1 B; D  hmysql> select month('1998-02-03');    0 w  X$ b6 C' `5 O
  -> 2   
+ ]( n. R* B, ], ^% a ; W( ~; ^; G- e2 |
dayname(date)   
. P+ o3 u2 S4 h+ @返回date是星期几(按英文名返回)  2 Y" S) ^8 P+ W& l* G3 c! m- r
mysql> select dayname("1998-02-05");    ) k4 N" t6 P, I
  -> 'thursday'   
4 h1 s. g+ }: |1 i! V 5 Z0 v8 s  V! S" \7 B- N. g  |
monthname(date)    
' G4 ~/ R8 @5 A1 {( m返回date是几月(按英文名返回)  
, y9 z7 U8 x7 t7 H3 ]. g2 N1 Wmysql> select monthname("1998-02-05");   
" ?  g& g, t$ v$ U1 W4 ^  -> 'february'   
5 f) L3 V+ ~& W" y4 l + F9 O! t! ~4 u" y0 c  j
quarter(date)   
- G! V- B2 A0 f  Q) o% O3 P, }返回date是一年的第几个季度   
1 @* d' }  Z9 i# Q! D: zmysql> select quarter('98-04-01');    # P( e& H, K6 Y
  -> 2    ; \6 x7 t8 ?* r8 q( p

) i- S8 W  J5 t4 o: Fweek(date,first)   ! F, n/ Y: g/ ^. N1 ~* y  {
返回date是一年的第几周(first默认值0,first取值1表示周一是
# i3 o" w& e# e, i8 `周的开始,0从周日开始)  ' b' C9 w( g" ^1 e  o
mysql> select week('1998-02-20');   
! ?9 }' i0 O9 M- h/ S  -> 7    * z. ]3 W9 N3 T5 l' n
mysql> select week('1998-02-20',0);   
6 q4 c% {# l% U: z# ~  -> 7    ( X4 k; v' N$ {8 [7 k9 J2 h/ c
mysql> select week('1998-02-20',1);    . X# T& y: U( D- }% \
  -> 8   
* ?6 k2 d7 V2 r& ~8 y% h$ s% g
5 A4 f/ ]* `( ?4 w& y) Y! \( Jyear(date)    . U' I# ]6 U0 C9 k( I% l  C
返回date的年份(范围在1000到9999)    1 \# A3 ^( ?5 a) O, o
mysql> select year('98-02-03');   
* B" q8 q+ _1 P* j  -> 1998   
: k5 n3 m; P9 c
" y2 |. Z/ N5 Yhour(time)      h# Y. ^% Y4 {) I" }: X  U
返回time的小时数(范围是0到23)   
( A8 I1 s/ c, p8 Zmysql> select hour('10:05:03');    4 Z' A% \$ D( W1 T6 a
  -> 10    ! W, T+ I' S6 Q3 t$ Z

! ~& z8 S. t; f  H! r/ [# Vminute(time)    , P' ?/ M( e4 N& Y4 E  @6 O$ [/ o
返回time的分钟数(范围是0到59)   
( U  U# |: }& r+ I! n1 Jmysql> select minute('98-02-03 10:05:03');    & v! G3 f% b* _8 P7 c# T
  -> 5   
# M# m$ c# x. l& O + d2 K  @: g  p1 [% }
second(time)    
) f+ h: y5 Z% f; P4 B& h" [返回time的秒数(范围是0到59)   ( v& W' m4 b# M7 W
mysql> select second('10:05:03');    6 D- s) e' h# Q8 |9 |1 P5 s& m
  -> 3    / U5 n4 l  d5 F4 p( J! Z& c. n

- Q3 \" @4 V( E0 h5 _  I1 S4 ]period_add(p,n)   
) D7 u6 r6 ?: b增加n个月到时期p并返回(p的格式yymm或yyyymm)      E2 S  {6 X7 |  Q' |
mysql> select period_add(9801,2);    ) b; V, I) K; e" l0 q- d. P% }
  -> 199803   
' B9 \; a2 s1 y2 w6 b1 V; `4 v6 I + r- y4 [" \6 F( L* x
period_diff(p1,p2)    , I$ M2 |/ v- O* _  K' |
返回在时期p1和p2之间月数(p1和p2的格式yymm或yyyymm)  
1 d; Z8 t- s* nmysql> select period_diff(9802,199703);   
2 k* V  w* n& V. t  -> 11    ) B/ r: R. K# T1 X

6 w: j9 j, W# v: a# B7 u3 p3 ?date_add(date,interval expr type)  
- [3 f! K. C; R* Idate_sub(date,interval expr type)    ! I7 B6 A! B1 x$ g$ D" i' J3 f
adddate(date,interval expr type)    * M5 k+ i) H( a0 J7 i9 G6 d9 D+ }
subdate(date,interval expr type)  
0 U/ {7 ^* y3 Y1 j* r. A对日期时间进行加减法运算  ! P& H# v5 \! L4 m
(adddate()和subdate()是date_add()和date_sub()的同义词,也
* [2 l8 B; p: Y, b" Y0 |' H可以用运算符+和-而不是函数  5 ^; d& b0 b: _# e
date是一个datetime或date值,expr对date进行加减法的一个表, A0 L* x- Q% l2 N) u$ c  N2 B
达式字符串type指明表达式expr应该如何被解释  
: W! C% O6 U5 H! q* B; c* y1 I [type值 含义 期望的expr格式]:  : ~" B/ k1 z! n+ Y
 second 秒 seconds   
/ {  l3 Q* h. V9 n minute 分钟 minutes    $ p  Q  ?( i' m( R3 l7 |4 r1 ]% z
 hour 时间 hours    ' B- c- g' `6 U) {
 day 天 days   
; ?! q) G4 P! t% c, f' k* Q+ E month 月 months   
# r( `+ Q4 l0 R  N9 f year 年 years    $ \6 `* B0 R# R( c5 E' p  {
 minute_second 分钟和秒 "minutes:seconds"    5 Y+ x  E/ K6 c* A; B* ?: A: P* ~. g
 hour_minute 小时和分钟 "hours:minutes"   
. E% d- |- Z- R5 x/ Y/ Y day_hour 天和小时 "days hours"    : y9 e, I& `9 s, X3 _" p$ i# d
 year_month 年和月 "years-months"   
1 Q5 e. V; ?) n9 A hour_second 小时, 分钟, "hours:minutes:seconds"    & ]6 f& }) |) l' Z
 day_minute 天, 小时, 分钟 "days hours:minutes"    ( H" j7 j6 S, [/ l9 q+ o
 day_second 天, 小时, 分钟, 秒 "days% K! {7 q* s& X$ ^/ n* ?
hours:minutes:seconds" ' Z) t! s; }* ^
 expr中允许任何标点做分隔符,如果所有是date值时结果是一个
6 Y  V6 A) d. Z8 n4 r1 M: e4 T0 W$ Wdate值,否则结果是一个datetime值)  0 q3 V. \- l  Q
 如果type关键词不完整,则mysql从右端取值,day_second因为缺) u5 w/ e, U& }% t, M  {! u
少小时分钟等于minute_second)  
0 u8 `  ]; F) r5 B4 X6 a 如果增加month、year_month或year,天数大于结果月份的最大天: [: H* x5 x; Y
数则使用最大天数)   
3 s% g+ b  R' [mysql> select "1997-12-31 23:59:59" + interval 1 second;  
& u0 ^6 \& R* b( ]4 I2 |  Z' I
( i: p/ i" X9 Z3 K/ B2 I  -> 1998-01-01 00:00:00    0 o( {& h% N6 [: I2 \' a
mysql> select interval 1 day + "1997-12-31";    8 a1 b& B+ t$ y6 r( Q& l/ A$ e6 o
  -> 1998-01-01    . k- o0 d8 o# A5 k
mysql> select "1998-01-01" - interval 1 second;    ( V! W7 |5 i; M- X& o( i
  -> 1997-12-31 23:59:59   
5 n3 R( k- P8 z1 S/ ~mysql> select date_add("1997-12-31 23:59:59",interval 1" _+ `1 m+ f# i+ w6 [# [
second);    4 h7 _3 c1 E) K
  -> 1998-01-01 00:00:00   
8 y) E  ]% W' P* `. C# [& Xmysql> select date_add("1997-12-31 23:59:59",interval 1
+ z# l! C  P2 N% [  D4 G' X# Pday);   
( q$ I, j0 ^( d7 ^6 f6 Y8 d) M  -> 1998-01-01 23:59:59    / b. }" s# j- M1 }: L" j
mysql> select date_add("1997-12-31 23:59:59",interval3 I7 S$ D, U+ w, _; f5 X0 }
"1:1" minute_second);   
, A1 z4 @% J& G' }/ l* Q4 {2 I  -> 1998-01-01 00:01:00    9 g/ S$ \: b2 d' I
mysql> select date_sub("1998-01-01 00:00:00",interval "1
  c" l: F, h8 s# C1:1:1" day_second);   
% Z6 U- W5 I- G  -> 1997-12-30 22:58:59   
  {' W- w/ f/ Vmysql> select date_add("1998-01-01 00:00:00", interval "-1" Z" ~; d9 r' @4 r
10" day_hour);  
' s- b# Z# l9 c6 K9 P! K  -> 1997-12-30 14:00:00   
+ O. s7 R* @2 w5 Omysql> select date_sub("1998-01-02", interval 31 day);    2 N2 l6 |5 r) m: Z$ M
  -> 1997-12-02    % `# @/ I( v; q; A$ T; M6 A
mysql> select extract(year from "1999-07-02");   
- i. L5 C) Q7 Z8 Z8 f* T  -> 1999    % X0 X, }3 [0 y! y& s2 d! ?6 d, u
mysql> select extract(year_month from "1999-07-02
; j4 m) ]2 j7 ^) j4 s* p01:02:03");   
8 K/ y! _4 T$ _% K  -> 199907      z5 G: V6 d" Z: ~6 D
mysql> select extract(day_minute from "1999-07-02* \; x' J/ L' E
01:02:03");   
6 ~7 T' `+ P/ w. a: E4 l0 m# N& w  -> 20102    5 |* z. z7 |+ p' @0 J) H  l3 \
0 Q* x5 r' E/ N) N( m
to_days(date)   
5 [) F3 b) b. l# g" w; X返回日期date是西元0年至今多少天(不计算1582年以前)  
  `9 D3 r  `! _3 Lmysql> select to_days(950501);   
# A) u& s1 a8 i  -> 728779   
) I$ ?: g, G9 U' F4 gmysql> select to_days('1997-10-07');      l/ _7 |) x! X
  -> 729669   
1 _* O& S- K( \$ _/ W) U   G# E# _2 U5 q( v: r4 a
from_days(n)   
+ H$ ?( r( T4 J# V7 C 给出西元0年至今多少天返回date值(不计算1582年以前)   
9 y' ~# n7 o  J1 [' [+ g2 amysql> select from_days(729669);   
* z$ Z! o4 T, W' t  -> '1997-10-07'    4 r, C( _6 c& H" i4 M' j+ U6 x

3 [& v$ V  [* X$ H/ ?% tdate_format(date,format)   
( P0 o7 D) s2 C; d# ~' J  n' D 根据format字符串格式化date值  ) h5 k( G7 W" Y8 b" P
 (在format字符串中可用标志符:  
) s, O/ ^( F/ p/ D. @# W %m 月名字(january……december)    0 O3 `) C3 i7 s1 y8 |- V
 %w 星期名字(sunday……saturday)   
) ^4 V2 ^% |; [7 i1 P5 S4 T, Y0 l %d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)   
1 `, X: Z, M5 \+ ^: B %y 年, 数字, 4 位   
/ o2 K( `: F9 u% |7 \ %y 年, 数字, 2 位    " }- {) y; c4 V$ |3 v% I  g
 %a 缩写的星期名字(sun……sat)    $ e; T! s9 x/ @
 %d 月份中的天数, 数字(00……31)      s( ~( \5 C# d% O7 j- |
 %e 月份中的天数, 数字(0……31)   
2 F9 h1 |! t3 e8 z" a2 T4 o( } %m 月, 数字(01……12)    5 V- Q/ }0 T- {" e! J
 %c 月, 数字(1……12)    7 d* l. {6 V* E3 p  j; o4 E& F$ z
 %b 缩写的月份名字(jan……dec)   
/ r/ a+ [: ~; t* Q! `/ u, f) N %j 一年中的天数(001……366)    " S8 s4 w# D8 E) o  B5 {! _, A
 %h 小时(00……23)    & _% e' }, w* Q4 x: e5 Q: j( E
 %k 小时(0……23)   
9 Z- i/ h% R* x/ W: ~' B% | %h 小时(01……12)   
9 s9 p! I/ V/ V! {) l8 `+ K %i 小时(01……12)    + H5 ?8 T4 S2 |% ^6 W' z
 %l 小时(1……12)    1 [  \& f2 G; @% e
 %i 分钟, 数字(00……59)    4 n, U( P5 V5 @' S9 ?/ E/ E% t' ^/ o
 %r 时间,12 小时(hh:mm:ss [ap]m)      c- f2 Z4 R* ^& c
 %t 时间,24 小时(hh:mm:ss)   
. i; L" D7 G3 u% k %s 秒(00……59)    0 {2 |/ w2 W. H9 ?7 R
 %s 秒(00……59)    , D4 a" a0 `" O, [2 d3 }* W; [6 H
 %p am或pm   
, m0 N) Z% ~0 ^0 Y6 V/ f %w 一个星期中的天数(0=sunday ……6=saturday )   
8 A8 e" Y! {( n/ D- Q5 R' v% ~0 G %u 星期(0……52), 这里星期天是星期的第一天    5 {3 V% _7 t1 d8 S7 N
 %u 星期(0……52), 这里星期一是星期的第一天    ) [  o" y( A4 z$ `% l
 %% 字符% )  
. f" Z9 `( i" b1 ~mysql> select date_format('1997-10-04 22:23:00','%w %m %
$ c; r$ G+ h* n/ y% U0 Dy');    & H& l7 c- X3 p# S9 j* d! ]. G
  -> 'saturday october 1997'   
1 ~& m) ?. T; a- c" B( r  B, Tmysql> select date_format('1997-10-04 22:23:00','%h:%i:%
' z0 _; O* A, q% zs');    4 w7 ]( u, ?( i7 ~8 [2 N. K
  -> '22:23:00'   
8 V* @1 S% I; z& y6 |mysql> select date_format('1997-10-04 22:23:00','%d %y %a
" @! j4 w% Y! J' [9 @2 L  t%d %m %b %j');   
. m9 E$ K0 {. i8 D7 u# Q# z  -> '4th 97 sat 04 10 oct 277'   
) ?$ n/ x6 r3 |- [1 Zmysql> select date_format('1997-10-04 22:23:00','%h %k %i
" _9 E& b( k! u7 O2 y%r %t %s %w');   
: v. b) }) z7 H# h2 }  -> '22 22 10 10:23:00 pm 22:23:00 00 6'    , I. ?7 s: l/ }6 S3 g  [* X: K

' b1 o' {3 L% B: j( ltime_format(time,format)  
  @$ u$ \4 B$ I3 l* g% ?( w8 F 和date_format()类似,但time_format只处理小时、分钟和秒(其' a3 f" K# V; j* F: e, `
余符号产生一个null值或0)  6 j. E$ j% P) W: u2 V! O! \% F* z$ Q  F
6 _/ s  F1 \3 \: ^1 H6 g! Q$ h
curdate()     ! T- p( E' v. p- a
current_date()  
$ r6 k% V* [) g/ }0 J& E6 K+ T* ~6 R 以'yyyy-mm-dd'或yyyymmdd格式返回当前日期值(根据返回值所
' e9 |; S1 s! T处上下文是字符串或数字)   
, @% @1 a8 }; L7 qmysql> select curdate();    * c" j" W- ]  C; P  y
  -> '1997-12-15'    1 [0 D! L* b. G) l1 a% l1 K
mysql> select curdate() + 0;   
! j0 r) {3 h; |; z4 Z: {' c  -> 19971215   
1 F# r3 Z1 p" N7 G/ J2 i9 s" |6 b
2 M7 O$ E8 r+ L8 ^  y$ u; E8 Icurtime()    5 H) z9 m( V9 v1 j# P& a4 c1 }3 Z1 [% Q
current_time()  ; A4 c8 d9 f7 C' A$ W" v, h# P
 以'hh:mm:ss'或hhmmss格式返回当前时间值(根据返回值所处上' K# j$ |$ M. ^/ e- c( R0 J0 f
下文是字符串或数字)      . j& E0 ?) {$ X3 ^4 i
mysql> select curtime();   
' B* X- R" B0 u$ r' z' \7 s  -> '23:50:26'    8 K# _; Y# T2 X# }. A9 ]: D
mysql> select curtime() + 0;   
1 {3 a8 U& u+ d" f  a  -> 235026    1 T6 _7 M* J! k! s+ B- b+ Z3 o; b5 Y

$ n( l; [& i6 A3 c' Snow()    
; y4 b( A" s3 m2 i1 T) J' g) fsysdate()    2 Q0 m, K. c& Y  D' D
current_timestamp()  
  ^% B. B" W8 a7 t, p 以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回当前日期4 v" Z* y( I" v; e
时间(根据返回值所处上下文是字符串或数字)     
* M6 T$ d( n8 }4 W- rmysql> select now();   
! h" @9 X" c/ R+ W) e  -> '1997-12-15 23:50:26'    3 Y" z0 O: q% E" G3 N% {: D
mysql> select now() + 0;    " x& n9 ]; j6 a" s: ?3 _
  -> 19971215235026   
% H, f& L( c- J! L* S
' E8 C! h  [$ \# |unix_timestamp()   
1 k4 F) o+ {# m, f! \unix_timestamp(date)   
6 b+ M" v5 K0 E* W4 G返回一个unix时间戳(从'1970-01-01 00:00:00'gmt开始的秒
/ M: y+ U3 A5 F7 E数,date默认值为当前时间)  % m5 ^% T& r& `2 v4 T0 O* v; N
mysql> select unix_timestamp();   
; M! C! J# H1 B  -> 882226357   
" I" k* o! }/ L0 [8 H- Hmysql> select unix_timestamp('1997-10-04 22:23:00');   
, k, ~* G7 m( d7 B  -> 875996580   
4 ^0 k+ Z! V8 d& W$ ?7 p3 t" a* @ $ @7 G4 _9 I$ B, G
from_unixtime(unix_timestamp)    9 w% z4 x: g" l0 f8 @
以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回时间戳的
3 m4 s; z8 m! L8 w值(根据返回值所处上下文是字符串或数字)     9 C; ~9 R8 C1 E+ g: Z- C4 D/ e
mysql> select from_unixtime(875996580);   
  _) P0 \1 ^- _  -> '1997-10-04 22:23:00'    4 X. q* i1 y, K& P. R
mysql> select from_unixtime(875996580) + 0;    6 Q2 j: i  Q8 h. z) H7 [
  -> 19971004222300    . F8 g  ~( L9 L2 o
1 s0 l# E1 ^* N
from_unixtime(unix_timestamp,format)    
) ~. W5 p. E! U( u以format字符串格式返回时间戳的值  8 a- G1 Q9 D* J& Z# r0 J
mysql> select from_unixtime(unix_timestamp(),'%y %d %m %7 i$ r- j  W! P; j) G
h:%i:%s %x');    0 Q) [# G, m" H9 j8 o2 w6 ]  T
  -> '1997 23rd december 03:43:30 x'    0 I0 Q! P0 Y* p4 s9 R& M/ ?& u

/ c$ h. l  b: zsec_to_time(seconds)    & Y: n3 w- ?  j4 K( W" e
以'hh:mm:ss'或hhmmss格式返回秒数转成的time值(根据返回值所处上下文是字符串或数字)     , D6 c* q8 q+ P7 `3 u
mysql> select sec_to_time(2378);    : m& k$ d, a; W) f6 z0 ?
  -> '00:39:38'    0 `( Z% f: b6 v1 G' y
mysql> select sec_to_time(2378) + 0;    , _# M; @1 @( [7 s' B5 o0 P" Y
  -> 3938   
' `" |4 [8 r; t! C8 c 8 t* V* O& R- d6 k% L0 [
time_to_sec(time)    : t) A1 L) u8 Z. @# y2 L6 U' @7 c
返回time值有多少秒   
  W7 M8 ]! Q: V' Z! U+ h! @mysql> select time_to_sec('22:23:00');    ( t( p  R( o9 I/ Q
  -> 80580    . R: s5 i2 B( o" v, l0 c
mysql> select time_to_sec('00:39:38');   
6 w; r7 k6 C! Q8 L. B  -> 2378
0 g- {9 `& n, R/ J7 ? 6 A: |: X" \. P- Z% k# g) _1 _, d5 @& i
转换函数* _- |  ^4 O* H2 j: c4 v( z
cast
$ K5 k. u3 a3 w: K; n2 r  I用法:cast(字段 as 数据类型) [当然是否可以成功转换,还要看数据类型强制转化时注意的问题]1 D, E' O6 R" k- N
实例:select cast(a as unsigned) as b from cardserver where order by b desc;
5 n/ z1 i" h0 l& Z: t" y; Tconvert:
3 p! [/ l; m4 j" z用法:convert(字段,数据类型)  u. F# G; X# H; J9 Q
实例:select convert(a ,unsigned) as b from cardserver where order by b desc;

9 M! o; b2 j+ g9 h9 H
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表