找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 1996|回复: 0
打印 上一主题 下一主题

mysql常用函数大全

[复制链接]
跳转到指定楼层
楼主
发表于 2018-3-21 16:07:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1、字符串函数# e: Z3 h, Z; |  l
ascii(str)   
) s4 o9 o! k) h* a3 A3 E返回字符串str的第一个字符的ascii值(str是空串时返回0)  
0 Y* a- o) A* M8 }/ xmysql> select ascii('2');  % ^' |  O/ O$ N1 c  Q& _+ W: V
  -> 50  
! P3 Q$ B$ A1 N& w% M+ Ymysql> select ascii(2);  
1 Y( J& O. i* d4 Y4 M1 C: |  -> 50  : |' O1 P. }' d, L( F1 d
mysql> select ascii('dete');  4 u- H. \' ^$ W' T: c+ H7 A
  -> 100

& O2 ^; r7 _5 |ord(str)   
- B2 [: S- a$ C1 U/ q0 Q如果字符串str句首是单字节返回与ascii()函数返回的相同值。
5 Z; `8 x* w  ?1 m3 d" g
" }! w9 [. Y( f* Q4 H4 _如果是一个多字节字符,以格式返回((first byte ascii code)*256+(second byte ascii code))[*256+third byte asciicode...]  # ~! R! b8 `+ `# z# |
mysql> select ord('2');  
! q+ y% {, F! }- ]# c  -> 50  
7 ]2 m1 |0 K8 g6 c/ |' N   
" T, F2 }! ]. S/ W0 ^- H) U, {7 }conv(n,from_base,to_base)   
, k7 e8 o% u! O7 ~  o8 T' A% X对数字n进制转换,并转换为字串返回(任何参数为null时返回null,进制范围为2-36进制,当to_base是负数时n作为有符号数否则作无符号数,conv以64位点精度工作)    _: A4 X: s; G: S+ v$ Z! V
mysql> select conv("a",16,2);  ) d9 M& t5 N, S5 o4 V
  -> '1010'
1 {9 {& X; R1 e! g2 e8 Vmysql> select conv("6e",18,8);    D* q% J6 n8 e
  -> '172' 2 m/ @- u- F% z8 w& \/ V
mysql> select conv(-17,10,-18);  * @! t( p) s+ X; G. L/ O
  -> '-h'
2 q0 K* `+ I- F9 I+ @% Kmysql> select conv(10+"10"+'10'+0xa,10,10);  + d/ d) Q+ p; g$ N3 d! W7 i: h$ `
  -> '40' + f9 y% z# z( y4 g- q2 q7 P/ A/ F
   
! O4 t/ q  o& w. `8 Rbin(n)   
$ i( v6 d6 H1 ]3 g3 D把n转为二进制值并以字串返回(n是bigint数字,等价于conv(n,10,2))  ( q0 \3 v7 E; J/ h6 o8 k- K
mysql> select bin(12);  0 T, O4 I  |9 Y! z
  -> '1100' 0 q8 E" X$ Z4 o' E' U) M
% d  Y! ?* U" f/ p2 t
oct(n)   & M) z* m; y$ C, k6 o
把n转为八进制值并以字串返回(n是bigint数字,等价于conv(n,10,8))  9 f% o# U- @7 l' p& U, ~6 x, W
mysql> select oct(12);  
8 W% U+ |: o! m8 y2 z. u  -> '14'
5 u6 w  A' S0 m' F3 P8 R0 C0 w6 b   
% x: B" i9 N: Z4 S; R# U- vhex(n)   
& ^, t7 T& m- p把n转为十六进制并以字串返回(n是bigint数字,等价于conv(n,10,16))  + n& u5 }* r2 K* i: j" p
mysql> select hex(255);  
  C  A# I% c; t7 i3 Q' ?/ I9 m  -> 'ff' 0 W) b& \! G. [4 e# h( G
   $ z7 }2 b/ v: _/ g4 D
char(n,...)   
3 h/ ?: r% t& y" h) _; |返回由参数n,...对应的ascii代码字符组成的一个字串(参数是n,...是数字序列,null值被跳过)   
3 V+ C) j% w% M* ~mysql> select char(77,121,83,81,'76');  
4 M, b$ {$ ]  n  -> 'mysql' 0 d6 J- m' K6 U9 R, y+ r
mysql> select char(77,77.3,'77.3');  
' p5 X4 \, F: L4 f# z3 F6 m  -> 'mmm' + B6 S. N: V" `/ l. b
   
. K' H; K# Y. A7 e. w' C$ W  aconcat(str1,str2,...)  
8 p. o: M; |# g% c* J7 j把参数连成一个长字符串并返回(任何参数是null时返回null)  
6 z% t! q9 F: x* Wmysql> select concat('my', 's', 'ql');  # u  Y0 t- A# O5 U' o  w
  -> 'mysql' . h8 w! Q$ g& D+ u; C
mysql> select concat('my', null, 'ql');  2 G; r( ~7 n. N. ^5 H# w" U$ P
  -> null
* b$ d1 Q* b* s8 W: |( Omysql> select concat(14.3);  - C7 B+ q0 t1 T2 w$ g
  -> '14.3'
2 s& H! Z. y3 u3 q  h 9 x! z7 z2 X( B/ g
length(str)   * D$ G4 X6 n& _0 b# x& _  n: \* O
octet_length(str)  0 ?6 u) O9 r  H/ l! U9 d) J  F
char_length(str)  
3 P% Y$ v6 I  Z) H& vcharacter_length(str)  7 s* K( X7 _0 N
返回字符串str的长度(对于多字节字符char_length仅计算一次)* ?3 g7 K3 N: U% v* Q
mysql> select length('text');  
# a! n8 J- o+ A( x8 E- q9 q. o  -> 4  9 B. ^7 Q5 N, _2 m! P
mysql> select octet_length('text');  
# C9 X' N0 X2 D* }) |, K; y. M  -> 4  $ s- A% f- t1 d1 c+ c
/ Y1 X# @6 s+ K( e! ~% {% K3 c+ ~
locate(substr,str)   
- H1 r9 b8 f+ e& \, P' Hposition(substr in str)   & h# f  U& f$ U" E. ^; M
返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  1 K# |' g. A) `2 D( c- K7 u* f
mysql> select locate('bar', 'foobarbar');  
0 W( v  {4 U, C: c2 I6 n- t* x  -> 4  
0 `/ \: h9 U  Q0 j3 ~mysql> select locate('xbar', 'foobar');  
+ x- R" F* s1 x& I1 z( j( m4 e% D1 t! z  -> 0  # M& o: v5 C0 E) a/ J  M- T  }
   
  ~1 _' k. e/ C: _* [: `locate(substr,str,pos)
# J2 }* Q5 T1 s  V, e返回字符串substr在字符串str的第pos个位置起第一次出现的位置(str不包含substr时返回0)  
# q2 v* G4 P% zmysql> select locate('bar', 'foobarbar',5);  
0 a. e  Z+ f; Q5 M  -> 7  
& ^2 D, d* ~+ Z/ l! C8 ]' B% Y / G, P; O* [) B1 m/ p
instr(str,substr)   * e( I* l! |6 m% `* H  w
返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  
" N+ |" m) s. }, O4 Y, [+ n2 @mysql> select instr('foobarbar', 'bar');  ) I4 k' {* M# K( X
  -> 4  
& c- |, _: L/ d0 }mysql> select instr('xbar', 'foobar');  . I5 e9 O2 }' w5 R
  -> 0   9 u% s1 C+ H- a5 _/ s) ^

% g  F1 f8 s- M: C9 h  @lpad(str,len,padstr)   " n2 @' m- B( d6 v* k8 v1 J
用字符串padstr填补str左端直到字串长度为len并返回  ' T1 s/ v) O% U4 y: m
mysql> select lpad('hi',4,'??');  * }, f7 ~; U2 C$ h0 K  i. K
  -> '??hi'
* {0 s; P8 j% i9 V, [( k   
! s4 w; r( k* t4 E  P/ b# I, X+ Qrpad(str,len,padstr)   . {' r4 _, g2 ]( c
用字符串padstr填补str右端直到字串长度为len并返回  , w  K$ ~3 S- E4 M$ f1 j- \( ^8 Z' H
mysql> select rpad('hi',5,'?');  & o; l3 g% W  U! s" L
  -> 'hi???' ! r' s( V0 d7 N0 R
; V2 H# K. t8 ?. v
left(str,len)   # X/ k7 q/ Q8 s' B# q! k
返回字符串str的左端len个字符  " t! O$ g% R: I! |# [
mysql> select left('foobarbar', 5);  7 n1 {3 y4 d' [+ F4 _
  -> 'fooba'
' l( }' R6 [+ k% `4 ?
- V6 @2 T; L9 k: H6 f% Rright(str,len)   7 N3 L# ?+ E: \/ V2 B
返回字符串str的右端len个字符   . J5 n; t5 R+ V3 z
mysql> select right('foobarbar', 4);  8 x9 k% I/ C4 z5 Y+ X/ a
  -> 'rbar'
. J: g+ W) J. J. x; l/ M0 ?
5 L0 x% l+ C7 Z# h: f/ W8 Bsubstring(str,pos,len)   ) f9 ^4 B! M% X4 R4 Y
substring(str from pos for len)   * ?  \) F) ~8 H2 r4 y4 d' I0 g
mid(str,pos,len)   2 y/ q+ E& M: V9 J% V. L2 y$ w
返回字符串str的位置pos起len个字符mysql> select substring('quadratically',5,6);  
" I1 V2 w- P' a3 u3 d0 b  -> 'ratica' # Z% q0 h" r) J( s

+ g6 E& T/ e% i4 k1 Wsubstring(str,pos)   & s' F" Y9 U& T4 H3 I1 A! |) a) m
substring(str from pos)   - @7 q  s* p! l7 {6 V6 [2 A
返回字符串str的位置pos起的一个子串  
, g1 C/ |9 O+ j) Y5 Dmysql> select substring('quadratically',5);  
' _( _2 k! W- U+ @) ]* B7 c  -> 'ratically'
( f( [2 T2 @+ [; g  X, K  Q6 j$ Z; tmysql> select substring('foobarbar' from 4);  5 {4 V' w* i# F! s+ h2 K
  -> 'barbar' 1 Y) w8 S7 P: q4 B4 P% H% G

3 R* x  L( [! m# Ksubstring_index(str,delim,count)   / {2 {0 F3 Q4 P# }7 D
返回从字符串str的第count个出现的分隔符delim之后的子串7 _* ]9 J# D0 j9 {
(count为正数时返回左端,否则返回右端子串)  1 b) N. L, Z( t2 f0 I% `% \
mysql> select substring_index('www.mysql.com', '.', 2);  $ O: \" G0 J+ {1 C1 o4 I' {
  -> 'www.mysql' . }9 F9 U2 R& Q2 j
mysql> select substring_index('www.mysql.com', '.', -2);  5 a( q& o2 j. F5 y0 z1 o& l
  -> 'mysql.com' , Y% Z' s5 S' ]: v( d

! E0 _  x; M" o- tltrim(str)   
# L( c7 c- l# L返回删除了左空格的字符串str  ; ]% k( F6 B2 t' N) M8 h) v
mysql> select ltrim('  barbar');  
! X# W5 ]% `4 p  -> 'barbar' 4 S* J: Q4 N, Q5 U; t  k

, [  w' v+ a# A' `/ Xrtrim(str)   2 p, x  ?3 I! G0 h) B# x
返回删除了右空格的字符串str  5 H$ m4 k; b6 m0 @. p6 _7 ~& h  s
mysql> select rtrim('barbar   ');  ! \' u$ z. }. y2 [3 L4 [
  -> 'barbar' : A2 Z5 e6 P3 A/ \

, Y; I: D( c, B7 G5 }$ Ftrim([[both | leading | trailing] [remstr] from] str)   . X4 w2 P' D7 O
返回前缀或后缀remstr被删除了的字符串str(位置参数默认both,remstr默认值为空格)  
: h8 Q, q' W9 p! ]% K4 l2 O4 Mmysql> select trim('  bar   ');  
: z1 U% O) C. H2 Q& L# k/ Q! I  -> 'bar'
$ }5 [% V& X0 x6 z5 U# |; Imysql> select trim(leading 'x' from 'xxxbarxxx');  ' V5 P; U9 r  R
  -> 'barxxx'
4 k1 B4 p+ z& C; Emysql> select trim(both 'x' from 'xxxbarxxx');  - O5 U; N" B$ W- u  ]/ B* W
  -> 'bar'
; Z" D& G" M* a9 G% r+ T- Mmysql> select trim(trailing 'xyz' from 'barxxyz');  
9 I. m. f$ d- R+ e  -> 'barx'
3 X) h! H( |: g7 h- h 9 o2 T8 ~; [# o( y% [. {9 n# \4 ]
soundex(str)   . F7 e0 Q; i/ q
返回str的一个同音字符串(听起来“大致相同”字符串有相同的
2 k4 @: Z4 e% f4 H6 t$ K8 D' B! h同音字符串,非数字字母字符被忽略,在a-z外的字母被当作元音)  - M5 {' I; T/ x8 Y0 U
mysql> select soundex('hello');  2 \2 ^9 S" H$ \! @7 t0 n5 }
  -> 'h400' , m9 b0 `' d+ Q' e& [. m  }3 y
mysql> select soundex('quadratically');  
; E4 e9 H5 a7 n3 c8 P0 F  -> 'q36324'
8 e: G: ]; z) T& w   & m# l- ^: O1 @! H3 k' {0 P; x
space(n)   - e3 ~1 F  Y  i; d
返回由n个空格字符组成的一个字符串  7 g1 V$ h  W( s9 x
mysql> select space(6);  
" }; D* ~, t6 `( D! U3 w  -> '      '
" W' l9 f. L5 i% j3 s/ Q   
/ ~! ?1 G( b6 Y* ureplace(str,from_str,to_str)   
6 [  i9 C1 K- w+ ^) S9 x- c& M用字符串to_str替换字符串str中的子串from_str并返回  
  I9 L4 a2 `( i* v) j$ xmysql> select replace('www.mysql.com', 'w', 'ww');  ( n9 X5 j5 ]  w$ y, {" H
  -> 'wwwwww.mysql.com' * @, ~9 P1 @5 R

6 E, I2 q' Y& E7 Z7 h# }5 E/ Irepeat(str,count)  
" t& [3 o" O+ g$ y3 x+ w返回由count个字符串str连成的一个字符串(任何参数为null时& a; V; w2 T1 V5 \- _
返回null,count<=0时返回一个空字符串)  & I$ J4 ^( a& G9 \
mysql> select repeat('mysql', 3);  
$ B7 V1 T3 U5 \  -> 'mysqlmysqlmysql' : x- @# j" i6 }1 E
   
2 x: N" M( ], H, `& U' [/ L( O. areverse(str)   
: c# c% |4 u* s颠倒字符串str的字符顺序并返回  
7 Y5 ^" Q7 o& |# D2 _8 H6 xmysql> select reverse('abc');  
( N# d9 _9 d; q; q  -> 'cba'
+ d6 o' O8 v' j$ n3 t
: r: V8 s3 B- j. minsert(str,pos,len,newstr)   
7 _  b: p7 R( a3 _把字符串str由位置pos起len个字符长的子串替换为字符串
9 G: A- c0 s! x- u* {newstr并返回  
6 ]5 {) z/ u3 @" [6 G3 Amysql> select insert('quadratic', 3, 4, 'what');  5 ]5 ~, P  o& r3 B5 D
  -> 'quwhattic'
6 e+ m1 l2 b" V1 W! R8 A% R 6 A" r$ {( @' z) c. t4 R* m+ K
elt(n,str1,str2,str3,...)   6 P6 N( V* u  t# Y# b, C2 `* H( i
返回第n个字符串(n小于1或大于参数个数返回null)  
) w( z) Q2 W4 L$ |- A$ x- y5 cmysql> select elt(1, 'ej', 'heja', 'hej', 'foo');  
7 S+ L/ t5 a# M  `  -> 'ej'
  A: u2 j# n! c0 {mysql> select elt(4, 'ej', 'heja', 'hej', 'foo');  7 \  @) a) {9 K( h& U* \- i
  -> 'foo' ( I# ~) p" C! B8 ?2 a6 @
  O# O2 {7 R! y
field(str,str1,str2,str3,...)  
" }& k- {. w# J* n返回str等于其后的第n个字符串的序号(如果str没找到返回0)  # }2 y, |" G2 w+ D' u3 m
mysql> select field('ej', 'hej', 'ej', 'heja', 'hej',
' D3 o7 H9 G+ w' }4 `) H( c/ O2 c'foo');  
) H# u  @/ g8 v" R7 J, h% p  -> 2  
! T' u) A6 X' ]$ v! ?& T; Gmysql> select field('fo', 'hej', 'ej', 'heja', 'hej',
- J* [$ Z9 S: L'foo');  
- c6 V! t) l' @0 @+ x: C  -> 0  & b/ N+ \5 T6 f+ U" J

+ H( S+ G& Q. f- {find_in_set(str,strlist)     N+ N4 z1 N2 U' z
返回str在字符串集strlist中的序号(任何参数是null则返回
8 q  p- }" |; W9 m  B5 Q$ snull,如果str没找到返回0,参数1包含","时工作异常)    v  \" F+ h% H/ y& `* S
mysql> select find_in_set('b','a,b,c,d');  
: O2 }/ q8 W! R& H- q8 r2 x  -> 2  
- P4 [: s# U0 J1 `" K     V# w0 q& O; c- f% U- \" i3 Y
make_set(bits,str1,str2,...)  - s. Q9 _" s- g6 b: w
把参数1的数字转为二进制,假如某个位置的二进制位等于1,对应; j2 s2 Y$ K: X, [6 r: ^6 e
位置的字串选入字串集并返回(null串不添加到结果中)  - X' I! N4 u4 D. e4 `) V
mysql> select make_set(1,'a','b','c');  
6 s3 Y7 R; u5 L/ l  -> 'a'
& g* @- i3 b( {mysql> select make_set(1 | 4,'hello','nice','world');  
8 Q) N% Y9 G+ Z, L9 S  -> 'hello,world' " O: y0 d  U& j* Y
mysql> select make_set(0,'a','b','c');  ) ?' i! {2 T" E! f4 M% D5 y
  -> ''
7 j' T/ O1 ]! x) b - A8 f0 C; A' `" r0 C9 J
export_set(bits,on,off,[separator,[number_of_bits]])   
1 e- m, d+ e9 {; v" N6 M4 o按bits排列字符串集,只有当位等于1时插入字串on,否则插入
; @. M. k; W* A- `* E8 `2 U, `off(separator默认值",",number_of_bits参数使用时长度不足补02 h5 _  C- c# w& S- a
而过长截断)   
. f1 }8 P7 z9 A' W5 f* amysql> select export_set(5,'y','n',',',4)  . v$ b" x. j2 P+ W- v
  -> y,n,y,n   & A) M" g( a: R) F9 T
; F* e# ?; U; i+ C
lcase(str)  ! z) z7 h. ~: ^
lower(str)   & g  N, x) s1 p4 @8 x" q7 X
返回小写的字符串str  
* H; _& ]% u* h+ y! `/ k+ b/ bmysql> select lcase('quadratically');  
  r7 m- W( B! L9 Z: j  -> 'quadratically'
* |" k8 y8 B- S, s3 |7 P   
6 T6 Q6 x# J. {! Q5 N! sucase(str)   
& H7 o( P: K, F8 pupper(str)   # X& f8 p! J, V) t
返回大写的字符串str  
8 N  Z+ Z5 I9 ]' m% k# o3 Fmysql> select ucase('quadratically');  
9 Q) R- r: b4 [. M+ q( j  -> 'quadratically'
! V% l1 Q/ h; _' Y3 a   m/ k% _  w3 y2 S% G2 x
load_file(file_name)   7 {1 Q2 S0 i$ S$ S9 j  L
读入文件并且作为一个字符串返回文件内容(文件无法找到,路径
* q2 r0 Q) {4 L+ K' }3 ]2 R不完整,没有权限,长度大于max_allowed_packet会返回null)  ; l$ ]. z* M7 h4 ^! f0 w" T
mysql> update table_name set blob_column=load_file3 t! Y9 t) j0 L
("/tmp/picture") where id=1;  
! \1 H5 R% f1 ~- E4 [* U- k1 ? % a; A4 c# `$ l: y
2、数学函数
# w8 |: ?: h/ [8 Nabs(n)
8 R8 T5 r, V4 H返回n的绝对值  
5 @* j- q0 @, Z7 F: V4 _: fmysql> select abs(2);    ! ~5 b0 l6 U* d3 \2 J  K5 x6 `
  -> 2    6 q4 y1 \4 W; H
mysql> select abs(-32);   
" Y$ `- k5 m5 g  -> 32   
# X$ q& k7 Y/ O: F6 O   6 L3 e1 s, ^  g' z# W
sign(n)  7 f* n- Z; c! r* O
返回参数的符号(为-1、0或1)  
& [: F# A2 K/ Y, ^) s( Q6 e& bmysql> select sign(-32);   
" D/ ?' L/ M' f! x1 i7 N  -> -1   
7 R" o  P+ c5 K$ A9 a* kmysql> select sign(0);    " j, I- Y/ z" x+ b" `: F& [+ E
  -> 0   
* L4 ~  }/ T* u3 u& m( jmysql> select sign(234);    % v* C! {, k. C) q6 G3 i
  -> 1    5 `% `! x" A1 S* _

1 T1 g" t, g" G' Z7 M2 Y: nmod(n,m)   
9 L2 l# H( X% F( g, V取模运算,返回n被m除的余数(同%操作符)   
% E  I: T! x$ C. {$ r0 p- l7 Tmysql> select mod(234, 10);   
# a3 g0 a- G/ ~  -> 4   
1 Z5 q! u' b1 x3 gmysql> select 234 % 10;    ! s! }1 E$ g! V9 e# x( c
  -> 4    ) I! p+ i  h# B. F, w0 g0 P& ~$ F
mysql> select mod(29,9);    . D: f( L  F- B+ R3 w$ [
  -> 2    # m" I. U/ W3 q3 o
! r4 i4 N8 {; n$ b" O1 d2 h! S7 |
floor(n)  6 d$ x8 H% s: H# C6 W
返回不大于n的最大整数值  8 }1 ^, b4 v3 S. ?' f
mysql> select floor(1.23);    , N# w0 C& B8 y% _) n
  -> 1   
; T8 t5 g1 m3 V& xmysql> select floor(-1.23);   
; e( f. I3 D# j  -> -2   
3 ?7 h" Z3 f, v8 u5 w, L& `' a
) h# s1 @9 s8 Jceiling(n)  
" @, o0 Q& q3 l. }7 _5 [* |返回不小于n的最小整数值  
6 W4 h3 C7 X3 }: J  h8 l  F1 [mysql> select ceiling(1.23);   
; c; s' H7 J  K- _  -> 2    3 x% V$ U9 U1 ?: T: T" x
mysql> select ceiling(-1.23);    % N: s1 R. T* I! W
  -> -1    + z6 B' {8 A9 {& G! u+ J3 R

+ p- d; V3 R/ E1 oround(n,d)  
0 f$ m3 G0 h6 V* i5 l% d1 q1 r返回n的四舍五入值,保留d位小数(d的默认值为0)  , T( K$ @% l% s9 ~, Q
mysql> select round(-1.23);    + k2 q6 x/ T( `# a5 e* l( L
  -> -1   
& a, p# \  c, |mysql> select round(-1.58);    1 U$ E5 v! \3 _
  -> -2    + v9 C. R" G3 m9 V0 d: L/ ]2 g
mysql> select round(1.58);    4 [& L* Z* F0 r- Q5 Y; Z
  -> 2    + I+ e4 a+ v' y5 X
mysql> select round(1.298, 1);   
& R* S4 g/ I7 d5 v: h/ ?  -> 1.3    * L8 \9 @2 K5 o4 g
mysql> select round(1.298, 0);    + }. F8 }0 T: ~; F
  -> 1   
" r7 u$ L, Q$ U. h ! I8 N6 d4 ^7 i9 M$ t3 M4 P
exp(n)  
- r. g3 m$ k& {# f返回值e的n次方(自然对数的底)  / u8 S0 {5 n5 M
mysql> select exp(2);   
4 t( ^+ l5 V! l) i  -> 7.389056    ) c0 [# a* m0 I5 g9 `( x% I2 O+ v
mysql> select exp(-2);    ! e* D5 \* B3 W7 ]
  -> 0.135335   
( u) X# ~; P0 i' M; r; F ) S, d7 Q  B6 D3 J
log(n)  1 ^# e; {( X/ M8 _
返回n的自然对数  
# y9 q( F2 J$ `& V$ d) Vmysql> select log(2);   
+ X( s, k. _; j5 ~. A  -> 0.693147   
  F5 w/ S* z$ m) t/ Y# N( n/ V, r; fmysql> select log(-2);   
0 q9 j- w3 N$ l4 ~& \3 b& w  -> null    1 c1 K  W: f' m2 [

" U6 g+ g5 @' P1 T$ s+ @; g' Mlog10(n)  
1 Z4 l* y& U2 ]返回n以10为底的对数  
# l( D( S1 q2 P6 P1 I; ~mysql> select log10(2);    ! P3 d- V5 J, L: c' [
  -> 0.301030   
# b. D: r2 Y' T  }9 l- fmysql> select log10(100);    8 M: U' _$ V9 r  [" j
  -> 2.000000   
3 ^4 A+ _9 w; B/ J' V' Q" Emysql> select log10(-100);   
$ W" d4 q) \: F5 [' i' d  -> null   
" {" u6 q* |+ @7 W( {" F
0 e6 M* X+ V1 Q2 R$ n" O* ~pow(x,y)    & ?( `" z" E- ?0 w+ S
power(x,y)    / G; q" s. H# [: x( ^$ H
 返回值x的y次幂  1 D0 k0 Y/ O) O6 N6 D! w
mysql> select pow(2,2);    " _% X- l/ B3 @% |% e2 s
  -> 4.000000   
0 t9 ^$ W# q7 Z  U# k, Cmysql> select pow(2,-2);    9 j: c% |; I( j- s: A
  -> 0.250000  
" R1 m- `, B) I/ [" c2 e8 Y
7 ~% I5 Y7 \  |6 N, k# m1 a7 osqrt(n)  ) y9 B7 [) \/ V, ]0 Y, E* r2 B
 返回非负数n的平方根  
. C7 N# T, y( k: kmysql> select sqrt(4);    . p7 w; V1 K. K6 U. S
  -> 2.000000    1 u0 f, D& D3 T
mysql> select sqrt(20);    & I: Y" \$ Q! B. D
  -> 4.472136   
8 p8 r2 ]# e1 v6 r5 k6 E5 Y* E  l# t + e) `' ~2 s- }# P+ y0 ~
pi()   
  M- Y& k7 I) E2 y; e 返回圆周率   2 U% Y$ i. ~9 C9 r  |5 [- x# \
mysql> select pi();   
  J) Z. y0 ^" i& a; `2 P# X4 h1 D7 O  -> 3.141593   
4 x; m; V3 m' v- o9 ~& s2 g ) _! e" P' O6 l
cos(n)  2 C+ C8 S. p1 H
 返回n的余弦值  ; z" o; {& `. ]0 L8 k
mysql> select cos(pi());  : c- o1 q$ T8 U" x8 G6 Z
  -> -1.000000   
7 w% Y4 m. {) S& U. u  I
8 y/ j9 h: p3 J2 x+ Asin(n)  / L  H) p2 O% M5 T, U" P% n
 返回n的正弦值   
2 n! Y" u& e$ B! B) R- vmysql> select sin(pi());    7 t  N- t; s# g# V
  -> 0.000000      t' m' }; ], w

- E# f% l+ K) }) S; s. k1 ntan(n)  
7 T3 D( a2 I0 O' w4 \返回n的正切值  # P- d% w( A5 X9 @/ i/ G6 P' \
mysql> select tan(pi()+1);    2 m2 h( ^/ w9 e5 \8 H) _
  -> 1.557408    " ?. l* N: R4 {) V0 [9 ]& O7 e" ?

/ t9 z9 _& d9 D' A" Q+ h, |3 Cacos(n)  
/ w0 ?# z2 r6 V3 }: X 返回n反余弦(n是余弦值,在-1到1的范围,否则返回null)  
2 Q& k; m/ c' @" Imysql> select acos(1);   
; ]( W$ ^. Z- f# }7 y8 D  -> 0.000000   
/ v  v. t$ J) w0 E: m- smysql> select acos(1.0001);   
  Y. ~* X, a8 Q' q6 S% T4 V  -> null   
2 O- k( }+ }6 Mmysql> select acos(0);   
% v' F' ?( J6 R5 @: _" L  -> 1.570796   
8 E8 y  `$ S; t
9 E5 M8 I6 y$ r2 i7 E2 D9 |/ vasin(n)  ' v9 R1 R) ^9 G* s
返回n反正弦值  % ]% V' J/ X* s& H$ c$ o" |
mysql> select asin(0.2);    & r8 t: W% r7 B8 ]) `
  -> 0.201358      _! w' h9 u1 R! n
mysql> select asin('foo');   
2 R, [& [2 `8 Z7 a6 @+ i; p  -> 0.000000   
! h" I7 O  K5 H1 @9 @0 A' [3 Z
. b! p, r' D$ d) ^atan(n)  
1 h; Z0 U; X9 E& q2 e7 N返回n的反正切值  
. m* H& H; q" lmysql> select atan(2);    4 r7 C5 }' i" X! y
  -> 1.107149    / {1 A. g9 q9 J2 P7 V4 m/ X6 P, R
mysql> select atan(-2);    8 l( S: g1 g1 |
  -> -1.107149   
$ p( b0 Y: k: S+ N( ratan2(x,y)    7 Y. s& k! g0 O
 返回2个变量x和y的反正切(类似y/x的反正切,符号决定象限)  
6 @8 ~1 F( G( b$ v8 t: rmysql> select atan(-2,2);    4 _. E& A, T$ ^  w/ m; L
  -> -0.785398   
( |! Y$ N$ k: L- W, hmysql> select atan(pi(),0);   
1 n; Y6 e" r7 ]6 p* O! Y6 D  -> 1.570796    9 [( L0 ]. \0 E; y6 `( M# l) a

# G% H* c2 M1 Rcot(n)  
. t" j0 Y6 {( F返回x的余切  
) q0 D0 W( u& o# rmysql> select cot(12);   
: G9 g+ P8 ^8 ^6 l  |% u  -> -1.57267341    ; G$ X0 \" W  s, V
mysql> select cot(0);   
' `6 D6 [1 C6 x1 Z. U  -> null   
" B9 p: z+ P% N4 G* G  L. G : I) ]% R2 ]8 n8 O  Z
rand()  
) D- \- ]" G0 o" f) W0 A% frand(n)    + Q6 B  y/ |& b6 ]  r" J( t
返回在范围0到1.0内的随机浮点值(可以使用数字n作为初始值)
" Z3 S: f, h/ l% z+ {/ t
9 u8 c/ D( @( H% y. S$ kmysql> select rand();   
* }$ D% Z8 H, [- R2 g  -> 0.5925    7 A+ L7 o0 C, w
mysql> select rand(20);    # H' ~, P( B/ z8 ^; }+ |- t3 L$ H( r
  -> 0.1811   
* c1 x& M  s; q+ y8 rmysql> select rand(20);    ! J+ S' S2 C1 j. T+ O+ I* E
  -> 0.1811   
5 s0 U' r" h9 d1 e+ c/ lmysql> select rand();   
! O8 G  l1 v3 p1 i$ M( S  -> 0.2079    * J9 j$ Q9 y% i
mysql> select rand();   
* Q/ l0 D7 @5 F& P# f  -> 0.7888   
7 M3 p" x8 l: e, H$ x7 a* U
- E3 C2 }# g* O+ G( pdegrees(n)  ) i4 Q5 C% _, B, [
把n从弧度变换为角度并返回  8 G4 e( J; m. ^
mysql> select degrees(pi());    # \3 U: `. |( e6 r7 W* ]0 T5 t
  -> 180.000000    . i" ?  g" W% d: n+ t8 r' |

, |+ S' b- y0 F# G1 U5 [! ~radians(n) % F8 ~( _6 S) D* v8 ^
把n从角度变换为弧度并返回   
- n( @  s% A, o6 F0 D3 k  v: Emysql> select radians(90);   
1 r# b- P$ n, l, v0 U, P  -> 1.570796   
* l4 @- t6 a0 c& {: F* L) }( ?: R/ k) G2 W5 I+ L) @
truncate(n,d)   
7 n9 c& \7 \; m保留数字n的d位小数并返回  ' K4 a% Y7 J* G6 n8 r; N
mysql> select truncate(1.223,1);      l: A* v  P+ X+ @1 K" J) P2 \
  -> 1.2    ' H* W* C9 u: n0 L! \
mysql> select truncate(1.999,1);    # m1 L4 B9 q3 j. i+ T% G
  -> 1.9    5 J, B6 R3 P! S
mysql> select truncate(1.999,0);    4 z/ ]# ]8 `( S' \2 Q, f) W, \
  -> 1   
% t2 n2 N* w6 s5 c; L + [+ K6 Y) \1 w+ J6 x; q& o
least(x,y,...)   
3 J: {/ N$ b0 d8 B/ ?返回最小值(如果返回值被用在整数(实数或大小敏感字串)上下文或所有参数都是整数(实数或大小敏感字串)则他们作为整数(实数或大小敏感字串)比较,否则按忽略大小写的字符串被比较)  6 {' i5 c  K3 E) l3 K
mysql> select least(2,0);    6 n: D* X3 y9 `" Q3 T# T
  -> 0   
1 C4 z. k" z- r; Imysql> select least(34.0,3.0,5.0,767.0);    8 x& S" B$ s1 |! l6 P& y
  -> 3.0   
9 o! }! w1 t" C2 t, @* [1 ]+ Lmysql> select least("b","a","c");    ' o- w8 p& h. t/ X) Q4 @7 U
  -> "a"    ( _+ m: d, w) g0 B* E

! z6 K$ x  u- `( {) j- qgreatest(x,y,...)   
* S3 f1 s9 j7 ]' Z, }返回最大值(其余同least())  
! f) `1 j) h  W: L& U2 x# G' Cmysql> select greatest(2,0);    ! p% h0 w% Y. i
  -> 2    8 ~% R; Y$ Q: Y, K& p4 Y# _+ d
mysql> select greatest(34.0,3.0,5.0,767.0);   
# H6 N% K  W) d+ c8 o# M  -> 767.0    ( s( m4 U/ A+ Y, ?: ^: ]( I0 D2 K- \
mysql> select greatest("b","a","c");   
$ p" Y- k& G( U  -> "c"     
0 V  p; Y. Y3 Q0 q* Q
8 S+ J. H' I/ {. I3、时期时间函数 & L2 b& [$ ^' F' c2 z1 m! @
dayofweek(date)   
/ g  O  _; `% u7 n9 K返回日期date是星期几(1=星期天,2=星期一,……7=星期六,odbc标准)  0 y8 j4 ]+ q; H6 P
mysql> select dayofweek('1998-02-03');   
9 F+ E/ i- d4 j4 X0 _: B: z; L  -> 3   
( ?3 s( d8 F6 l7 h% p5 |
6 S& Z8 b( B  h: wweekday(date)    8 N4 }! i9 K+ p0 z
返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。
% t  F6 x, ]. D8 `; @! D- K  
3 P% ~+ j. S2 h% P9 c0 O8 hmysql> select weekday('1997-10-04 22:23:00');   
, X. D/ U* c/ l  i  -> 5    - }. Z) k! }1 B$ D  y
mysql> select weekday('1997-11-05');   
( C$ s  z/ H9 {  -> 2   
( A& S% F: a: H# |3 ]
5 ^* L  u2 _; x, K# z1 r# C  ddayofmonth(date)    
6 k. d- R% V! C% ~, ?% h6 z7 @% K返回date是一月中的第几日(在1到31范围内)   
( H0 p# \0 Q5 M$ xmysql> select dayofmonth('1998-02-03');   
( O( I( \' Z$ ?* G  -> 3    $ O1 c8 F9 Z6 U

# {! M2 _6 c6 j4 Idayofyear(date)    ' w+ `7 D* U% F3 k2 U0 |* z
返回date是一年中的第几日(在1到366范围内)   
% e7 }0 g. I9 m- |mysql> select dayofyear('1998-02-03');    ! w8 N$ k" m7 n/ u- Y# Z. t; b6 d
  -> 34    . T8 M- V6 h& z, |/ N9 B

; R; ^  {$ ]" x+ y3 Hmonth(date)    6 k* K+ d7 |+ F( g7 N- i" L
返回date中的月份数值    & r6 I6 j2 B% A" r$ q
mysql> select month('1998-02-03');   
4 W. h+ k6 q  ^  -> 2   
8 p: W* {; i8 l) K! l7 i% T1 K" T: q " `/ Q3 b* d. P, V
dayname(date)   
. z( f" W& K, @  X: R1 c返回date是星期几(按英文名返回)  
9 s: L4 f! l  _+ Mmysql> select dayname("1998-02-05");    5 v  P5 l) Z% h4 \  j, r
  -> 'thursday'   
% S( I# H2 _& _4 `+ x2 z" T0 L
; D% U" j9 A- `monthname(date)    : f6 j  j: L0 O
返回date是几月(按英文名返回)  
4 O/ k% `% r  p( {mysql> select monthname("1998-02-05");   
* G9 X4 ]$ {7 C  J  -> 'february'   
# H* g# K( _2 R' B$ m. e 2 R: s0 a. s- u( V& Z* Y; ]! Y; k
quarter(date)   
  N/ ?2 [( ]7 y返回date是一年的第几个季度   
0 d5 n4 G. t# D- ?7 P7 s: Dmysql> select quarter('98-04-01');   
( p3 f: ^9 B  ?6 k8 `# i) J* O  -> 2   
0 O* }( ?9 a, V! w- j; J" E : Y) u  q6 O# W/ N7 p3 G
week(date,first)   5 |& I; q' ^1 H1 W. R
返回date是一年的第几周(first默认值0,first取值1表示周一是
2 l* Q7 f, Y9 t5 }& c( x周的开始,0从周日开始)  
+ L, R3 i4 E- U+ I% Rmysql> select week('1998-02-20');   
) P& _; Q& z! ]5 g* \& }2 ]  -> 7   
2 x% s8 Q/ m8 `# Rmysql> select week('1998-02-20',0);    6 G% H! q/ a2 m: F/ c( ?) {
  -> 7    6 e- H* G0 @8 H
mysql> select week('1998-02-20',1);    1 ^1 \2 |1 L7 \7 Z( @
  -> 8   
, @: |9 k8 H9 s  Z3 K, x ; q0 J' D* O+ M/ B# e
year(date)   
4 e. U) i4 |, ~8 e) V( S9 I$ l# v返回date的年份(范围在1000到9999)    ! ?+ t" ^# w$ g' v) G. w4 S
mysql> select year('98-02-03');   
6 Y5 T6 n9 I! u5 h  -> 1998    9 R3 `, n# d- G  V; U2 ?. P6 w

( K8 T! @9 B. |  v  ehour(time)    . i; A) X7 Q9 E# x& f
返回time的小时数(范围是0到23)   
/ _  j4 z& A& z$ Rmysql> select hour('10:05:03');    : d9 w1 d: L8 u6 m
  -> 10    / e0 x+ E/ Y' o0 Y2 u- h5 h

+ D9 l6 ^" M. ~5 ?3 W  R3 P  Sminute(time)    / e; p7 x# g1 A0 s3 N' j& Q
返回time的分钟数(范围是0到59)   
& T9 e- j5 F0 t1 }) R; `* s1 T# Bmysql> select minute('98-02-03 10:05:03');   
& C& L( w* x1 |+ o: Y. A  -> 5   
( {! V! \; I6 z& r8 s* b' z2 P  i1 K! i 8 [" b% D) K0 @* W. i0 I* a
second(time)    / k% ]# U3 ?' ]% Q, ?
返回time的秒数(范围是0到59)   
1 d8 `) r! E% ^  _) c! F  Y& t4 qmysql> select second('10:05:03');    ' W+ S$ j* c) h- e/ t/ f
  -> 3    + ?7 N8 P( Q# `0 C/ P

9 I; Y  e: G5 u4 I& Aperiod_add(p,n)   
, Q1 ]/ X, [3 E5 I3 D, U) O9 s增加n个月到时期p并返回(p的格式yymm或yyyymm)   
( ?$ U) ]4 G8 r4 W+ fmysql> select period_add(9801,2);   
3 S+ `+ G( j6 p/ ?; w1 C% u  -> 199803   
: c5 N- F9 E) z$ C
- v& y0 x' i. ?/ ]0 r% I; h" v( yperiod_diff(p1,p2)   
! e) y. I0 L7 U1 U* d% Z- W返回在时期p1和p2之间月数(p1和p2的格式yymm或yyyymm)  
0 Q5 _! a6 @9 }9 |3 y: tmysql> select period_diff(9802,199703);   
1 Z% I$ j" y8 s& k9 V  -> 11   
& Z2 S0 }5 c( R/ Y ( x7 \1 u. ?7 w, r) X4 N6 ~  m% A
date_add(date,interval expr type)  
6 i7 N  U3 k7 J6 p3 N/ ndate_sub(date,interval expr type)    ' W( a* v" g% i7 Q1 `8 n
adddate(date,interval expr type)    % e; D; i- s/ }8 ~% L
subdate(date,interval expr type)  . g2 V; }* O7 M' d8 i
对日期时间进行加减法运算  6 W0 m: b' {* G# Q* P+ Y
(adddate()和subdate()是date_add()和date_sub()的同义词,也
: f/ i$ g# m* A0 R# c+ W! n可以用运算符+和-而不是函数  " M, F- F3 X% Y) z
date是一个datetime或date值,expr对date进行加减法的一个表
; Q$ J- B3 z% K: t/ X( y1 Q* [4 y达式字符串type指明表达式expr应该如何被解释  ; q# P. T% n" `8 ^
 [type值 含义 期望的expr格式]:  ! a: {. C3 t: W: N; j3 M
 second 秒 seconds    + L0 i9 t3 \* D$ M  U
 minute 分钟 minutes    ! n9 U6 I5 d+ S! Q0 x( v
 hour 时间 hours   
. w3 N7 x* f9 L day 天 days    9 G2 @0 ]. i5 g  J( c7 V4 X: F
 month 月 months   
/ M3 t" |/ @8 \& w year 年 years   
7 |! Q  F6 ?; {/ h' b/ z# L minute_second 分钟和秒 "minutes:seconds"   
  \! Q! Q4 c7 H( ^/ ] hour_minute 小时和分钟 "hours:minutes"   
3 l3 r# _; d0 z, x' V: _" x. U8 O day_hour 天和小时 "days hours"   
# n/ {* R% r( j! i/ @; Y year_month 年和月 "years-months"   
' {; g: u: q0 n" A hour_second 小时, 分钟, "hours:minutes:seconds"    - D, ^" }: _. E0 H9 X" H( X0 {! E
 day_minute 天, 小时, 分钟 "days hours:minutes"   
: m3 Z7 j$ A3 {8 E* } day_second 天, 小时, 分钟, 秒 "days" q/ l! E) d" c7 t) B1 ^
hours:minutes:seconds" & e) f, K, t  L! }. |
 expr中允许任何标点做分隔符,如果所有是date值时结果是一个5 H9 c* b$ j( X- _2 q/ m5 W
date值,否则结果是一个datetime值)  
4 n! S. v9 x* R5 c/ T  B$ U 如果type关键词不完整,则mysql从右端取值,day_second因为缺& m/ X$ u: E! ]% ?7 G$ f4 o4 Y
少小时分钟等于minute_second)  - F9 E3 o0 j+ `  l$ F
 如果增加month、year_month或year,天数大于结果月份的最大天
- F% q2 }; u5 s* t0 l' d0 n# }数则使用最大天数)   
. B8 q8 @& i" E( N: J: tmysql> select "1997-12-31 23:59:59" + interval 1 second;    p/ X" b5 r# B: q; B" A5 B

4 x6 q" C  L+ `0 C& C" d' B# Z  -> 1998-01-01 00:00:00   
# @% B) Z% ?2 a" ?3 l. ~mysql> select interval 1 day + "1997-12-31";    ; [7 N! M! Y4 _- p
  -> 1998-01-01   
4 D, H8 r# {! M$ I2 imysql> select "1998-01-01" - interval 1 second;   
* K9 ^3 f# V; Y  -> 1997-12-31 23:59:59   
5 V* O# ^1 _, [6 nmysql> select date_add("1997-12-31 23:59:59",interval 1
) d, }; }9 d0 @second);    8 \6 P9 P) R' H( x7 z: k6 t' c
  -> 1998-01-01 00:00:00   
0 b& p! i( Y) w+ [0 k8 ?) xmysql> select date_add("1997-12-31 23:59:59",interval 1
# L) j6 E; t7 d& g$ Z2 S) s* B# |& ~! uday);   
: d; j0 m. k+ A  e, \8 w% @  -> 1998-01-01 23:59:59    & x; G3 l3 _3 Z3 B" Y
mysql> select date_add("1997-12-31 23:59:59",interval
- F& f) l6 B* Z* h"1:1" minute_second);   
& f9 \: _7 s8 R: h8 C3 l" p  -> 1998-01-01 00:01:00    ; c. j% ]3 x/ W8 u, l
mysql> select date_sub("1998-01-01 00:00:00",interval "15 s5 r6 V9 a- w2 S1 L
1:1:1" day_second);   
. T$ p. g9 e$ H( f: `2 `$ v  -> 1997-12-30 22:58:59   
0 n. v7 p! r; R+ imysql> select date_add("1998-01-01 00:00:00", interval "-1" Y+ a5 |8 m$ J3 d% \
10" day_hour);  
# w, N; a4 I+ L: o" l# O  -> 1997-12-30 14:00:00   
. _2 `' H1 S  h0 [" Cmysql> select date_sub("1998-01-02", interval 31 day);   
3 J2 }9 v9 N7 V3 }# Z  -> 1997-12-02    % e' m# r/ p. k9 p! q, w
mysql> select extract(year from "1999-07-02");   
' _3 ^+ s. L2 A' b: y  -> 1999   
: z# p" g3 y6 }/ A) D0 Dmysql> select extract(year_month from "1999-07-02
8 ]; }% t7 w7 x" d$ m, s01:02:03");    1 x& V* F7 _; ~
  -> 199907   
& _! t" n" ]' W7 H2 mmysql> select extract(day_minute from "1999-07-02
  }6 m2 [) d3 K$ B& |9 X01:02:03");    2 u( |2 @% W+ i: y* R
  -> 20102   
7 P) z; R4 F; C4 t) }6 O 3 r1 }* j8 r$ M( ]( y
to_days(date)   
6 K" {* D* }9 J8 Y2 ?返回日期date是西元0年至今多少天(不计算1582年以前)  7 ~' }+ d8 k  o  R  ]4 l; Z. J
mysql> select to_days(950501);    8 A  i. F  k7 F
  -> 728779   
( Q4 p" p& Z- V- e0 z, \% Rmysql> select to_days('1997-10-07');    / }- P! b. J( U( ]
  -> 729669   
7 m$ D* W8 m6 Z0 V8 }
) K4 e. \' m- F) e7 u! c7 [from_days(n)      [3 G2 L1 J9 k( K
 给出西元0年至今多少天返回date值(不计算1582年以前)   " z7 [+ G1 {% Q2 d
mysql> select from_days(729669);    , \5 n0 [- c8 G0 |9 g
  -> '1997-10-07'   
6 m# y, ~/ T+ g+ ?: \/ i/ c: d
. |' o( z9 Y; Q8 T1 Qdate_format(date,format)    7 h7 o8 L+ G1 K7 z7 c. J
 根据format字符串格式化date值  3 h/ ]% i7 k, i5 m
 (在format字符串中可用标志符:  # }, z. E2 E- {* `: E0 ~
 %m 月名字(january……december)    # Z1 s& o9 Q, Y& f' e$ V* F
 %w 星期名字(sunday……saturday)   
( h" p9 W. o0 _- M* j %d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)   
+ X$ I" o5 s/ t" }9 F %y 年, 数字, 4 位    # Z& G7 e/ l  t+ T! [
 %y 年, 数字, 2 位    - v$ Z  U- k( p, [6 A
 %a 缩写的星期名字(sun……sat)    * g: j; @6 Q8 M" w* u
 %d 月份中的天数, 数字(00……31)   
7 j+ X# \. j& k: N" v8 W) m) @# p %e 月份中的天数, 数字(0……31)    , E: Z% C& Z6 l; x# E: q6 B! d/ G
 %m 月, 数字(01……12)    + T% g  b" u9 A+ x& P
 %c 月, 数字(1……12)    , w6 |; G+ X' a4 c( e- p4 I! W
 %b 缩写的月份名字(jan……dec)    # I3 \: Y  T7 g! T( S5 {
 %j 一年中的天数(001……366)   
7 ^, s0 g+ E) j" p- u4 ]* ^5 n %h 小时(00……23)    & H3 I- p) ^: R& H& L5 h
 %k 小时(0……23)    ' f/ t3 f4 z9 c. H( T' x$ O
 %h 小时(01……12)    % n  \) i; d! F" X6 w: @( T6 b
 %i 小时(01……12)   
$ C& B' }% ]/ K$ ^3 C5 l %l 小时(1……12)    : n, u# j1 \2 [. E! O7 o1 m1 t
 %i 分钟, 数字(00……59)    3 I4 ?/ j$ C8 N5 _- R  M5 e
 %r 时间,12 小时(hh:mm:ss [ap]m)    ) g3 H% [- o; W  f+ f9 z
 %t 时间,24 小时(hh:mm:ss)   
4 {) g8 V  k3 e4 J" |0 b6 _% T! F %s 秒(00……59)    7 E0 }0 b- j1 [; e
 %s 秒(00……59)    ) ?: x- F9 G0 K# R1 p7 T6 D
 %p am或pm    ( b: [: q$ W( M% M
 %w 一个星期中的天数(0=sunday ……6=saturday )   
7 B! C$ Q8 _4 q( G3 g/ N %u 星期(0……52), 这里星期天是星期的第一天   
+ I: x6 F9 R, R- Q: j+ M) e  |. T %u 星期(0……52), 这里星期一是星期的第一天   
. |6 b' E1 U6 f) q8 x %% 字符% )  
/ q4 Z0 W0 v$ v1 F, k3 ]mysql> select date_format('1997-10-04 22:23:00','%w %m %# l1 A* q2 z- B! V2 k
y');   
' V0 M9 R* |2 j& S, s1 D  -> 'saturday october 1997'    # B3 f! x( ]( x) b& {3 k" M
mysql> select date_format('1997-10-04 22:23:00','%h:%i:%
, H& b6 n; m. ]6 q8 hs');    " y9 D; ?, S* p' J* v, J1 X
  -> '22:23:00'   
% H+ g, w/ a( v" k' N6 c$ t* D8 bmysql> select date_format('1997-10-04 22:23:00','%d %y %a' w( t* ~1 F! J' w9 H1 k! M
%d %m %b %j');   
) k9 `2 W+ {6 t7 G$ v! [  -> '4th 97 sat 04 10 oct 277'   
9 Z2 P# u- q. a: n2 W- mmysql> select date_format('1997-10-04 22:23:00','%h %k %i  O1 E% Q# r* M/ T
%r %t %s %w');   
: u! U, B3 t# V, c5 y  -> '22 22 10 10:23:00 pm 22:23:00 00 6'    ) ?4 i0 B* `9 t9 W  n
! N3 l" w) Q# [! C5 E0 A4 n
time_format(time,format)  ' j7 z' @6 l/ ]2 q5 X: d7 d$ a; {
 和date_format()类似,但time_format只处理小时、分钟和秒(其
. W3 G) h$ P* u/ D2 d% N6 I6 K余符号产生一个null值或0)  $ j4 G+ N  j( t% D' C- Y' Q* ?
0 d* h% W4 X1 V
curdate()     
( q* D4 z' t3 T4 `0 L' |9 x) Kcurrent_date()  ; x& y+ C" T( o5 G( E$ o, x& l
 以'yyyy-mm-dd'或yyyymmdd格式返回当前日期值(根据返回值所
8 M/ f4 Q0 `( B3 ~9 [处上下文是字符串或数字)   
& `. o) W, A+ H! mmysql> select curdate();    ; I; i( F# W6 z3 F! Z: \
  -> '1997-12-15'    ! G" M5 |, d+ F6 ~
mysql> select curdate() + 0;    / f" j0 r" t) ]
  -> 19971215    ) E1 Y  l1 [+ i1 x- ?
% S7 O& @4 T+ O4 k, w  _$ |) N6 M
curtime()    " ?9 s  i# e  G4 G
current_time()  
) M3 w; |; T' M" i* H0 d 以'hh:mm:ss'或hhmmss格式返回当前时间值(根据返回值所处上
7 C5 D4 U' w9 p+ Q7 T8 }. O4 y: q; u下文是字符串或数字)      
; Q+ V$ L# p. e4 Omysql> select curtime();   
- q1 N2 G, @7 l0 F# Y  \  -> '23:50:26'    : l5 p# S7 ~7 F' O1 X2 X' [; `
mysql> select curtime() + 0;    0 _0 Q" f" f- q9 i
  -> 235026    6 @# B$ t( p- U  T

3 c, `4 z& z0 L3 L# c* Snow()    / h7 s) P+ D3 o. X2 H( X
sysdate()    ) Q  U+ T+ H. O% O3 [9 a" _5 T
current_timestamp()  ! t0 ^- P4 w0 n' x' d
 以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回当前日期# p8 x% G- }/ f! G- |
时间(根据返回值所处上下文是字符串或数字)     
% k6 p3 t4 f  T& ]5 H; x. Tmysql> select now();   
; }6 |# u1 Z" I  G9 R  -> '1997-12-15 23:50:26'    ( A5 M5 D1 K- @* H  c
mysql> select now() + 0;    % {  s, C+ P& I" q7 L0 V
  -> 19971215235026   
" _: Z" f+ r% j- u6 g* d . G$ o$ F. ]  ]" U6 D
unix_timestamp()    2 _$ V: n6 D& l$ d; f
unix_timestamp(date)   
" `- G- T2 V: g返回一个unix时间戳(从'1970-01-01 00:00:00'gmt开始的秒4 f( I2 L% ^. Y6 E% B0 C$ u
数,date默认值为当前时间)  
: |7 }* D3 B+ A# u( V% u) xmysql> select unix_timestamp();   
$ [- e$ h9 b. n! `  n  -> 882226357   
% f4 e; R5 L. t6 imysql> select unix_timestamp('1997-10-04 22:23:00');    3 i; ~. ^4 O0 Y7 s/ b( ~, B7 o: E1 g
  -> 875996580    # v7 k# U& n7 c) V

' \2 ~. ~% U6 e; I& R/ a# mfrom_unixtime(unix_timestamp)   
' k4 ^" f  F  |: n, X以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回时间戳的
8 P1 U( J% h1 W6 R% ]4 \: y值(根据返回值所处上下文是字符串或数字)     
( H1 f; m: e3 h) ]& w, a; [, Wmysql> select from_unixtime(875996580);    8 u$ l( p4 x. G6 @6 y* N1 M
  -> '1997-10-04 22:23:00'   
) v: j6 @5 J& w8 k7 Wmysql> select from_unixtime(875996580) + 0;    ! I0 z) N0 S9 E" ~, D2 X
  -> 19971004222300   
3 O% d& F* m, P& P 6 G+ Z2 W* \! A/ _& u( G; s- H% O
from_unixtime(unix_timestamp,format)    ; E9 s4 J3 n. J# r5 T
以format字符串格式返回时间戳的值  ' G4 V0 ~+ g9 T, o+ W& j, @9 |# T
mysql> select from_unixtime(unix_timestamp(),'%y %d %m %' g4 n+ C) M5 l6 C
h:%i:%s %x');   
! o8 }, N! d1 k/ ^  -> '1997 23rd december 03:43:30 x'   
9 F& U. H9 w! s3 g/ C+ |6 O+ a- U
' V% K' G1 _9 p% Bsec_to_time(seconds)   
7 E8 S4 T7 A5 S2 J5 J# e4 a" @以'hh:mm:ss'或hhmmss格式返回秒数转成的time值(根据返回值所处上下文是字符串或数字)     
' }( m3 @6 r8 w$ q1 emysql> select sec_to_time(2378);   
' u/ ~  }) l2 j  k7 N# ^: n2 B! n  -> '00:39:38'    # Q" M$ d$ f9 ^( [' K5 ], @0 }1 L
mysql> select sec_to_time(2378) + 0;    , H8 N2 ^4 p- @
  -> 3938   
; I1 ]( H- R; m! T" E # T5 M, q; @$ S5 M# O
time_to_sec(time)    2 L& i, E" d  R9 x( Z' O$ ?7 V2 _
返回time值有多少秒   
" J! Y, _5 K2 U% h( [mysql> select time_to_sec('22:23:00');    0 f- m- K8 a7 _! v- r
  -> 80580    3 S9 w$ X7 N7 }) K- T" G6 z
mysql> select time_to_sec('00:39:38');   
) h6 q8 F8 V5 f& L  -> 2378 6 e6 }9 y3 ?% ^9 W3 f$ Y9 H6 I) k5 {' O

# @  _& n8 d% t; b5 G% z转换函数
2 t4 }% R* ], i( b5 r1 U4 Mcast
0 I# C0 z; y+ n6 e9 n0 M" s用法:cast(字段 as 数据类型) [当然是否可以成功转换,还要看数据类型强制转化时注意的问题]
9 l2 e) {3 o$ u% `实例:select cast(a as unsigned) as b from cardserver where order by b desc;
! x8 ^3 K- [; z( cconvert:' u  f' {3 j" n3 G, H
用法:convert(字段,数据类型)
  a7 x& V/ ~/ ?9 D& k2 D0 E实例:select convert(a ,unsigned) as b from cardserver where order by b desc;
$ D' H1 [6 X! ?. }! X3 W
回复

使用道具 举报

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

本版积分规则

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