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

mysql常用函数大全

[复制链接]
跳转到指定楼层
楼主
发表于 2018-3-21 16:07:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1、字符串函数1 u6 E5 ]8 t2 B" U3 I7 o
ascii(str)   
2 t7 o% W5 F% f$ F返回字符串str的第一个字符的ascii值(str是空串时返回0)  . y+ K+ ^/ g' N' ?! W  j$ h0 J
mysql> select ascii('2');  ! |0 ]2 O0 e4 Z
  -> 50  
- r, `, q; C. k$ d/ Kmysql> select ascii(2);  
' F! d& a, K7 g8 L: y5 r+ h3 X  -> 50  " q, A/ k+ N+ s
mysql> select ascii('dete');    d# }7 W& F; [: i; ?+ S
  -> 100
8 C8 i' O/ [$ J0 b
ord(str)   
* u% L+ @/ g. k如果字符串str句首是单字节返回与ascii()函数返回的相同值。0 y7 z( Y  w! X1 |! P( g' ?

0 c% s1 C/ z5 }% }如果是一个多字节字符,以格式返回((first byte ascii code)*256+(second byte ascii code))[*256+third byte asciicode...]  # Q9 e/ w5 c& J
mysql> select ord('2');  
( U- m+ s* v  K( k  -> 50  1 X) g) G' F& X
   
+ Z; w5 B" i6 Y" O- rconv(n,from_base,to_base)   - ]1 [2 n; b  Y8 O5 ]6 ~6 C- q
对数字n进制转换,并转换为字串返回(任何参数为null时返回null,进制范围为2-36进制,当to_base是负数时n作为有符号数否则作无符号数,conv以64位点精度工作)  
1 d9 Y5 d' w2 v9 ~" s( ]6 kmysql> select conv("a",16,2);  
% Z- n7 x! Q) D. t1 S: P9 U  -> '1010' % t  i1 i8 T* L( x% G
mysql> select conv("6e",18,8);  9 K( ^8 z0 \0 E
  -> '172'
, b  B$ K* X/ J: L0 I8 umysql> select conv(-17,10,-18);  6 B0 s- A; B: r. T( P6 f
  -> '-h'
8 ~) w% b8 W* c& X! imysql> select conv(10+"10"+'10'+0xa,10,10);  + R0 p! T) Q: B! e; d
  -> '40' % M5 v7 _. o7 f5 z# ?
   
7 `, }( V7 R" N- a% h1 @/ U+ I7 P7 zbin(n)   
8 Y# S- O% d6 T4 E; I- b把n转为二进制值并以字串返回(n是bigint数字,等价于conv(n,10,2))  0 W  l1 ~4 p/ a6 B5 U) {9 |& }2 M
mysql> select bin(12);  4 f" k) L5 }& h5 z# y
  -> '1100' 7 \! b# m% n/ j6 h
) a" G& y4 F9 J- _0 s
oct(n)   
8 n- q3 ^3 G* P! p. Z! D+ y3 A把n转为八进制值并以字串返回(n是bigint数字,等价于conv(n,10,8))  
, v/ x0 T& p' ?- N  i+ _% Hmysql> select oct(12);  
* N( v& m2 K" x% b, B; B0 E. k% e  -> '14'
9 M# l! A) g5 D& k( c& b  a7 y   
$ M, l0 S, Q- ]. V$ c' Qhex(n)   1 f! O. X( `( P1 z; O9 r, H
把n转为十六进制并以字串返回(n是bigint数字,等价于conv(n,10,16))  : V7 w7 q& K5 A9 D; M
mysql> select hex(255);  
0 p& p7 S' |: w- s  -> 'ff'
: h' `# o+ P: v& z" V; y' A   
8 m8 J4 o8 W3 Z5 s1 q$ nchar(n,...)   ) T: {) `' T% d5 T6 @0 q
返回由参数n,...对应的ascii代码字符组成的一个字串(参数是n,...是数字序列,null值被跳过)   ) X& W, C- g$ {6 d9 P
mysql> select char(77,121,83,81,'76');  
8 Q! x4 T, o$ e: ^. i) B0 w* F- V$ t  -> 'mysql'
  `2 {! ?+ G/ E( y: Y0 F1 Emysql> select char(77,77.3,'77.3');  
. _* S6 e) b: T5 o  -> 'mmm'
1 ]4 z# A6 h% e" E: s   
# q; |- U0 n2 i" `8 T( Uconcat(str1,str2,...)  
7 _, ?) [% G* U9 w把参数连成一个长字符串并返回(任何参数是null时返回null)  
0 P$ v% o/ @! g4 {# {mysql> select concat('my', 's', 'ql');  
& I8 p" l' N' ]7 ^7 y  -> 'mysql'
3 A0 X, ~8 T* l: @( b! u( fmysql> select concat('my', null, 'ql');  6 }7 T) `8 Q, Y- S5 }8 M
  -> null 1 s5 O- f1 k+ m, m$ h3 {4 S6 _
mysql> select concat(14.3);  ( [$ @! F$ d  z
  -> '14.3' ) D9 {0 ^# ?6 V! b+ `' x' u6 M
9 h6 V) N& {8 q" v: t
length(str)   
9 b; W% v1 g4 {: O4 K! Xoctet_length(str)  
6 r7 O2 r) ]) s% V( a8 gchar_length(str)  
! g7 S0 E- I5 j# \; Bcharacter_length(str)  
7 D& j/ j* r& W返回字符串str的长度(对于多字节字符char_length仅计算一次); M/ U6 ]- u- u4 i
mysql> select length('text');  
% m+ N/ E; ?% d$ p7 M' m( B6 U  -> 4  - V( U' R) F, D, A# U! K
mysql> select octet_length('text');  
% F! h& d. \  M( @5 r% t  -> 4  
( o/ O" c/ S( s2 Z. t9 ^0 x1 V 1 T6 c) S) a7 m$ n% n
locate(substr,str)     ?8 T2 |4 W$ |" q
position(substr in str)   1 Y. T" J* @/ {3 u6 w1 {
返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  9 G/ _# ]; t: K; O3 ]+ n9 F
mysql> select locate('bar', 'foobarbar');  
- P+ Y, f  _& z# U6 e. F  -> 4  # j8 ^! F) D9 P8 G2 U
mysql> select locate('xbar', 'foobar');  
9 q3 m- g& o1 k  -> 0  - G+ `& H& o9 ]+ V- ?+ j- v
    $ ~6 A* n2 Y/ I+ I& K
locate(substr,str,pos)
+ J& q  `7 i. i! w/ k0 Z返回字符串substr在字符串str的第pos个位置起第一次出现的位置(str不包含substr时返回0)  
0 _0 G( K! J" S1 [9 l% S5 gmysql> select locate('bar', 'foobarbar',5);  $ Q/ z3 O/ T' N. z/ }; i
  -> 7  
6 x$ V" e( N6 c
6 ~6 n( w1 X7 x( hinstr(str,substr)   . C: u  j8 G0 R, b
返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  
" K4 T! P% c7 a1 u* Qmysql> select instr('foobarbar', 'bar');  % }6 f/ A$ ?( h+ w
  -> 4  
+ p. Z# V: o; g: z  Gmysql> select instr('xbar', 'foobar');  + ~) d2 r+ l+ `" _
  -> 0   
6 f5 Q3 l2 \8 ?6 }8 G& F
! d) ^# {' K# |+ |+ L$ glpad(str,len,padstr)   ! F% G- b: s" v1 B3 U/ \, c( {
用字符串padstr填补str左端直到字串长度为len并返回  ! Z+ c+ h1 n( x# h# \& C
mysql> select lpad('hi',4,'??');  
. o% B# h6 k9 n+ \" G7 p  -> '??hi' * m  s7 Y% S# m7 U
   $ v. U$ `  \6 n* G7 ^% H
rpad(str,len,padstr)   
4 Y3 g6 U1 q, y6 l8 z用字符串padstr填补str右端直到字串长度为len并返回  $ T# y9 {; e1 {/ i. I( T
mysql> select rpad('hi',5,'?');  
! q1 E3 w- i. N9 U) d) @+ G  -> 'hi???' * B1 @7 L# z7 R/ b
% E& F  M; j% N# L, `2 k% @/ X% H
left(str,len)   
. K1 C2 W2 H3 Q( {返回字符串str的左端len个字符  0 E- \3 [$ c! [1 r9 m; G5 L$ x
mysql> select left('foobarbar', 5);  + N* L) b0 o* ^! a: V
  -> 'fooba'
6 a! p) r3 I# P) x9 J7 d( m) t7 z1 b
6 {+ }$ _$ v  T' G5 u+ E; Tright(str,len)   
5 w1 D4 B2 K" c9 F% `返回字符串str的右端len个字符   
5 S+ o$ i$ V( d+ Ymysql> select right('foobarbar', 4);  
+ M* u& ~2 ^/ T1 C6 i1 I. m  -> 'rbar'
5 d- \: N& o" e1 c9 M. D9 f9 t' { 9 U) v% G' o( b- s
substring(str,pos,len)   # N) ]) [- q  g3 i
substring(str from pos for len)   
7 @1 ?2 O$ I5 k3 q4 zmid(str,pos,len)   
$ ^1 |2 G8 B% n返回字符串str的位置pos起len个字符mysql> select substring('quadratically',5,6);  
3 g, L2 s0 a/ o2 ?* y# O# @8 K  -> 'ratica'
2 L: e' }6 z& K; q# K- B! h* L ) X/ x# \# K. X8 O
substring(str,pos)   
1 R  }* P9 v" J' xsubstring(str from pos)   # N; |. ]: B# Q" X
返回字符串str的位置pos起的一个子串  $ U5 ?$ v% e2 U% t: P
mysql> select substring('quadratically',5);  
& q. p8 L: g" F  -> 'ratically'
% Y6 A+ K3 S0 d( Xmysql> select substring('foobarbar' from 4);  4 `. T$ w- U' k) ]( m
  -> 'barbar'
# V1 w4 c0 P2 p0 s* K 7 `. \# W& ~" Q/ H" j9 K
substring_index(str,delim,count)   
; u+ d7 }; w5 k+ t, j; R# i返回从字符串str的第count个出现的分隔符delim之后的子串5 R% a# Y2 S- ?
(count为正数时返回左端,否则返回右端子串)  ) j, h4 {: _/ Y- T( c9 X" ?" e/ d0 E
mysql> select substring_index('www.mysql.com', '.', 2);  
1 T. P9 X0 `! y4 c/ c% h* [" h  -> 'www.mysql' ! h! ?, I  B# d/ h: J
mysql> select substring_index('www.mysql.com', '.', -2);  ( l, M3 ?* @2 |- K& h
  -> 'mysql.com'
, l# |# k8 L& n3 P' D4 y& b) j$ }
% a& \# T9 `3 B6 q+ M5 h; oltrim(str)   
+ a2 v& K( u: M0 z. R返回删除了左空格的字符串str  
8 j7 l4 z" K$ m# ^4 O( T( b1 ]mysql> select ltrim('  barbar');  4 i1 A( I5 X3 C# D# F$ G
  -> 'barbar' $ d3 T1 f. M9 D: K

, e& r1 K  ~% krtrim(str)   
! ^3 J& U$ J3 M% M5 T# g( i返回删除了右空格的字符串str  % F* E# h9 q6 q0 M
mysql> select rtrim('barbar   ');  - c$ }5 R6 D1 y. ~+ \( {1 y- |
  -> 'barbar' 8 p  ], b, s. t% a3 Z% D
/ y  _3 ]9 r, K; m' y: z3 l3 m/ y
trim([[both | leading | trailing] [remstr] from] str)  
- Z6 ^; S1 \5 F返回前缀或后缀remstr被删除了的字符串str(位置参数默认both,remstr默认值为空格)  2 h% N! W; i8 K! l0 |
mysql> select trim('  bar   ');  
: `4 U' Y0 b' f6 c5 Z% G  -> 'bar'
9 E% b( }; X% w' y/ T6 m/ emysql> select trim(leading 'x' from 'xxxbarxxx');  
+ B  g+ e8 O" {. `$ w" L3 Z7 C  -> 'barxxx' / t4 G& x/ @, P+ p: l; E6 \
mysql> select trim(both 'x' from 'xxxbarxxx');  9 Q# f, N5 M# |
  -> 'bar'
& t0 F% M: K2 zmysql> select trim(trailing 'xyz' from 'barxxyz');  
8 e5 U, z5 s- P; ], h  -> 'barx' 1 J. B9 a9 W, U- e3 C. Q  q, C
  Q5 J9 F; ]# L+ Z+ `7 R6 Q
soundex(str)   
  @1 [* N, R! x% z0 S2 u, `! |返回str的一个同音字符串(听起来“大致相同”字符串有相同的- N1 B9 G( f+ {" [
同音字符串,非数字字母字符被忽略,在a-z外的字母被当作元音)  
% \/ h) s2 x" {; [mysql> select soundex('hello');  
+ P5 L0 L) Q: Z! h" @+ K  -> 'h400' ! z7 ~( K( [% m; W; H
mysql> select soundex('quadratically');  " v9 B5 r5 `, l/ ?# C0 F
  -> 'q36324'
) o  ]$ k/ u: Y: i9 a$ x   ) W4 U) C8 P- l/ W" g$ M- I( X) Y
space(n)   
: c0 E8 T0 f& x& M返回由n个空格字符组成的一个字符串  ; D3 X4 ]" k3 z$ h3 V) f
mysql> select space(6);  ! e+ b0 B% ?+ h; {1 K2 e6 X
  -> '      ' ( T. n. v$ O+ v% h" U' r
   9 O4 A! O$ M! y2 H  u
replace(str,from_str,to_str)   
1 g6 O& q& ~* Y3 d用字符串to_str替换字符串str中的子串from_str并返回  
. G4 {. L. M+ gmysql> select replace('www.mysql.com', 'w', 'ww');  7 \4 |8 Y, I0 s& R% E' m
  -> 'wwwwww.mysql.com'
- J5 Q( M9 J& O1 D8 U) C
' W9 n% U: G, C$ l+ Trepeat(str,count)  
  {1 E0 A# r0 g; C返回由count个字符串str连成的一个字符串(任何参数为null时
# J  J4 t' |6 y. X返回null,count<=0时返回一个空字符串)  " G( v3 R5 {6 t0 G+ ?
mysql> select repeat('mysql', 3);  6 z# _3 A) j- C3 b* l
  -> 'mysqlmysqlmysql'
6 f* U# H' l2 x) X% `( j   
6 D  T* X+ a! \: Greverse(str)   * ~& o" q9 N3 r0 s$ B) g
颠倒字符串str的字符顺序并返回  
: r3 x( c$ {$ {; v7 Y4 B5 Zmysql> select reverse('abc');  " {( @1 v7 y3 _: N- [! l8 F
  -> 'cba' 8 L' u9 o, Z3 V, l- t8 v! a
5 D9 `7 ~9 s: V* q% {) F
insert(str,pos,len,newstr)   8 u7 V8 i- w$ x- Y
把字符串str由位置pos起len个字符长的子串替换为字符串+ o6 G+ J/ d( l% l
newstr并返回  - {1 B3 X' L5 X' W$ R  K3 m& J
mysql> select insert('quadratic', 3, 4, 'what');  
+ l9 Z& b2 |* Y# u3 r- ]) R  -> 'quwhattic' 3 i$ ?" O7 z: h
) l+ f  C# N- ~3 y
elt(n,str1,str2,str3,...)   2 S9 B% b+ N& {  K+ }( p+ \, K
返回第n个字符串(n小于1或大于参数个数返回null)  . g6 b# Z1 C, {
mysql> select elt(1, 'ej', 'heja', 'hej', 'foo');  
: E& S: l5 C) c  {4 @# K  -> 'ej'
$ @7 \2 u! b; S$ Bmysql> select elt(4, 'ej', 'heja', 'hej', 'foo');  3 ?, p& A: ]; f; v$ T8 x+ `  o4 L
  -> 'foo'
+ W/ ~7 v- G( W
2 C5 t  K0 R, O3 I9 R) qfield(str,str1,str2,str3,...)   6 O9 O5 O1 t- p2 V) b
返回str等于其后的第n个字符串的序号(如果str没找到返回0)  : s  G' D5 S6 W) L
mysql> select field('ej', 'hej', 'ej', 'heja', 'hej',+ V) W' Q/ [5 `9 y) s7 u  U
'foo');  : {5 b: k, P( R
  -> 2  8 ~) i1 Q+ O; s5 D
mysql> select field('fo', 'hej', 'ej', 'heja', 'hej',
. ~  f; x. p5 e, Q: M* I'foo');  
6 [( Y& R9 [$ K" ?  -> 0  ) S" H* g6 g4 E: q5 j1 S+ T, o8 @

, u; a: R& N, A: H" r; afind_in_set(str,strlist)   
0 j1 |, @1 l: ~6 a4 \返回str在字符串集strlist中的序号(任何参数是null则返回. _$ F) w0 a" n( N% A
null,如果str没找到返回0,参数1包含","时工作异常)  
, `3 i+ j! l, I$ Smysql> select find_in_set('b','a,b,c,d');  + u% F, E- j" Y' }5 [
  -> 2  : R/ k, p+ U9 H# u' Q
   
' ^/ Q$ i: ?* Rmake_set(bits,str1,str2,...)  
1 B4 j1 J7 S$ D9 ^$ [4 G把参数1的数字转为二进制,假如某个位置的二进制位等于1,对应4 V& b) j, ^  \3 ~( f3 C
位置的字串选入字串集并返回(null串不添加到结果中)  2 B5 g/ y6 o2 B# I
mysql> select make_set(1,'a','b','c');  
8 `' A- W7 b4 X* j) q  -> 'a'
8 N  d: _) y+ ?; f% k2 Mmysql> select make_set(1 | 4,'hello','nice','world');  $ u) e! `1 t* \! h1 P& w4 ~
  -> 'hello,world' / K! _& Z! V  _  {
mysql> select make_set(0,'a','b','c');  1 l" ~9 M( ]; |# S. Y# M6 S* T
  -> ''
! f% O" C# V, t7 W  i
4 u) w/ V/ S& O* i+ Jexport_set(bits,on,off,[separator,[number_of_bits]])   
$ W9 x* ?/ P6 j$ I& E按bits排列字符串集,只有当位等于1时插入字串on,否则插入
' b/ S8 x7 y" S, K* Zoff(separator默认值",",number_of_bits参数使用时长度不足补0, X" u: J+ ], w4 p+ V2 [3 V! o
而过长截断)   3 R! Y/ o; }! I# q7 a* i
mysql> select export_set(5,'y','n',',',4)  
. @1 [+ N/ Y. h1 @! p/ ~  -> y,n,y,n   # N6 i* q( u* X* d
- \- b5 f' d1 u# @& s# A
lcase(str)  
7 A% R! J9 Z& e# N. }lower(str)   ) {8 m' S7 z. ~% N! V- B# c
返回小写的字符串str  * s+ {$ R1 T: q$ i  E
mysql> select lcase('quadratically');  
% r+ Z' y7 t: w# Z* ]5 _, U" p5 g# A, [  -> 'quadratically'
; `! L" `, K6 A+ d   8 \4 ?8 f& D6 T6 V& {3 A
ucase(str)   
* q  g4 A0 }% J& Tupper(str)  
2 R* X! h  L2 T( L1 K" i9 e8 ^返回大写的字符串str  
/ I1 |, t8 K1 Y% X) l! Ymysql> select ucase('quadratically');  
$ J! C3 T; B6 ?- M  -> 'quadratically'
# c* i+ l4 w  u1 U
# C5 Z5 F  T6 |- {load_file(file_name)   
( h, b0 Z0 e0 p读入文件并且作为一个字符串返回文件内容(文件无法找到,路径
( g) H9 ?# }- m6 m不完整,没有权限,长度大于max_allowed_packet会返回null)  
1 s. {" ~% V$ a6 x/ ^mysql> update table_name set blob_column=load_file7 Z8 f: L' j# X( `  ]" i6 L
("/tmp/picture") where id=1;  
  J% s# {+ h* D. Z
& W# T" r1 z2 X" |( T2、数学函数
+ r# @$ u$ K  ~2 H6 O) W0 Sabs(n)
8 a2 G& k/ H/ }1 N7 q3 D返回n的绝对值  , Q7 c% o: A! W& L# Y( g# p
mysql> select abs(2);    - H/ n* U3 n0 w# _2 [
  -> 2   
% F/ G/ q+ G0 [) ]4 S. Vmysql> select abs(-32);   
8 U' D7 f7 I- G2 C  -> 32   
: P5 G1 `0 x7 ?/ I; A   
) H% i. T8 s* z$ A; S- Msign(n)  
6 u: W1 \) ?1 K; T* K7 t返回参数的符号(为-1、0或1)  
( D8 _# N4 t7 i; gmysql> select sign(-32);   
3 T& O: p5 @# ^6 O3 y; q6 x  -> -1    # j- L0 j0 M8 q5 t" V3 S0 a
mysql> select sign(0);   
! s3 W7 ]& q; [# N: Y( v  -> 0    3 t; t, z! Z  c
mysql> select sign(234);   
9 J' |9 l2 ^- p. O0 _1 o  -> 1    2 X8 o8 A' E4 m. H
) D$ l  ~0 q+ l% M8 m
mod(n,m)    ; u  `5 H$ |" G5 F+ F
取模运算,返回n被m除的余数(同%操作符)    8 A5 |% P0 N* m) C6 K
mysql> select mod(234, 10);   
0 i0 E( d0 }+ Z, H9 P1 B3 X" R  -> 4   
+ b1 |: S" j" @( `mysql> select 234 % 10;    5 I. Z3 @+ S+ P* ]+ R6 w$ @
  -> 4    1 B6 p; M2 b7 k" {1 h
mysql> select mod(29,9);    5 z9 Z$ x( [1 j6 k5 x
  -> 2    : k1 s& X' _  q2 t3 s+ T& J8 T8 ^
: f5 E# c& V6 Q; t9 a! v; l
floor(n)  
5 X6 {) o6 [3 _1 x) O* F! b% J! X返回不大于n的最大整数值  
7 i& O0 q% c7 |6 H, pmysql> select floor(1.23);   
  e! l2 l0 ?% Q/ u$ G( k" C& k  -> 1   
# K/ G  |: F7 ^% D* j( y" o& Smysql> select floor(-1.23);    ' f' v) _, P+ a7 Q5 V6 f, h
  -> -2    " p4 T+ `" u( D5 f' L. K8 }3 h+ l
2 w! f+ C- h) s& P, T9 P1 D
ceiling(n)  * i! x+ \! p, B* O' i; [) {
返回不小于n的最小整数值  
) t9 z7 ~+ T% Omysql> select ceiling(1.23);   
) ~+ z# N; c+ d& K3 A- T  -> 2   
3 ~7 p  m8 w+ o5 Fmysql> select ceiling(-1.23);   
9 k) J+ P; w; \$ V  V" Q. m  -> -1   
3 g' D# D' I  D, o- L8 W+ O& i5 g
! U/ D% J6 ~; v& Qround(n,d)  # Y1 H9 q) @6 u* ^$ f) ^
返回n的四舍五入值,保留d位小数(d的默认值为0)  8 J2 |& r) Y$ q  ?1 k
mysql> select round(-1.23);    2 ?: C" R* _! C, V4 j5 U; ^
  -> -1    0 n" O7 b* [, X& e7 E, ?
mysql> select round(-1.58);   
: T8 T) ^$ A( L( n- r/ v2 P6 b7 W  -> -2   
# c- j+ C; @7 [0 L/ `3 jmysql> select round(1.58);   
. S0 I' c7 E3 L+ s0 K: p  -> 2   
- l! K  K+ r+ x* U6 [mysql> select round(1.298, 1);    ( o" G( J5 ?( n
  -> 1.3    6 ]/ g" N0 U: y  o2 d5 q) y; h+ [
mysql> select round(1.298, 0);    ; \  w6 S& a; n( p
  -> 1    ( H8 G' ~) q8 `6 Y

$ O0 [, ]. N+ @7 Gexp(n)  ; k; G" j, X0 v" x" z* R+ o
返回值e的n次方(自然对数的底)  6 _& Q6 s" b, y' E: b
mysql> select exp(2);    " p* e) I& ?0 W' b5 Z9 ]
  -> 7.389056   
  }; _" E3 l2 H% v0 umysql> select exp(-2);   
% G* I# C- [$ \0 P  -> 0.135335    6 E1 ?4 w3 u7 P6 h( V" T
& }( Y/ q6 J& {9 \1 h  d5 l
log(n)  
2 X% a$ X3 O- \返回n的自然对数  
' D5 `4 x. T3 B4 Gmysql> select log(2);   
( X$ u6 o# j4 j' q6 C  -> 0.693147    5 N5 U5 s9 b1 \
mysql> select log(-2);    * o4 X  H, M+ i* @
  -> null    $ T/ J) \, d9 e4 L* G% q" F/ s# K4 v1 A

/ g% j$ w6 \( Y; ylog10(n)  ; H" ]( E* d. E
返回n以10为底的对数  
: T  [# U8 u2 b5 x5 a! [mysql> select log10(2);   
+ X. @1 k. {! A; {, k) C) g4 O  -> 0.301030   
) r7 s' n" @: ]. fmysql> select log10(100);   
) `; Q: ^( i1 }3 y+ g  -> 2.000000   
. `5 z& I6 R. i+ ^  emysql> select log10(-100);   
' `( t" v3 l! l- _  -> null   
7 E* f; @5 c; a& ^8 T0 H
" M9 f( v. D/ j- K0 ]pow(x,y)    ' F( q2 D& Y2 `; @/ Y
power(x,y)    7 u9 @$ Z% _0 z! m
 返回值x的y次幂  
7 j! e7 x/ n% Emysql> select pow(2,2);    9 O; ]' B1 O- Q
  -> 4.000000   
- a! v- T- @: Omysql> select pow(2,-2);    3 Q+ _1 `/ M0 N3 f2 J
  -> 0.250000  % {; w2 E  G  C+ e
: {% |, o; {4 [7 b/ o* E
sqrt(n)  
; k% _: w2 q9 X3 f  l; i* ? 返回非负数n的平方根  ' ^- E! b) V0 C5 V& p
mysql> select sqrt(4);   
6 F, Z( v, U1 j# D  -> 2.000000    / Q* V# q, H* N
mysql> select sqrt(20);   
3 D& C8 f2 I: O2 ]/ [4 y: a  -> 4.472136    ' _5 P' J$ f) t

( K, y7 l" G, P( u- P1 |# {pi()   
5 u# s$ C) d/ t 返回圆周率   
4 b' \- h3 S# Pmysql> select pi();    - b7 e% D* R5 E* N" ?
  -> 3.141593   
8 {: E$ S6 F+ a8 w5 i ! \, F" u6 E+ U; X, M! m: c& D
cos(n)  
" ^# K! i5 ?' l: c1 T/ K* Q 返回n的余弦值  
$ ]( }+ z. \0 Z5 X2 z0 Hmysql> select cos(pi());  / K( \; J! ?$ O1 e$ J0 n
  -> -1.000000   
* ]* u$ g) ]: S% B" Y . L# q3 ?8 K4 c3 I; G2 p# _
sin(n)  ! K7 K/ u/ I1 d; v* l! s# G
 返回n的正弦值   
% T$ j0 [" ?- |6 W* jmysql> select sin(pi());    8 p2 L6 w+ i# U, R: d
  -> 0.000000   
) W) y# J) {5 _8 ?
& f" C# x, ^: K6 q5 L9 S! Ptan(n)  
: V0 |% s! w! P: M0 b3 C返回n的正切值  
% @9 o0 u% l) e3 e' U; d6 P' Emysql> select tan(pi()+1);   
' x% m6 Y1 G: V  ?! T6 M# O+ M% `* l  -> 1.557408   
$ r9 e( S+ |: Q( K5 J$ r
8 l6 O5 V4 L; }% C5 Y' ^# jacos(n)  , b0 B7 z# Q+ N3 w4 G: v6 |
 返回n反余弦(n是余弦值,在-1到1的范围,否则返回null)  
& P  O/ {5 n6 T3 K$ t8 d0 [mysql> select acos(1);    ; t2 f  B& N: L
  -> 0.000000   
6 j- H" g4 v. Cmysql> select acos(1.0001);   
9 c, Q; n+ j! o# m" \2 \1 H5 _4 c  -> null   
; P; C) ?( @) K& z" ]mysql> select acos(0);      c6 a  O9 a& I; ?* x- o! Q9 V, W) H
  -> 1.570796    9 o* L4 E: P) p; D+ U
4 }+ s5 K* ~/ U+ Q: D- f7 ]0 |: k
asin(n)  * P+ ?, [4 k$ J8 W% U) z) K
返回n反正弦值  
# Q; O6 C0 G* w% Bmysql> select asin(0.2);   
2 D5 v; [$ N8 P' t  -> 0.201358    / o& m  w) G3 K# K
mysql> select asin('foo');    ; F1 {& w( B, }
  -> 0.000000    . h2 I! I. W, M+ ~5 A! i1 `

' Z; E; w+ M" aatan(n)  : R+ y1 v! a, k$ x  B
返回n的反正切值  - }* ]" o6 w% H# S
mysql> select atan(2);    ! G, M5 W$ x, w# h9 ?$ t2 ^
  -> 1.107149   
2 ?/ q# H; z; d# N3 cmysql> select atan(-2);    : M1 Z: B* Z5 R
  -> -1.107149    4 [7 a0 f8 b3 i3 P0 a' U( k
atan2(x,y)    ) }9 I& E* U  H0 ?+ J: [5 W& K( f
 返回2个变量x和y的反正切(类似y/x的反正切,符号决定象限)  
. f; e; p% [1 s: T5 o' fmysql> select atan(-2,2);   
$ M' W5 i6 C% a  -> -0.785398   
! d9 R9 K7 k. ^2 q* ]7 I+ G, `mysql> select atan(pi(),0);      h% Q" B# j4 j" l# s5 b& U
  -> 1.570796    7 s3 X/ u. R6 H9 m
) c& `( c; Q9 s( b  G" l1 i: \' ^
cot(n)  
: o; Y6 r; {) a" M. Y返回x的余切  
4 F2 M* ^- |2 e& l, _" ~mysql> select cot(12);   
0 e3 ?- e8 c0 _1 s* d9 r: P" f  -> -1.57267341   
  ]2 U2 ^! ?! a3 ^# Tmysql> select cot(0);    , Y, }: f' [2 s# n: c7 R
  -> null    8 F% n5 F0 Y9 v4 }' y3 i
3 e8 {  u/ y0 p
rand()  
( b. E9 b* }! c2 }$ G4 D$ W# arand(n)    ) s& {% `, T( X6 {, X
返回在范围0到1.0内的随机浮点值(可以使用数字n作为初始值)
2 k: X8 e' A* X* E) p, I
" f' w1 A5 \* f4 t7 [mysql> select rand();   
3 i; Z- D9 i8 @/ _8 }) `5 C2 R" }  -> 0.5925   
+ |1 E+ O3 ^! d. gmysql> select rand(20);   
% o! ?  a9 s/ U! c3 O; b  -> 0.1811   
  g2 t: [) }$ ?9 l# Nmysql> select rand(20);    ; E1 Z  P2 l; R
  -> 0.1811    3 I2 e1 B, R" {7 K; ]8 j1 X
mysql> select rand();   
$ g, I* b! {! J  -> 0.2079    ; K8 }: I% q9 i* f
mysql> select rand();    0 Q4 z1 R+ g0 b" p8 B
  -> 0.7888    0 ^1 D, Q- V) S- R, m

' j' @/ E% y6 @3 b4 Z( u, P8 |degrees(n)  
% {, D  c3 F) W6 Q! |6 M9 \* S把n从弧度变换为角度并返回  
8 h  h, l# }& @mysql> select degrees(pi());    ) y8 a( K0 N: w# i
  -> 180.000000   
) P$ o) N# Z$ k# _: v0 [1 V
' h, x* J; Y" `% {* Hradians(n) ; D" }; j+ r: W$ g# [( z/ r
把n从角度变换为弧度并返回   
& O1 J3 Z9 ?& a2 x/ wmysql> select radians(90);   
. U2 W' F, j2 @9 h- v, k  -> 1.570796    " M/ C9 S# u6 n$ q, G( i3 z0 \

! n9 N( \0 b  z$ Ytruncate(n,d)   
' }$ n7 h4 d0 S8 d) \& R0 R保留数字n的d位小数并返回  ' [6 l( m4 m% p9 b* o! h# G/ Q& Y
mysql> select truncate(1.223,1);    0 X# l+ I% w# E
  -> 1.2   
8 C) q" _1 Z  Y$ i0 _5 }mysql> select truncate(1.999,1);    $ j* ]. D% g9 G' N. l# E
  -> 1.9    : j7 ^2 d2 S+ U% d8 Z% [) `8 Q
mysql> select truncate(1.999,0);    * q/ ~8 T- A7 N
  -> 1    $ ]0 A6 y' {: f0 n8 h* u* l
" }8 ^. \# s% Q1 M, a* e
least(x,y,...)   
+ h- L: @1 q% y2 j& X返回最小值(如果返回值被用在整数(实数或大小敏感字串)上下文或所有参数都是整数(实数或大小敏感字串)则他们作为整数(实数或大小敏感字串)比较,否则按忽略大小写的字符串被比较)  1 e% e. ?/ X- e
mysql> select least(2,0);   
9 \* `- A* x! @3 U$ {1 ^: h  -> 0    ; v; G: Q/ v% w/ _7 O
mysql> select least(34.0,3.0,5.0,767.0);   
6 t% B- T, p' l1 _' L' {  -> 3.0   
& y+ U3 D2 s: Dmysql> select least("b","a","c");    4 j: I3 t. e+ B' q9 D# n. w
  -> "a"   
& w# n$ W0 D9 q 1 }" p1 b% ?' f2 B" F% P/ F
greatest(x,y,...)   
9 w! Q$ Z% G6 E8 C& P7 ^, D返回最大值(其余同least())  # o$ N; S6 V5 d4 g. T3 q
mysql> select greatest(2,0);   
* V* Z0 Y, ~+ z& m. j% d  -> 2   
) l. O* `) m$ U1 Tmysql> select greatest(34.0,3.0,5.0,767.0);    5 x) ]) N' e# H1 e0 i" ?# E% h4 o
  -> 767.0   
5 T! W# F# h  ]: }* d7 amysql> select greatest("b","a","c");   
3 W6 O+ V& v1 n% K  -> "c"     
7 A  w  [# Z3 k# p) r, a, ]/ q5 X1 g' l4 S. L5 h
3、时期时间函数 % X2 n$ u' I- x+ [" _1 M* }. N
dayofweek(date)   
+ m3 \. A- k, D5 H2 K2 R& ^- q返回日期date是星期几(1=星期天,2=星期一,……7=星期六,odbc标准)  5 t! k; u! A" n5 P2 r) j
mysql> select dayofweek('1998-02-03');    * Q) J! h4 p" `& p
  -> 3    # p) ]. i! x& ~5 ?3 V6 ~

+ r6 I% d7 i, \9 @9 k; Mweekday(date)    # l3 l. z4 i; f) x
返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。 # Q# @5 D# \2 S$ S6 J
  $ L3 c: N6 z7 ]
mysql> select weekday('1997-10-04 22:23:00');    , f8 _# E2 a9 E6 Q% ~2 B2 I% |
  -> 5   
+ N! |# w7 B0 tmysql> select weekday('1997-11-05');    7 s" f7 E$ n) u  ?$ j
  -> 2   
; d. q8 Z  |% j( O1 t
" k. G+ G* ?# }# vdayofmonth(date)    / W& d( P% z; x: ?. N/ R
返回date是一月中的第几日(在1到31范围内)   
* A$ M! O9 h5 t+ wmysql> select dayofmonth('1998-02-03');   
+ P1 ~* a: \* r& o" v3 M( }  -> 3    + W0 I, V& E# G  P. h( A, Q
! J2 z/ s% U8 y) f+ _. |% O, a
dayofyear(date)    6 R4 t4 S# f& V& d! k  M! \; `/ f3 n
返回date是一年中的第几日(在1到366范围内)   
1 J$ G: N0 @  x. T: ~( I5 Hmysql> select dayofyear('1998-02-03');    ; n7 C0 P2 P% m  H: @
  -> 34    ' W) b* g" m; Z; ?1 h2 r  Q
5 R3 q5 F8 M  h. M# w3 a
month(date)    + i# ?0 o% ^5 T) L5 ?& H5 P
返回date中的月份数值    4 W7 v( p1 w2 _. v
mysql> select month('1998-02-03');   
1 J% v  ?8 F, f% H6 A  -> 2   
# P# N6 V3 \( T4 F4 I$ B2 o
4 R" Y# B& H+ F; l" f# \/ t: l: U2 ~2 jdayname(date)   
, z' D$ A/ |7 D, V4 Q( k2 H返回date是星期几(按英文名返回)  
, I9 z- q: Z, ]) C# ?  F" C8 Gmysql> select dayname("1998-02-05");   
8 C. y. P# h. ]% H  -> 'thursday'    2 j4 o4 {( f" m1 R
8 f! X* J1 L9 A/ m' j
monthname(date)    $ b' c. U, d: Y( l
返回date是几月(按英文名返回)  ' A. e3 p: w% ?$ [. Y3 q7 S
mysql> select monthname("1998-02-05");   
7 a+ c9 d$ ^: L1 z! H. I1 L  -> 'february'    0 g) [$ D4 X8 L6 N

% V' @8 _* X( f: P  k6 Kquarter(date)   
8 }* g4 Y: U& b返回date是一年的第几个季度    - W3 H, M" A+ g3 h
mysql> select quarter('98-04-01');    9 m5 T* t8 F# V* V$ g( w1 x: M
  -> 2   
: x9 Z: a8 K; Z  o! ]
/ L9 `+ Y2 p0 O6 M7 cweek(date,first)   - s  z( O8 @/ [- p
返回date是一年的第几周(first默认值0,first取值1表示周一是: B3 G# E* {2 Q6 a2 K- H0 ~
周的开始,0从周日开始)  
& H: C. N5 G; j& r$ `8 {mysql> select week('1998-02-20');    & u: ~( Q2 O( g, h
  -> 7    $ t" u. c, p' {: @+ x( w4 l+ T
mysql> select week('1998-02-20',0);    8 _0 w1 F5 j* Z
  -> 7    : {4 s4 ?  o: X; `- K4 V8 L
mysql> select week('1998-02-20',1);    ; P0 n+ _+ G1 J! ~$ q8 E
  -> 8    # ^* r' y5 N# @  J9 g
- h: g5 s, }  s3 x- h& _  j  W
year(date)    - T  }7 ?0 [* L# l9 C
返回date的年份(范围在1000到9999)    ' O5 z$ _# Y0 ^  {
mysql> select year('98-02-03');   
7 a6 c2 x$ `7 r8 s5 b7 |  -> 1998      ^  I( D# u  o" W! g

; b3 n  r! T; ?( E1 K; u  Mhour(time)    
! s7 p/ X$ ?  O. i返回time的小时数(范围是0到23)   
! i4 n- h* F( h: L* t$ z- P6 ]  lmysql> select hour('10:05:03');    2 X% Z2 N% a9 f
  -> 10   
( ?0 ~' w3 b1 o5 Z; g2 k
0 s, P3 Z+ P: T( Q% D, mminute(time)   
3 E; V8 Z( Y$ @/ _: F4 l0 _  Q返回time的分钟数(范围是0到59)    . u. L9 {6 J( {" Y
mysql> select minute('98-02-03 10:05:03');    / i) o2 u, U2 W5 w: l1 d) L
  -> 5   
# p; w( H! O- R9 ^' }6 S
% m$ p* x, s0 L0 v  R( Msecond(time)    
0 n. R& H( I( v+ W返回time的秒数(范围是0到59)   
, t3 e: C: |) Q! L  l$ C9 rmysql> select second('10:05:03');    - E2 K+ z8 T; L* o% s6 s$ t3 @
  -> 3   
4 A" B' J- {) ~ ( J" c( c4 ?- y8 e# p- A
period_add(p,n)   
9 a) G5 K& g' D$ K  E% R0 }' m/ I1 O增加n个月到时期p并返回(p的格式yymm或yyyymm)    0 [1 `: p& I( O3 W. P" m
mysql> select period_add(9801,2);   
5 W. j' {1 J& u7 i" S% s  -> 199803    , e+ a/ {; w- n$ _. t

5 ]9 \/ L5 I: ~5 qperiod_diff(p1,p2)    1 Y8 k0 n# |3 t5 t# o8 M# A
返回在时期p1和p2之间月数(p1和p2的格式yymm或yyyymm)  
7 R% l7 o' L4 ]5 Q3 [; A  [2 ~' O% E# Imysql> select period_diff(9802,199703);   
# k5 t) S! j( F) v9 M1 {  -> 11    $ C! o  h# H/ k. t# ]4 d

/ u* G% T' ^! e# V1 Odate_add(date,interval expr type)  $ d+ q+ U9 H7 S% A$ p; ^% v: O6 @
date_sub(date,interval expr type)    2 U/ U7 `" D3 i& m; |( |
adddate(date,interval expr type)   
: u2 X1 s* t1 o; xsubdate(date,interval expr type)  : a8 Y3 F/ o7 }8 D+ Q- G
对日期时间进行加减法运算  3 J3 d8 `1 Y9 \; e
(adddate()和subdate()是date_add()和date_sub()的同义词,也
6 Z- ~# b# v, G; n2 [7 y可以用运算符+和-而不是函数  
- E3 v3 M" M4 mdate是一个datetime或date值,expr对date进行加减法的一个表! E  O% B" b1 \7 G; k$ p
达式字符串type指明表达式expr应该如何被解释  , M( w9 V8 k' [+ ?: H: ]; ^; ^
 [type值 含义 期望的expr格式]:  
% i# e( R% d/ k/ P: f second 秒 seconds    - Q$ v& L! \5 a
 minute 分钟 minutes   
$ j! z+ K* e; C. t; Y2 g- x4 L hour 时间 hours   
8 M; w( R& ~# g day 天 days    + U7 C4 v% [6 N( W% T, W* j1 G
 month 月 months    6 ?5 L2 h3 |3 n! B  Z- f2 P
 year 年 years   
1 }6 T) J/ y2 }; H% u8 d  N minute_second 分钟和秒 "minutes:seconds"   
3 q0 W, {2 q( u: S/ v! V hour_minute 小时和分钟 "hours:minutes"   
. T4 A) N7 |" N2 ^0 m8 i* W day_hour 天和小时 "days hours"   
* e8 h( Q# M8 S: i* K year_month 年和月 "years-months"    3 b( A- v9 a  O# ^3 o
 hour_second 小时, 分钟, "hours:minutes:seconds"    * n# u2 [' s  ]4 @9 C
 day_minute 天, 小时, 分钟 "days hours:minutes"    9 r) b, c6 t' s1 u1 Z' A( O
 day_second 天, 小时, 分钟, 秒 "days
# F$ Q1 H  V3 M' M' U3 Q3 X8 d+ Nhours:minutes:seconds" : I* K; m) q7 s8 }) T! X
 expr中允许任何标点做分隔符,如果所有是date值时结果是一个% \' U: `) {' C& K& ?4 \+ a
date值,否则结果是一个datetime值)  6 Q2 y. x# s/ [1 ]
 如果type关键词不完整,则mysql从右端取值,day_second因为缺
+ V/ |  w/ P* C& j- k* L) L少小时分钟等于minute_second)  + G% \2 j# w+ [! U8 J7 }1 z1 u
 如果增加month、year_month或year,天数大于结果月份的最大天3 `' ]% f% T0 Z# `. r% ~# r
数则使用最大天数)    ! c7 _& @$ T  A2 G6 q! t/ p
mysql> select "1997-12-31 23:59:59" + interval 1 second;  
& m" u" Y  b3 }- e) w+ _
4 W- S" R7 H* [7 L1 S$ m6 |! t) R$ F  -> 1998-01-01 00:00:00    2 R, V# @+ I9 Z! w1 o3 L
mysql> select interval 1 day + "1997-12-31";    & i  {: F4 M' |8 d/ S2 a% [
  -> 1998-01-01   
7 V, Z' T  @0 j4 }- D- z: ]mysql> select "1998-01-01" - interval 1 second;   
2 G- L2 ^- Y( ^0 U6 u  -> 1997-12-31 23:59:59    * L+ V5 s6 R7 X! P) {# }6 O- e
mysql> select date_add("1997-12-31 23:59:59",interval 1$ \5 M* B2 o( S- J' X- |5 y4 a
second);    9 h% o3 o5 Q& \0 V# o1 l
  -> 1998-01-01 00:00:00   
2 L4 |. t& {. C+ ?mysql> select date_add("1997-12-31 23:59:59",interval 1: }6 R( T, ~" w. h" n+ _. a
day);    + j" Z/ ^/ \" q. l5 q
  -> 1998-01-01 23:59:59   
  ?$ K7 e9 V& h" B) hmysql> select date_add("1997-12-31 23:59:59",interval
# j, Q2 ^' v1 M! E( F9 G* \"1:1" minute_second);    5 P' B! l$ r) a" t! f4 Q
  -> 1998-01-01 00:01:00    - J+ P! D$ o2 p& g' [' O8 u: T' w' s
mysql> select date_sub("1998-01-01 00:00:00",interval "1# [, W8 K; i/ d5 ?! L. i/ T& J
1:1:1" day_second);   
: r' ?% U' \# ?# l# f  -> 1997-12-30 22:58:59   
) D/ u. k9 W6 y# rmysql> select date_add("1998-01-01 00:00:00", interval "-1
& U3 j3 `# Y. ~6 t; c1 x10" day_hour);  
% z9 r  H, `& L8 r2 S  -> 1997-12-30 14:00:00   
$ K; a$ U. S7 c7 Zmysql> select date_sub("1998-01-02", interval 31 day);   
/ i: j. n# ]! H$ O  R% S$ |  h9 {  -> 1997-12-02   
, s: I3 F- T5 e% mmysql> select extract(year from "1999-07-02");   
2 d; u6 {5 ~: h  a. g2 q  -> 1999    ) L5 m; ~( w$ D+ e
mysql> select extract(year_month from "1999-07-02
! O0 a" q9 [# y$ w2 ^* Q! |01:02:03");   
5 Q* }5 i& d8 k3 A  z  -> 199907    , x& M& @2 y9 }/ c, _, V; b
mysql> select extract(day_minute from "1999-07-020 `: p) G0 G+ B# x
01:02:03");   
) \) g6 \7 n5 K' S, z5 t  -> 20102    # M+ G; ^  e4 D3 r8 t

3 k. R1 S% I6 E- I" ~$ l( ]to_days(date)   
# Z( B! S3 Y% P返回日期date是西元0年至今多少天(不计算1582年以前)  
9 ?$ d: q( y; X8 U: i. I; v9 z. zmysql> select to_days(950501);   
/ ]9 ]5 u! ~# M5 `/ E- [$ V; x  -> 728779    " o" p' ^3 |6 L; N" ~4 S
mysql> select to_days('1997-10-07');   
4 X1 |% t( @9 N8 k0 d/ B4 c  -> 729669   
" h% n6 }& }0 i3 @' v+ X ! u6 }' C4 K( |0 {3 s% o
from_days(n)   
# i' T: {+ w" v 给出西元0年至今多少天返回date值(不计算1582年以前)     o! j) a( h" q8 d6 p- C3 A& k
mysql> select from_days(729669);   
+ M& X- W; `5 S$ j' P  -> '1997-10-07'   
" `9 R8 |0 U2 w. ~/ j
( y$ [4 j* B4 ?# ^- V" X/ D6 Udate_format(date,format)   
) |2 z/ g5 Y, i  i( a' s 根据format字符串格式化date值  0 r2 m/ z% G; J( ]# k2 L
 (在format字符串中可用标志符:  * E7 Y" H+ ]3 c$ J5 V) ?
 %m 月名字(january……december)   
2 ^+ v3 B4 K9 l %w 星期名字(sunday……saturday)   
, R1 I# ~2 H6 a7 L0 Y  { %d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)    , L9 e2 o# S8 J
 %y 年, 数字, 4 位    ' n& ?+ Q) ~/ u
 %y 年, 数字, 2 位   
9 ~( a4 I1 U* z! N- y5 m: ] %a 缩写的星期名字(sun……sat)   
; l9 n1 _( F7 I( v% ]* A %d 月份中的天数, 数字(00……31)   
: {0 Q9 I7 k# N %e 月份中的天数, 数字(0……31)    - R* }1 U( f) Y( Q
 %m 月, 数字(01……12)   
' U+ k, j. h6 _( h- M %c 月, 数字(1……12)    + F, Z; e7 z0 b! [+ f( n
 %b 缩写的月份名字(jan……dec)    5 D1 m1 k) b- c
 %j 一年中的天数(001……366)    5 `( E% a; J: W$ _& J
 %h 小时(00……23)   
. |6 v# F) m' d6 }1 j %k 小时(0……23)    6 b& [/ h& l) O% |3 F, ~+ W/ A2 E5 }
 %h 小时(01……12)    % Q6 I+ B# l/ L% g
 %i 小时(01……12)   
$ T; `- K" t- i0 i# n. B %l 小时(1……12)    % J: t! @$ H! b) ^- i) ^" z' ]3 a
 %i 分钟, 数字(00……59)   
9 d1 W8 G% r( z. E %r 时间,12 小时(hh:mm:ss [ap]m)    0 X* t- t! ^1 M' \4 b" O
 %t 时间,24 小时(hh:mm:ss)    9 U6 C2 ?- M/ `7 N, V4 W5 }$ q, A
 %s 秒(00……59)    . ~- B' k1 r8 H$ {
 %s 秒(00……59)    " j( B7 f7 H8 X8 N3 Z
 %p am或pm   
- Y+ c% @. l5 X# D %w 一个星期中的天数(0=sunday ……6=saturday )    2 }7 H) v+ @0 ^* t% H3 H& _
 %u 星期(0……52), 这里星期天是星期的第一天    * B: s1 e7 h8 ]" Z/ `
 %u 星期(0……52), 这里星期一是星期的第一天   
3 ]2 }  k; }. B+ _4 e %% 字符% )  
0 [- i: k% C0 T% M  Pmysql> select date_format('1997-10-04 22:23:00','%w %m %4 ^' Z/ ~' q0 m) x
y');   
4 ]) [6 ~* Z7 F  -> 'saturday october 1997'    ) M# }( S3 j- q+ n" D: E
mysql> select date_format('1997-10-04 22:23:00','%h:%i:%
5 G3 P* a; Y# ?- E& I' bs');    * Z5 _0 K, Y. d8 c9 |) W
  -> '22:23:00'    ) _+ I& c+ T& o* W0 A- ~4 ^
mysql> select date_format('1997-10-04 22:23:00','%d %y %a
" ]& E! V' @6 f+ ?% i/ a%d %m %b %j');    8 U7 t1 m: A, r
  -> '4th 97 sat 04 10 oct 277'   
' k# k; I- Q' W; b3 ?! Cmysql> select date_format('1997-10-04 22:23:00','%h %k %i/ U4 B  k3 I7 U8 ]  \
%r %t %s %w');    + C: C. H1 i* g- \* `
  -> '22 22 10 10:23:00 pm 22:23:00 00 6'    - A! R) h% [" e
: g6 v* \/ e1 V' y# W" p
time_format(time,format)  
9 M8 _8 `, L1 c* M6 o' ?/ }  ?# i! x, M 和date_format()类似,但time_format只处理小时、分钟和秒(其$ p* _7 z8 @  K2 g  M
余符号产生一个null值或0)  
; H* e" ^' J+ D5 ]$ {! D
; Y6 ?% J0 h$ m# V. {, y; }8 k1 N" jcurdate()     
" z0 P3 {, r' `6 ?current_date()  ( x$ g( @- ?: M: Q4 J& R1 l, o
 以'yyyy-mm-dd'或yyyymmdd格式返回当前日期值(根据返回值所
4 p& A# U! n/ b4 I* A$ Q0 S# Q. z处上下文是字符串或数字)   
/ B6 x, Y- @1 H: d0 T' O0 {mysql> select curdate();   
" Y% u- U4 ]6 x- j5 K  -> '1997-12-15'    % \6 Y$ ]2 k' X- B
mysql> select curdate() + 0;      s1 C( }: h# K: G( d# ?
  -> 19971215    & U5 ~+ M. B) y6 q

6 L, i4 x2 g' |; X6 N/ Ecurtime()    % w) P# v$ m' _4 D+ A: I, k( {) }" R$ P
current_time()  
( g. P$ V7 O( }* m 以'hh:mm:ss'或hhmmss格式返回当前时间值(根据返回值所处上3 P# O7 B" L7 l8 Q) o: \
下文是字符串或数字)      
& r6 t& @/ E! |% M+ |, F. bmysql> select curtime();    ( \7 B1 f, }: `$ C! u% t1 J0 x2 I: r% d
  -> '23:50:26'    & P7 H+ Z% H* x( E8 p
mysql> select curtime() + 0;    ) W* U, ?- [% B
  -> 235026   
4 C1 a* H2 P. Z ' U1 I, |# y, A/ k
now()    
, Y/ s/ ^8 T4 [& h/ k2 `sysdate()      _; g( s( m# c
current_timestamp()  
! C6 l1 Y/ [# n 以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回当前日期
) o* ]! R7 z+ Q, m) a时间(根据返回值所处上下文是字符串或数字)     
: s! q( `3 T& \* j+ xmysql> select now();    - G; p4 P" s- _2 ], `5 \7 J
  -> '1997-12-15 23:50:26'    % t' J, W" y8 W; E2 q1 e& _) h
mysql> select now() + 0;    ; H1 C" m+ x$ t+ f  a
  -> 19971215235026    / \( m& [$ F6 E4 n% B, N" o: ?1 t9 F
) d" ~+ a7 Z+ C0 n1 N5 l
unix_timestamp()   
5 e- x3 k* G3 A3 p$ R, q, dunix_timestamp(date)    ! v  Y, J! j3 S2 b, G
返回一个unix时间戳(从'1970-01-01 00:00:00'gmt开始的秒
2 O; _, K% u$ F9 U; v. \数,date默认值为当前时间)  
2 @! a, B* @1 [8 }* j# }6 J" B) xmysql> select unix_timestamp();    ' z* b8 `2 |* }2 d. t
  -> 882226357    9 y/ ~0 s: d3 T; Q
mysql> select unix_timestamp('1997-10-04 22:23:00');    ) W# M! Q& W. A) L+ ?1 I% p
  -> 875996580    3 N1 {9 d$ c: z: `% K! S
: f; X, q- s4 Y2 u4 g5 f2 i, [' U! ^
from_unixtime(unix_timestamp)   
; ?7 }7 H- Y1 e8 I' f以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回时间戳的
  f9 k9 ^" j0 K( H值(根据返回值所处上下文是字符串或数字)     
: g% v. k% x7 u, X& mmysql> select from_unixtime(875996580);    & A+ Q7 j) h3 c0 u( y
  -> '1997-10-04 22:23:00'    8 C+ I9 m% W* N( ?6 }3 i( l4 p
mysql> select from_unixtime(875996580) + 0;    / A3 b+ L2 z. M: g' Q  U
  -> 19971004222300   
0 a8 _& Q  m8 L
( i7 F8 Z- [( Lfrom_unixtime(unix_timestamp,format)    7 n3 L  s5 y" e+ U* B
以format字符串格式返回时间戳的值  1 J, L& W& V7 _  o$ m8 e
mysql> select from_unixtime(unix_timestamp(),'%y %d %m %
6 a4 u# {8 {7 {  `h:%i:%s %x');    ! T4 O- c9 {7 s) Z& T, y7 x
  -> '1997 23rd december 03:43:30 x'   
: P0 i/ W8 Q" C! R# G9 u% Z4 B( z( ? $ H8 o8 h7 b$ v. j' z  t) p* t
sec_to_time(seconds)   
  t6 m- ?& B$ E. R$ n1 ~以'hh:mm:ss'或hhmmss格式返回秒数转成的time值(根据返回值所处上下文是字符串或数字)     1 d- s1 m( P, \) r4 @7 P: h/ ~$ [; x
mysql> select sec_to_time(2378);    , \$ P) [) X" I$ I; p( t
  -> '00:39:38'    0 s1 M# w9 k- i  F  J
mysql> select sec_to_time(2378) + 0;    2 s2 d6 A8 q8 h4 |. U
  -> 3938   
3 S$ J  }- A) P % k9 A" `  e! y. ?9 Y8 L
time_to_sec(time)    ! [: D5 E* b% g& r* P; j$ K
返回time值有多少秒   
! h& c4 e/ J2 `- y4 Kmysql> select time_to_sec('22:23:00');   
$ S  {! V$ i9 H  C; q" t  w/ ^$ U  -> 80580   
  j6 D' X9 c3 i" I2 ?2 Smysql> select time_to_sec('00:39:38');    0 D( ^0 J! O6 b
  -> 2378
( ^, y; G/ y9 H% }/ t, { - r- T3 ?! H+ }+ I. a% k
转换函数/ _! t2 N2 @: s  Y
cast% q( U4 d; a+ p4 B3 s$ t% a
用法:cast(字段 as 数据类型) [当然是否可以成功转换,还要看数据类型强制转化时注意的问题]
/ S9 R8 t' m9 {6 }! B  o5 f$ z! o实例:select cast(a as unsigned) as b from cardserver where order by b desc;
5 H7 [0 h0 G* gconvert:
/ `+ a  J: D* j+ \& {0 m  D用法:convert(字段,数据类型)
6 N6 x* u* x2 q7 \+ X5 N实例:select convert(a ,unsigned) as b from cardserver where order by b desc;
, C- E, |. G2 U2 m0 ?
回复

使用道具 举报

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

本版积分规则

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