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

mysql常用函数大全

[复制链接]
跳转到指定楼层
楼主
发表于 2018-3-21 16:07:02 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
1、字符串函数
  R7 Q" @7 q5 Y$ N& O5 s( fascii(str)   
9 `8 z4 Z! i- K. U* U- L+ Q. ~返回字符串str的第一个字符的ascii值(str是空串时返回0)  
5 @; F2 V) J3 n' t6 `& Xmysql> select ascii('2');  
. k7 E( e1 y# P' l" a  -> 50  8 b5 i! X/ C0 L; i# P
mysql> select ascii(2);  
1 H1 {6 M  x* D7 c1 m  L( e  -> 50  . S0 g) d% ?8 H+ y3 v* R
mysql> select ascii('dete');  
! i- Z* Z) f1 ?- w  W- ?! j  -> 100

. T9 W0 J) B5 g$ w5 U6 cord(str)   - F& q0 P& P( D" _, s
如果字符串str句首是单字节返回与ascii()函数返回的相同值。
8 a) O- z3 L1 h9 n
2 v2 j! z1 F$ |, T5 K9 s如果是一个多字节字符,以格式返回((first byte ascii code)*256+(second byte ascii code))[*256+third byte asciicode...]  6 p5 g0 e/ N: J' o4 g; f8 }# A1 E
mysql> select ord('2');  ' s' B0 h8 s% W0 g  J( l0 H  m
  -> 50  
$ t9 a! t3 N, p$ Y7 V( R   
& e+ m! g8 b7 M. xconv(n,from_base,to_base)   
. a" L5 g4 h5 i5 d对数字n进制转换,并转换为字串返回(任何参数为null时返回null,进制范围为2-36进制,当to_base是负数时n作为有符号数否则作无符号数,conv以64位点精度工作)  
( L+ v) a  }1 d% F' F5 L1 kmysql> select conv("a",16,2);  2 V& w& Y$ }" i; d  U/ V0 J& K
  -> '1010'
. ~2 `5 F* v) S) _8 z' h- V; t# Kmysql> select conv("6e",18,8);  4 y5 j3 ^. I3 ]8 c: O
  -> '172'
- k% x6 y3 m8 \0 wmysql> select conv(-17,10,-18);  & V8 t& E+ q  @1 l: d
  -> '-h' ! N' Q6 q9 r0 M4 S
mysql> select conv(10+"10"+'10'+0xa,10,10);  
. x0 Z2 B. ~. S9 e: k3 z9 D7 ?  -> '40'
  V# u% T; N( X; q; b4 X/ \   
- X3 ]- Q0 L& Y$ }  Q3 bbin(n)   2 _, u- B7 Y+ O! X
把n转为二进制值并以字串返回(n是bigint数字,等价于conv(n,10,2))  . S! j; ?- {  E1 y
mysql> select bin(12);  
5 m0 i1 n5 l. u( Z  -> '1100'
& T  e4 K9 |* _8 l" @
! ^% f2 U7 b! B  w- ^1 \' ioct(n)   
; g9 c& Q$ e' K4 M0 o2 B把n转为八进制值并以字串返回(n是bigint数字,等价于conv(n,10,8))  1 c3 w# R# K8 U5 q; @! |
mysql> select oct(12);  + N* H3 w2 d3 j$ R. _$ D
  -> '14'
. |9 q& l  _( w- O8 L" C   
3 S4 l5 h6 V, i4 c1 uhex(n)   ( D6 Q6 g/ h: V- g0 w3 w' _5 k
把n转为十六进制并以字串返回(n是bigint数字,等价于conv(n,10,16))  
8 R; u9 @- n1 tmysql> select hex(255);  . k) ]) ]4 ~4 U9 p2 p" @- J
  -> 'ff'
' g& F! w0 r9 k, W   
  P5 J+ q: y, n' c1 R& Wchar(n,...)   
; G5 A% b: z# ]5 \返回由参数n,...对应的ascii代码字符组成的一个字串(参数是n,...是数字序列,null值被跳过)   
4 a5 @7 J2 }1 y1 a3 |6 z% C$ w5 Fmysql> select char(77,121,83,81,'76');  - G& i3 Q8 x8 W0 G  }9 R; b1 _
  -> 'mysql' ; C* G3 h8 }# D$ p1 x
mysql> select char(77,77.3,'77.3');  5 S/ l2 U5 z2 P
  -> 'mmm'
( m7 F3 q! N3 ?# H& o" T   
+ ^( u5 _* Z* ~" J8 wconcat(str1,str2,...)   6 Z1 q0 E$ N* Z1 t, T7 r8 N& s
把参数连成一个长字符串并返回(任何参数是null时返回null)  ) R9 M, C0 R3 H4 W$ v# c. ~
mysql> select concat('my', 's', 'ql');  
- I! I( \. n% N; t0 a  -> 'mysql'
& U: ]3 n/ ~  O* {2 y: h9 bmysql> select concat('my', null, 'ql');  : U3 M6 I0 ]4 x) P
  -> null $ |/ {7 x$ W/ R: [6 `: |  w* c
mysql> select concat(14.3);  . y0 D( ^, h9 r$ j
  -> '14.3' / ?- y" _7 c8 h3 i9 ^1 q, p

5 g5 c, x' [/ A! Mlength(str)   
1 d3 b6 E2 {& T  R1 L* |$ q  A% Foctet_length(str)  
7 ]) p) X" H& V7 d" E# tchar_length(str)  
% i1 p6 {( {) R; V8 s8 Acharacter_length(str)  8 d0 ]$ t  T6 G0 D8 S6 U* y$ R
返回字符串str的长度(对于多字节字符char_length仅计算一次)" q" |6 p# o1 @+ B( x2 \
mysql> select length('text');  ) ]/ l2 J) @) ?0 ?$ q
  -> 4  
" G4 {" I! W& F; y5 Gmysql> select octet_length('text');  
- S$ O8 |5 f$ [  -> 4  $ U" i8 Z, b  L: [/ r0 U$ r
3 Y! r! O- Z" P3 a% Y: O
locate(substr,str)   " {! {; n1 W/ Q2 j9 [0 R. r% Z
position(substr in str)   
0 F2 h+ F9 v8 a+ t2 n3 x( E3 ~返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)    k1 ]; i" B$ \& m8 }3 ^( s# d$ o5 C
mysql> select locate('bar', 'foobarbar');  % V0 p1 v* {* G( g  K  g2 g
  -> 4  " _* ~* f$ \. N0 x
mysql> select locate('xbar', 'foobar');  
4 ]( ?9 Q( Q5 h+ ?# g5 s/ k  -> 0  ' ?* h2 M2 t& \; `1 Z3 u7 n
   
- ~4 N- k9 C/ h# I, \locate(substr,str,pos) 1 K- B' O  ?/ h& L
返回字符串substr在字符串str的第pos个位置起第一次出现的位置(str不包含substr时返回0)  + m8 w4 b+ f, y0 l
mysql> select locate('bar', 'foobarbar',5);  
/ ^4 V9 Z8 x4 o: S  -> 7  
+ M& A8 I- O, t* J( K
" _, {% u. O* s# q" \# ninstr(str,substr)  
8 }( n) d+ X& X  ]0 @$ |返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  
' B6 F6 B4 s: y, n7 [% Kmysql> select instr('foobarbar', 'bar');  
/ o3 ~* v2 R: B2 K) @9 v* n4 p9 w  -> 4  1 E$ l1 u) \, Y: W
mysql> select instr('xbar', 'foobar');  ; f/ p1 C; P% Y2 D9 ^5 Y
  -> 0   
8 p( C% x& F6 _" }- z
9 B4 ~$ h: s7 V. e/ {lpad(str,len,padstr)   
. p0 H8 w: X* D9 @3 u1 P# q+ |用字符串padstr填补str左端直到字串长度为len并返回  
# d) n+ Z# o) k5 lmysql> select lpad('hi',4,'??');  " z6 t; t2 o) w
  -> '??hi'   l4 ^- @: J7 A/ d
   
' a$ i+ R: [, j' L6 crpad(str,len,padstr)   
4 w7 e& e! l- f0 m2 d( q9 q" F4 o用字符串padstr填补str右端直到字串长度为len并返回  
1 b. _! H  f  e6 {$ ^' \" qmysql> select rpad('hi',5,'?');  
7 g: z6 n0 i9 H0 ?  E9 `  -> 'hi???'
8 }8 f7 ], y4 e5 F, I+ }8 ~ 0 L0 _* ?9 Z! n
left(str,len)   ( r% P6 A. A) N5 v1 R
返回字符串str的左端len个字符  4 B" c+ B4 \. R  O2 g% M3 P0 T4 C
mysql> select left('foobarbar', 5);  
  p9 i3 P- K1 k2 g  -> 'fooba' * s) K% ~: T+ M% Q' C6 H; v

& h# Z+ _. e; x  v" |( N! [4 Qright(str,len)   
7 o$ j* z- @5 P' R返回字符串str的右端len个字符   
+ [' w9 }, x- I/ nmysql> select right('foobarbar', 4);  
- ^/ h$ J$ \: c9 d; T  L  -> 'rbar' 1 v" a7 ^3 H5 w# B& y
2 {3 N& G5 k# d# z
substring(str,pos,len)   
+ f% P5 c) b- y) M7 S" K* ^, Y) Nsubstring(str from pos for len)   3 U8 d$ x. v9 D5 r% @* h
mid(str,pos,len)   
: J: B! z5 e; M& f: v; z8 ]# }返回字符串str的位置pos起len个字符mysql> select substring('quadratically',5,6);  
5 @4 c9 p. p6 A  S$ o  -> 'ratica'
/ Q. j6 o( _0 b! e0 H8 _" z
  e  ?: _6 H, ?  J/ csubstring(str,pos)   5 g; r) [7 c. a* E  C' u9 W
substring(str from pos)     M  J7 a* B2 X
返回字符串str的位置pos起的一个子串  4 e# l! E' Z4 m* U6 N9 z
mysql> select substring('quadratically',5);  - v4 R% ]' j' r- f  W, R
  -> 'ratically'
4 X; w# P: n1 v* Ymysql> select substring('foobarbar' from 4);  $ m; p& ^/ z" M6 F, M
  -> 'barbar'
8 j5 n; R6 S8 ^6 p8 o, f
) l* p9 ~- M: P. {substring_index(str,delim,count)   
+ D$ u8 P/ _. v4 {3 Q7 q( |返回从字符串str的第count个出现的分隔符delim之后的子串
1 v0 O8 Y3 R. C6 g4 h; @(count为正数时返回左端,否则返回右端子串)  - {4 m8 B  n2 q: S
mysql> select substring_index('www.mysql.com', '.', 2);  
' ]# W* f( e+ j; C2 D# u$ Y4 z# U  -> 'www.mysql' $ Y* z8 K* e% Y3 q9 ]; P; Y/ s
mysql> select substring_index('www.mysql.com', '.', -2);  + }: v+ D  |7 B4 W1 A' \2 M
  -> 'mysql.com'
* I% L* T- D5 Q
  g3 \6 L" {- \" Ultrim(str)   
$ L! b) o+ k/ r, h返回删除了左空格的字符串str    c8 ]% Y5 o% R* X
mysql> select ltrim('  barbar');  
% ]4 D- o: C- G1 A" h  -> 'barbar'
* s5 K8 ^2 _5 C( I& l9 w" A1 h/ e6 g- x: H
; e0 E! c4 v' G8 W% y2 wrtrim(str)   
1 l  s& I$ m; |& b( p+ r: u: E返回删除了右空格的字符串str  
1 ^8 g- ]5 k7 _( f- Mmysql> select rtrim('barbar   ');  / \% u* {( O' c1 }% o0 J" K) f
  -> 'barbar'
9 D( e+ D" n6 S. j5 N 2 a+ F) t. J% h3 q+ R
trim([[both | leading | trailing] [remstr] from] str)   3 K) Q4 d! a2 ^6 S6 U1 i* @
返回前缀或后缀remstr被删除了的字符串str(位置参数默认both,remstr默认值为空格)  
$ E1 [$ V7 N% _3 Jmysql> select trim('  bar   ');  
5 }( ]- }$ Y8 [1 I+ ^  -> 'bar'
% `/ k# |0 m! L$ }  Z. ?1 Xmysql> select trim(leading 'x' from 'xxxbarxxx');  , G. l( J: R! U( v; m5 u3 m/ n, A
  -> 'barxxx'
, S4 n' Q" e8 r( K, z( }% q2 nmysql> select trim(both 'x' from 'xxxbarxxx');  
% v/ X4 m1 `) P- j3 F2 M- p: S  -> 'bar' 4 y0 `, k( n8 q6 z! C* G
mysql> select trim(trailing 'xyz' from 'barxxyz');  8 m0 w! C  z4 _2 S& e2 F% |
  -> 'barx'
: U1 i- g4 y, V% Y' b  [ ) ^7 j1 l! B. Y8 L+ ~, G$ r' Z
soundex(str)   
. j- K6 ?/ B  e) C& ]返回str的一个同音字符串(听起来“大致相同”字符串有相同的# c" A. V. r2 A% P# [/ Q
同音字符串,非数字字母字符被忽略,在a-z外的字母被当作元音)  
5 O+ \9 k! Y0 c+ I: xmysql> select soundex('hello');  
3 G0 K  ]' \. I  D% ^: O  -> 'h400' * H* u; w& S* o# |3 Z# s# `" `
mysql> select soundex('quadratically');  
7 J7 ]& G" t8 h3 d0 y. {  -> 'q36324' % j- w2 _, O) E4 ?
   
1 G6 @# A) H5 G! \- Z' u6 a! J% ~space(n)   
, J) b) h) w0 S. l返回由n个空格字符组成的一个字符串  3 Z' U  Q) l3 x! F* Z0 A
mysql> select space(6);  . O( \& b! t4 V0 Q, N
  -> '      ' % X* n* W+ F1 i7 i5 O
   - B8 a7 g, g$ o" X" P+ ]* V2 A% M% n( n
replace(str,from_str,to_str)   
- `- K- p' ^3 k+ ^1 R& n用字符串to_str替换字符串str中的子串from_str并返回  4 X& L: K9 f" V* {) l
mysql> select replace('www.mysql.com', 'w', 'ww');  
% s6 q1 a4 n( C5 U6 c) d  -> 'wwwwww.mysql.com'
: y4 k6 i& E; r$ d( E5 V6 T" @6 r$ D( m
3 [' t0 w' y. p. krepeat(str,count)   2 G' t/ _2 c( D! V& U9 ?
返回由count个字符串str连成的一个字符串(任何参数为null时- @3 V2 W& a3 T' r* N* s9 h8 ]
返回null,count<=0时返回一个空字符串)  
  ~' `/ j2 f' U1 }: O$ X7 ymysql> select repeat('mysql', 3);    d& y% G4 k( X" \+ t! ?
  -> 'mysqlmysqlmysql' / g  _& c+ [% L5 ~5 d! @
   ' ]8 n3 R$ d3 X0 ^) ~8 w( s) e* b( o
reverse(str)   
- e& f7 A  h# z2 t颠倒字符串str的字符顺序并返回  
9 m+ F9 G: ^9 imysql> select reverse('abc');  
' b. d" F) D( m  -> 'cba' + ]5 p+ T/ R1 T" p
3 i- \3 t- E5 a2 f% ^( v# A/ X. y0 [
insert(str,pos,len,newstr)   ! H5 B; u5 Q( z: u+ }
把字符串str由位置pos起len个字符长的子串替换为字符串
! z* W; L, t9 q) g# hnewstr并返回  8 c7 A( c0 H1 ?: L  Y9 X7 P
mysql> select insert('quadratic', 3, 4, 'what');  
- T8 D& X9 F# L7 v7 H2 o# r  -> 'quwhattic' : z8 ]' [- I5 |# B4 a% t$ a  M

% R$ j# ^- U5 \$ felt(n,str1,str2,str3,...)   
& _4 [* `7 y4 N0 {9 X9 T返回第n个字符串(n小于1或大于参数个数返回null)  
  A4 ?0 l  P% W/ Q  qmysql> select elt(1, 'ej', 'heja', 'hej', 'foo');  * U5 G! W0 Q9 V$ K! p9 a$ N2 b# `
  -> 'ej' ) a; c% t9 c" {  n8 A
mysql> select elt(4, 'ej', 'heja', 'hej', 'foo');  - i( v- s( p+ w( I9 T% r
  -> 'foo' * @' d; G) y# T- b  ~& S) G
" W) x" ?8 y7 W' K8 y8 s
field(str,str1,str2,str3,...)     m$ r$ g$ Q! ~  x! B- |
返回str等于其后的第n个字符串的序号(如果str没找到返回0)  
2 P  b  q6 \, |( V7 Umysql> select field('ej', 'hej', 'ej', 'heja', 'hej',
2 Q' j" B& u9 N! W" ?'foo');  2 b. {& o/ Q$ w2 F. \7 y
  -> 2  / B. s4 g$ ?4 j  l$ m' A, S
mysql> select field('fo', 'hej', 'ej', 'heja', 'hej',9 h* U1 p# |; u- q9 _, x
'foo');  
# T' O  e( e5 \& H  -> 0    H1 N# T# p' Q$ d: ]

: ?. X. H4 k( Kfind_in_set(str,strlist)   ' `; }8 M. o$ u1 R
返回str在字符串集strlist中的序号(任何参数是null则返回% R8 Z9 J& a7 h  _- ~
null,如果str没找到返回0,参数1包含","时工作异常)  
* \* V( e% P$ V  imysql> select find_in_set('b','a,b,c,d');  " l) ~! W; j4 `3 V) R
  -> 2  6 F  i. u' T9 K  L6 J6 \
   
+ |+ @0 `& [% Y$ Qmake_set(bits,str1,str2,...)  . ~# N: n- `' Y
把参数1的数字转为二进制,假如某个位置的二进制位等于1,对应
! ~1 A( j, ~* E! f) f8 ~( ~/ ^位置的字串选入字串集并返回(null串不添加到结果中)  
% X6 i+ |5 ]4 G7 f& s! F8 {mysql> select make_set(1,'a','b','c');  
- C7 ]& X0 ?8 i4 r. o5 a* R9 H  -> 'a' 5 [  ^. e: D& F) Z& `' }
mysql> select make_set(1 | 4,'hello','nice','world');  
* n+ Z) P, W3 I# }  -> 'hello,world' 7 U) ^' d( N* ^8 i  \- f
mysql> select make_set(0,'a','b','c');  ( ?" @4 C! @" J8 a
  -> ''
4 S8 g& j" {' \4 G
6 V+ f9 M8 B  }- |export_set(bits,on,off,[separator,[number_of_bits]])   + @/ N, N% D0 J1 E' z' d* ?
按bits排列字符串集,只有当位等于1时插入字串on,否则插入
3 @% [3 N5 k0 k+ f# ]: ^9 I- ooff(separator默认值",",number_of_bits参数使用时长度不足补0
; P  K0 H/ u# q; ~5 a4 m0 y而过长截断)   
) V, H# q. w. L2 mmysql> select export_set(5,'y','n',',',4)  * w4 i$ O# D* M' Z; s! X5 l+ v
  -> y,n,y,n   
2 b2 Z& z* Q' S3 b9 B* e. I" C , C) D& J' K0 m' t& l, b% K
lcase(str)  & [) Z' a3 E0 z( Q
lower(str)   
. l+ A# D/ J& J7 S返回小写的字符串str  
4 T. u3 T6 |$ s" a: L* K/ {2 _mysql> select lcase('quadratically');  
2 N3 T- g4 [4 ^& e  -> 'quadratically'
% l3 s% D, D4 T  P3 [   ( r3 W/ I  [# X  ?
ucase(str)   
0 Z! H" x* Q9 O3 e3 b5 Xupper(str)   - W) J( z% B8 ~# V) ?6 U; p" i1 X
返回大写的字符串str  6 m/ T; i1 _3 r7 v/ Q) K
mysql> select ucase('quadratically');  
" I8 w# [% L7 r0 m  -> 'quadratically' 0 o2 g- F4 R* w$ I! u, ?$ I

# Z/ |5 _9 A" X! J5 x# e# S8 {load_file(file_name)   2 r8 w. d! T% ?7 _7 u: A" I
读入文件并且作为一个字符串返回文件内容(文件无法找到,路径" z7 a( G  N% B5 J& e" ~" ^
不完整,没有权限,长度大于max_allowed_packet会返回null)    o! B3 o! c. t6 m4 t: x
mysql> update table_name set blob_column=load_file
. e6 C  G% d% F2 ]9 S7 K  q  }("/tmp/picture") where id=1;  
( i  p1 d* ^- M9 S& S$ W $ [0 V- o, u1 \% }& l0 W0 o- s9 r
2、数学函数8 j+ y: H$ _+ H& N
abs(n) & R% H9 T1 o: T. C
返回n的绝对值  / g" ^& w, J+ n2 @
mysql> select abs(2);   
$ q2 `3 @8 m9 k% V  -> 2   
! V7 K4 S' G) e) h9 Umysql> select abs(-32);    ! D, Z5 p+ E6 O5 z0 }$ E' ~
  -> 32   
' l' x4 L2 k  e/ O, Q   1 B, B) f2 b! E# _
sign(n)  ( E6 A' I4 X* \; r+ {
返回参数的符号(为-1、0或1)  ! p' _) z% ^+ ?1 O0 E+ P
mysql> select sign(-32);    , K8 D& e3 R0 Q- n" g: V8 [$ V) D7 a
  -> -1   
( `/ b$ x1 \' A! k% tmysql> select sign(0);    7 n5 w0 M" d7 j/ I
  -> 0   
( K. b5 i. }0 u; B9 w. i* B9 i6 imysql> select sign(234);    ; l% Y4 v3 y: N. `3 e4 a, u0 `, T
  -> 1    * b( p8 L$ {2 D% W) Q# ?* o
  x5 X9 C3 u6 i/ @2 p+ l
mod(n,m)    5 g) c" V4 Z& `- U' |1 }
取模运算,返回n被m除的余数(同%操作符)   
7 a9 q8 I  W* r' @- a5 Bmysql> select mod(234, 10);    ' J" G; _7 k6 E/ g2 ]0 R
  -> 4   
& z! \& ]# ?5 p+ d+ zmysql> select 234 % 10;    % P( N, K1 e1 G% _% m  C
  -> 4    $ R0 W' |: p" R2 T/ ^
mysql> select mod(29,9);    / d$ M+ y* b% ?; O" ^! n. B/ c. A
  -> 2    ( Q+ G8 O& S0 c8 a8 X

4 T$ {( m; D  h1 b( `$ t' c1 T4 S0 gfloor(n)  
- L+ S; C/ r4 u; F& |3 \+ W/ H: x返回不大于n的最大整数值  " K$ [# Y$ Z7 j  v- t# S
mysql> select floor(1.23);    ; k& w. @3 E. n6 K
  -> 1    ; k; J3 W( ?* S& ?) G( V# o/ Q
mysql> select floor(-1.23);    " G/ x* c2 K/ j1 p5 H; P5 N% l
  -> -2   
# C/ H& h3 b5 t1 T; d5 G   A- _7 P/ ^8 j: @- p
ceiling(n)  6 C; }* J# `9 l9 u* x
返回不小于n的最小整数值  
& Z& `  y  U5 F0 I' Zmysql> select ceiling(1.23);    + g. @4 G* F# ~' l
  -> 2    1 }/ W) r0 P9 }% x  R1 _
mysql> select ceiling(-1.23);    1 |% V! X! R) j5 a2 Y! c3 `4 M; A
  -> -1    7 T8 T4 i0 \& `! S
; l, K, {7 ^% N" \
round(n,d)  
# ?  Z3 n( ]; X0 Y8 h3 g返回n的四舍五入值,保留d位小数(d的默认值为0)  
! a* o4 E& z- J( U: c: i4 p! {mysql> select round(-1.23);    9 r2 [$ ]& o- V( z/ _
  -> -1   
  I% A1 z6 R: W! g7 j+ amysql> select round(-1.58);    ! X/ Y. C( j7 V0 z3 p6 p
  -> -2   
) E/ z4 C  s9 w3 k$ Emysql> select round(1.58);    - Y) |* |+ Q* `+ n, B0 |
  -> 2   
9 P7 [- |3 ]4 B" `: x" S( Pmysql> select round(1.298, 1);   
+ G' t, ^* _& e  -> 1.3   
' F3 o1 X5 x" N; N, g% jmysql> select round(1.298, 0);    ' u& w$ G6 r# m0 `; L0 M
  -> 1    2 S, U! [: ~6 c4 i1 `$ V- [
; H! U& C' r7 L" c+ Y! K7 e: n
exp(n)  
% ~9 T0 Y! a4 E2 @" I2 h返回值e的n次方(自然对数的底)  
4 z. D- v, o) J" ^& ]4 [/ ~mysql> select exp(2);    9 J8 [1 Q4 k/ r! W
  -> 7.389056   
3 n( g% }: ~) J5 Amysql> select exp(-2);   
$ T& N. Q# l! }, y  -> 0.135335   
( N* i! W8 |( v5 ?& A  ~, b4 n 1 t3 P: ~0 ^7 p% P% C
log(n)  
* E, v! U$ R: ~- S# _4 b返回n的自然对数  
$ C0 ~$ l0 l8 s  m; ?5 Q( smysql> select log(2);   
1 u4 ~- ?+ h' i* H! j  -> 0.693147    / S3 T1 b( f3 q( g  L. j7 ?/ d( K
mysql> select log(-2);    ; r: q1 G0 d, v
  -> null    8 ~, q9 ~) {0 i

- v* e: e/ b5 ]log10(n)  
' v# g* h) p6 f, k8 ~; \返回n以10为底的对数  
- K$ T0 P. [$ Imysql> select log10(2);   
- K0 r+ ?& y. h4 d) A2 P  -> 0.301030   
4 q* w& B  C! \3 T. g4 Hmysql> select log10(100);    2 W1 E1 v$ V* e# e- Z& a
  -> 2.000000    ! `- l. `  `/ H+ K, p+ r
mysql> select log10(-100);   
+ s. z* T9 p  D$ }3 i; H2 m8 N  -> null    ; E1 k+ O7 U5 v. H% A. g% h/ U
, x0 ]6 Y) l, P/ r' ~4 C, f3 U
pow(x,y)    3 e8 ]% o( ]8 O. L
power(x,y)    3 I3 y0 u" T, G3 ?' o, m
 返回值x的y次幂  
* G% R2 B5 A; f- _6 _$ ]- O: jmysql> select pow(2,2);   
* y4 x- U3 \- K+ k0 R# v: e  h, X! r2 ~  -> 4.000000   
4 O  ?3 p6 o5 _' b; fmysql> select pow(2,-2);    / L" M* t" A5 F' M
  -> 0.250000  * o* c, F( i; q3 H: ]+ }; _( r5 A
3 Q4 i( p' p+ ~+ C6 H& J% N, G4 o
sqrt(n)    \6 F" T3 M; H8 ^; J2 ?% Q; O
 返回非负数n的平方根  / j5 @8 r1 \: G) a9 {0 x
mysql> select sqrt(4);   
& l9 f6 |& [& g; K# }7 g  -> 2.000000    * l6 r% H0 f* u9 {
mysql> select sqrt(20);    ; P. ~" S9 Y- d% C  i* |
  -> 4.472136    ( z* @# C: j9 A" @" h% \" A
" f3 k  K! {4 |. J
pi()    + s: m: L$ g1 X" t. m: R- k
 返回圆周率   
5 T4 }0 f' ]  E' v3 omysql> select pi();   
' s7 N% m( s$ w; a" f  -> 3.141593    , e3 ~" [1 y& P) [

* [3 g3 c) O8 Kcos(n)  # h6 N8 a5 l6 Z# W  d* p
 返回n的余弦值  
0 O! I) |* L5 d- I( Xmysql> select cos(pi());  
9 Z7 ]2 U6 G: G6 y6 f  O, h  -> -1.000000   
2 S7 e) `+ M$ o ) Z1 j# k4 S. c% |4 D, @
sin(n)  
& l) Y0 `2 [, J- |9 S# D 返回n的正弦值   
+ b# H! J6 r8 s! Vmysql> select sin(pi());   
# Y& k* _& q9 p5 e, ^  -> 0.000000    1 {( Y3 J% X8 x2 Y; B. u, l' S: o

8 Q" Q5 s% I1 m; [: ], utan(n)  ( _  G: m$ @- ^! `
返回n的正切值  
$ W/ h6 k$ N$ p0 d0 X% E& Vmysql> select tan(pi()+1);    # z1 m: c8 k: k  ?9 Q9 t4 {8 `
  -> 1.557408    0 g4 z1 k4 p& T# J

' ]+ |+ t0 y0 ^: A1 B8 jacos(n)  9 |0 H; b2 P% q0 X
 返回n反余弦(n是余弦值,在-1到1的范围,否则返回null)  
- c1 W/ U- P  f- y. z+ D. f& dmysql> select acos(1);    1 v! P" h9 A1 M$ D6 l
  -> 0.000000   
4 e+ C5 @# |2 H! \- ^$ |/ }1 o. Bmysql> select acos(1.0001);   
9 S4 |3 ]. r( h  -> null   
6 i! \7 \7 p4 c7 r; q( Nmysql> select acos(0);   
2 y$ Y% y  B0 |5 ]  -> 1.570796    8 u( Z# b- k4 P% j

5 z8 G. B5 ?+ ?/ S9 ^1 G: yasin(n)  6 i, C0 i: x: \' x6 {  a
返回n反正弦值  
6 w" U& N6 C, H$ _mysql> select asin(0.2);   
' I3 M# F% t) }: g8 z+ ]  -> 0.201358    ( s. t- n0 ^) a2 {
mysql> select asin('foo');   
* r+ K; p, T7 e- g7 s' C  -> 0.000000   
  N  _$ N# N6 R3 G/ I 1 K7 h7 s  n( N; g% e$ Y
atan(n)  : h! C  j4 J* A) I5 J( c
返回n的反正切值  3 C- V, d/ i. _* w' J6 c
mysql> select atan(2);    & i( r% r/ c5 I' W0 f& ~
  -> 1.107149   
" h9 r" |+ c3 M! V6 }& }, zmysql> select atan(-2);   
+ Y( ^5 E9 h  b* w. I" Y  -> -1.107149    ! f. ~' c; y. }5 v* g
atan2(x,y)    ; n) C7 }( R" \8 \2 c" V/ J* {, ^
 返回2个变量x和y的反正切(类似y/x的反正切,符号决定象限)  
* C) ^0 G2 K7 k& R& j4 `+ C. nmysql> select atan(-2,2);    ; T, y9 _1 u6 p2 k
  -> -0.785398   
. |" }6 {+ I7 a* \$ {$ lmysql> select atan(pi(),0);   
' x  @* l9 A) W6 y9 [  -> 1.570796   
5 ]: T& S2 a4 v3 {* R ) y0 T9 N$ i0 c
cot(n)  
( T7 K* I$ R6 G3 M4 b' l返回x的余切  5 S3 r+ g. o* U
mysql> select cot(12);    . D/ Z; y: b3 ]* a' X  R
  -> -1.57267341   
" y/ w: x0 p" P0 d5 {' k- j* Ymysql> select cot(0);   
8 {% x! ^; j0 Z) M  r0 l! Q  -> null    - k5 g: a0 Q$ ^1 d! T
& I  w. s, a; Y) o6 g/ r
rand()  8 Z2 z% e' I+ i! `2 t: q
rand(n)    
2 n) b* }" y; |; V' H' N返回在范围0到1.0内的随机浮点值(可以使用数字n作为初始值)
% B5 r  }# g2 N. ~- A# q
# u" F% ^& Z0 p- n8 _4 n2 P, E  Amysql> select rand();   
. u, F# U% }7 Q+ ^/ W7 R  -> 0.5925    0 `* D& n; d6 y2 G# E$ G1 B
mysql> select rand(20);    7 s# s' j$ X& @# {* j! z" b- c
  -> 0.1811    * E: n7 L# R. |
mysql> select rand(20);    7 a  s) [2 A% B' j! z, o, `
  -> 0.1811   
* x0 i' S+ p) p1 H# c- M& f* Rmysql> select rand();   
) T! a) \4 X7 D8 h  -> 0.2079   
  e, |! T: @5 t  V& Y5 tmysql> select rand();    ; N- l/ k- Y+ ~  g* ]" G
  -> 0.7888    # M' X! x; p! @9 F8 p: D3 K

# C! a0 a* l! q& k) W7 j# kdegrees(n)  4 P( }, H3 O0 u# e
把n从弧度变换为角度并返回  + t, X- h1 e4 R+ d' A' i
mysql> select degrees(pi());    , f" }$ K9 T- y/ B1 J9 C
  -> 180.000000    ! F8 Z6 j! L8 @; u/ |, g
: K2 ^- Q; s% c' N, m1 U
radians(n) 8 M5 F; J) G% @" Z: \- u
把n从角度变换为弧度并返回   / v5 H% w$ ]3 {+ ]  k2 S6 N) `
mysql> select radians(90);    4 B& ~& O# B9 s6 `5 }. x4 `
  -> 1.570796   
8 A# L, m0 A' A
& t1 m* J  w: |truncate(n,d)    ) K9 ^5 c, d2 V0 ~
保留数字n的d位小数并返回  
% q: x0 y: q- N7 w5 z, g  ?mysql> select truncate(1.223,1);   
+ c: r/ }( k7 _) H* o& e4 ~& |# j  -> 1.2   
; W' }' g) M  ^! nmysql> select truncate(1.999,1);    ! |. L/ {. `5 Z+ N8 d2 l2 H, k& X
  -> 1.9    + [3 b0 m8 J  I
mysql> select truncate(1.999,0);    1 {7 J! L+ \7 X. e. ?4 ~8 |) f, `
  -> 1    4 i8 [! w$ ]  ]2 W# [3 F' p7 g
3 A7 R: w! P( s
least(x,y,...)   
2 `( f2 c% B5 N. l! ?* z返回最小值(如果返回值被用在整数(实数或大小敏感字串)上下文或所有参数都是整数(实数或大小敏感字串)则他们作为整数(实数或大小敏感字串)比较,否则按忽略大小写的字符串被比较)  3 n& {* g* I* `9 J" l: j9 ~
mysql> select least(2,0);    + n9 G- c& q* g* ]6 J5 M
  -> 0   
9 {$ _- X* v& {+ q* q7 E  O0 nmysql> select least(34.0,3.0,5.0,767.0);    , W- R' W: Q' N
  -> 3.0   
' q% Q( y0 E  S9 |mysql> select least("b","a","c");   
* J/ e0 M4 \0 _2 f6 |* O( b! k0 _2 V  -> "a"   
' n0 r7 B* c1 V& O
6 A* P3 ]: T/ y  y* Qgreatest(x,y,...)    7 Q  |  h# L: f1 }2 `4 ~
返回最大值(其余同least())  
0 F6 O& n; |: d. J2 Z- ^& W/ V& Kmysql> select greatest(2,0);   
$ i" E9 z0 t# M. @  -> 2   
, f- J0 b' q# C" e( [+ amysql> select greatest(34.0,3.0,5.0,767.0);   
% D0 o; N  E5 L$ {0 ]  -> 767.0    + m  ~5 |; p' [+ m6 \7 {( O+ u
mysql> select greatest("b","a","c");    ' Y& i3 m: b( t* r  c
  -> "c"     
* f( O( Q$ B. G+ _; f( `; M. ?
- I3 C) S; N7 x$ U" }3、时期时间函数 - [* T' y- D! k$ M
dayofweek(date)   
0 y- d/ m& X; l" \7 ~返回日期date是星期几(1=星期天,2=星期一,……7=星期六,odbc标准)  1 F0 K- V, Q9 |. P, D, H
mysql> select dayofweek('1998-02-03');   
; U( V4 A$ I  f  -> 3   
4 u9 I2 d) R1 h: g / q( T" p: y9 c
weekday(date)   
% i' O  I$ j  w: F返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。
2 G0 v; A2 F5 E" Q) x9 \9 ]  
& M* `) @  R  n; r1 d3 Hmysql> select weekday('1997-10-04 22:23:00');    9 g) W  X% x) d7 ~. ?4 W2 z. X$ s
  -> 5   
1 ]9 L5 m/ H6 Rmysql> select weekday('1997-11-05');    . i! Y% B8 P1 E! F1 x
  -> 2    * z1 W5 Q5 I6 k( V0 L+ |$ T- J

( X' T+ o/ O- E# t. }- Hdayofmonth(date)    ) W& W9 x6 L* i
返回date是一月中的第几日(在1到31范围内)    . k: e7 W5 E6 y9 H# z; b
mysql> select dayofmonth('1998-02-03');   
4 T$ K9 a% |  _& B, ~  -> 3   
3 ~' D% u# n: R2 N
5 [7 a6 F7 ~7 G" _3 B& xdayofyear(date)   
' R& f3 l: _, c5 ^返回date是一年中的第几日(在1到366范围内)    ) ~$ _! b1 H7 d+ y1 O) E
mysql> select dayofyear('1998-02-03');   
" P) y1 ^" Z* P0 e; ?3 J  -> 34   
! r+ M2 R6 q* j
& P) V2 I+ V6 f4 e) V9 Y! p3 I' \month(date)   
( G. o# E- ^9 d3 t) R返回date中的月份数值    # W7 k2 a, g# p6 E- T. ?
mysql> select month('1998-02-03');   
7 s# C7 z/ O8 X, i4 g7 h! v% j( P  -> 2    ; P% V  I0 ]9 _2 x$ v+ L

* G1 C9 ~$ ]$ k  o& }- p' K9 vdayname(date)   
$ m7 P/ ]( M2 G- H0 x: z! i" C返回date是星期几(按英文名返回)  
7 _, K6 R0 h0 u" T, xmysql> select dayname("1998-02-05");   
' \  T# R2 q7 s  -> 'thursday'    " o+ C& q! e$ C, ~$ O
: f: s9 K6 Z8 I; `4 g
monthname(date)    ) _( ?; a/ B0 q! S6 K
返回date是几月(按英文名返回)  
0 L8 ^* P1 ~" G' p. Vmysql> select monthname("1998-02-05");    0 d7 t: g, a- [' H% X  J/ w; w
  -> 'february'   
" U1 K9 R) T2 e2 i. N" K  T
/ |# b3 q* e# G6 W! e' B5 Gquarter(date)   
  V. O9 M* J1 H% v- _返回date是一年的第几个季度    6 o: _) a" W2 a" Z4 u$ g
mysql> select quarter('98-04-01');   
4 L4 q; ?! Y$ S1 M: X  -> 2      |  C. k8 H( Q+ y% N
7 A1 N0 K) H- D3 ?6 k3 A
week(date,first)   
6 M: j* C" x( X* u; U返回date是一年的第几周(first默认值0,first取值1表示周一是% D+ T! t6 e2 e9 @
周的开始,0从周日开始)  
" h* b2 V( O" a$ b0 K+ Ymysql> select week('1998-02-20');    7 V8 A: \& N8 ~% g$ j& M
  -> 7   
+ z( Z3 M7 p1 s1 z2 Smysql> select week('1998-02-20',0);    " Z+ a, ]; y6 ?& e
  -> 7    8 ]0 P& t% C- [9 ?- G9 U
mysql> select week('1998-02-20',1);   
4 V3 f7 i6 N5 c& K0 Y7 S1 ^  -> 8    3 p) ^9 N: k' a
" C% f$ N6 U* o% \
year(date)   
' ~9 I4 h, j% {9 H' {返回date的年份(范围在1000到9999)    2 _7 ]9 x, M) _8 D9 y% c" k+ n
mysql> select year('98-02-03');    - j6 R5 o4 t* ~! |
  -> 1998    2 t1 A; b" D" A6 X

0 o2 F0 p" ~5 q! L, Rhour(time)    
) w  `  w5 T3 ^/ z' l9 K返回time的小时数(范围是0到23)   
9 v# P) F7 c: @mysql> select hour('10:05:03');   
( {. ~1 U6 |& ]  -> 10   
- q' g$ w# J! R6 e" S0 f2 ^: }
/ [% l/ u2 S: R7 ]1 aminute(time)    , _$ {+ @- b- |' C7 e9 f5 P# q
返回time的分钟数(范围是0到59)    5 j8 N3 R# u. {* J$ v, v4 P0 w" ?
mysql> select minute('98-02-03 10:05:03');   
+ `& J3 h; o, B9 F. q7 [: J3 H0 E: M  -> 5   
8 u& m* h+ p+ {: a $ q5 D$ T" F( }1 Q$ [( m3 k
second(time)    
2 X- C1 R  M+ H9 D% S3 x$ s返回time的秒数(范围是0到59)   9 J4 j/ V: C( l. r% P
mysql> select second('10:05:03');    0 ~5 F; }- a" @7 ^
  -> 3    ( a6 |) b; g# q
% w) y+ B$ g5 H' x: X
period_add(p,n)    2 M0 O! m( j. q$ R4 Q* Y4 K- ~
增加n个月到时期p并返回(p的格式yymm或yyyymm)   
2 G" ^/ X- n, R- W/ T, pmysql> select period_add(9801,2);    . c, O( }" D9 i( s4 O8 T$ P
  -> 199803   
) N$ _$ U! k* h4 X$ |' N' |; D2 Y   T& L1 z. d9 Z8 O
period_diff(p1,p2)    * m* |8 H) _  X) t$ s9 k
返回在时期p1和p2之间月数(p1和p2的格式yymm或yyyymm)  
. l% |+ Q5 t# M( {3 s5 Hmysql> select period_diff(9802,199703);    ' @2 |! j# @6 U' m) u( m  J7 Y
  -> 11    / Z2 i; y9 C+ Q; O2 Q; }4 o7 w5 {

' ?- D+ m( j' G$ z. A7 {9 ^2 q% ]date_add(date,interval expr type)  4 j2 ~4 H+ B- l
date_sub(date,interval expr type)   
+ C/ |! W& @2 D3 M9 A. Cadddate(date,interval expr type)    % I7 V; L: ]' [. T# a( P- F
subdate(date,interval expr type)  
  e' g' L, e+ D( U对日期时间进行加减法运算  6 C7 _9 f( W' F7 _! z* L! f' S0 r
(adddate()和subdate()是date_add()和date_sub()的同义词,也5 r: V9 O" o# [: h
可以用运算符+和-而不是函数  
, V4 ^' N' d# gdate是一个datetime或date值,expr对date进行加减法的一个表: u8 O. z; {6 ]9 z& a% [
达式字符串type指明表达式expr应该如何被解释  ' m$ w9 S. }8 G$ o# e- X
 [type值 含义 期望的expr格式]:  
: _  S$ q+ {  L8 h% S second 秒 seconds   
( Y, i. @  R5 g- K! e minute 分钟 minutes   
+ z# q! X4 }0 x6 X hour 时间 hours   
; H9 t0 B. J! ~4 y4 K, G3 |# ? day 天 days    9 ]+ t* z! J5 Z# t: R. r" p
 month 月 months    8 @/ g  d4 R! D9 q4 E
 year 年 years   
# X. V9 f/ ?' L2 s) B minute_second 分钟和秒 "minutes:seconds"    ; k; r/ W9 J* Y! p0 z
 hour_minute 小时和分钟 "hours:minutes"    ( Q9 N9 L$ B2 g6 g& A) p
 day_hour 天和小时 "days hours"    , J- s7 z0 D5 \* {
 year_month 年和月 "years-months"    1 o; A  k5 u, ^# t. D
 hour_second 小时, 分钟, "hours:minutes:seconds"   
  w* a6 W# H# N: `$ W6 g+ ^& d day_minute 天, 小时, 分钟 "days hours:minutes"    1 A# O; \6 `: k
 day_second 天, 小时, 分钟, 秒 "days0 b* r0 B5 @. k/ d2 i0 o1 p/ X
hours:minutes:seconds"
, T/ `7 M+ S: ^ expr中允许任何标点做分隔符,如果所有是date值时结果是一个4 F% T9 l# g& D$ l$ ~: W
date值,否则结果是一个datetime值)  5 i3 b( M. {; k# V9 w
 如果type关键词不完整,则mysql从右端取值,day_second因为缺  G1 r6 U8 Z6 t# S0 W. Y. g7 P3 b
少小时分钟等于minute_second)  
& f+ s( M8 e! v8 O. @ 如果增加month、year_month或year,天数大于结果月份的最大天
+ ]) i4 W- m' ]8 v; ?. U  k7 O数则使用最大天数)    - \. p( {+ E- t4 L2 u
mysql> select "1997-12-31 23:59:59" + interval 1 second;  
, \& p' _7 c2 O7 Z) k0 L+ s1 e 0 a4 i5 c8 u' S$ |0 I  l. h
  -> 1998-01-01 00:00:00    4 c, F, z5 P# G9 x  x5 Q' s/ K
mysql> select interval 1 day + "1997-12-31";   
' q/ d7 G" ?3 s) j% M  -> 1998-01-01   
4 ]$ ^8 W: V1 n9 |mysql> select "1998-01-01" - interval 1 second;   
# Q0 l7 _/ N. ^9 a- y: G  H. X  -> 1997-12-31 23:59:59    2 f6 ]# L1 q' o" Q+ F1 D
mysql> select date_add("1997-12-31 23:59:59",interval 1! i- y, o) ]- H$ O- g  _
second);   
3 G) r6 g7 O/ x6 Z" o. A  n- v6 L  -> 1998-01-01 00:00:00   
; m' w1 Z4 i  s" y& Tmysql> select date_add("1997-12-31 23:59:59",interval 12 C( G) y9 u0 C% W7 j
day);    . O  V6 q- Q+ {  L. h
  -> 1998-01-01 23:59:59   
7 e" _4 c9 d9 W8 j! j  n6 dmysql> select date_add("1997-12-31 23:59:59",interval
$ \% ?" u, l- `0 [+ J! c; u8 \"1:1" minute_second);   
! N; s" j* H8 D. i) p: l, V  -> 1998-01-01 00:01:00   
3 N. t/ l; E) ~mysql> select date_sub("1998-01-01 00:00:00",interval "14 ?# @  F3 r) M' }" _$ X- y5 O- \5 z
1:1:1" day_second);    1 t( ?6 \/ p# D- X8 z
  -> 1997-12-30 22:58:59    : K$ h' B: W# o
mysql> select date_add("1998-01-01 00:00:00", interval "-1! p* Z: R2 n! b2 N# s
10" day_hour);  . C: a, k. v& {+ R+ Z& {
  -> 1997-12-30 14:00:00   
8 o' T' [% |. M  M( p6 h& \mysql> select date_sub("1998-01-02", interval 31 day);   
: ?5 H# R( V% U" j: F8 d1 m  -> 1997-12-02    5 r. {. M, K4 O( _0 [7 L9 G. T/ l
mysql> select extract(year from "1999-07-02");   
# o: r+ A7 ~7 ?  -> 1999    6 t. ]; t6 ^4 w7 [+ F
mysql> select extract(year_month from "1999-07-02% ^( ]$ @/ D! A
01:02:03");    1 |; Y+ y( i1 n0 S# a. Q4 N
  -> 199907    * |( @! ~1 G5 _! e
mysql> select extract(day_minute from "1999-07-02
# B+ x# r( ^5 ?01:02:03");   
& c8 \& j2 A! y1 Q+ J- X5 G; X  -> 20102   
5 J! A$ i3 U2 d1 G$ v! E/ M , X( d9 N5 q/ b' H; B
to_days(date)    . k& p/ @4 D9 B9 W- `. ~0 ?( f7 u
返回日期date是西元0年至今多少天(不计算1582年以前)  
1 x* D7 y# F* ]: w$ D+ T' Bmysql> select to_days(950501);   
! _( n; W3 C4 p  -> 728779    4 W$ Q( [4 E* X2 D9 v+ H# j) h
mysql> select to_days('1997-10-07');   
* J/ j( s, `4 b2 y. |& S  -> 729669   
8 k8 I. X7 r" m) t. H+ Z, y 6 f, ~& @1 |0 ]/ l3 m
from_days(n)   
  H9 a% h2 f3 F5 V- t' u1 R# O) O 给出西元0年至今多少天返回date值(不计算1582年以前)   
5 T8 H, i5 c: X3 Z: t$ W! ?; omysql> select from_days(729669);    ) X$ C( F( l4 a( k( W% T
  -> '1997-10-07'    9 w) l; K9 m/ q1 e/ A% y
8 d9 X! d4 I+ M. j. K5 G1 F
date_format(date,format)   
4 f; N' o( L6 H5 F1 K! X 根据format字符串格式化date值  
6 x' N- Y2 ?4 \2 o5 K$ t (在format字符串中可用标志符:  
* @5 Z. W8 u6 z  d %m 月名字(january……december)    - b+ b6 n3 n7 f3 O; B+ i- d
 %w 星期名字(sunday……saturday)    1 L+ l9 I. p' J! @6 E9 a3 X* m" Y
 %d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)    2 q* O( M. y5 c* E/ E9 t* g
 %y 年, 数字, 4 位   
( g( Y8 g% l* c; y* x  j/ i$ [7 j %y 年, 数字, 2 位   
6 f+ d% j3 q6 r1 u %a 缩写的星期名字(sun……sat)    3 t- a5 N: ?8 n7 W8 m
 %d 月份中的天数, 数字(00……31)    # K7 \7 B9 J7 I8 P, v2 l# z
 %e 月份中的天数, 数字(0……31)   
3 Q2 w6 K6 ?! C+ c4 j- W: H1 v %m 月, 数字(01……12)    * {% i# Q! A9 a7 q
 %c 月, 数字(1……12)   
) P( b/ J* t; n, c# q %b 缩写的月份名字(jan……dec)   
- m! l6 L6 }! i3 P5 }5 c' [ %j 一年中的天数(001……366)   
9 H, w' W& Z" W. X8 P %h 小时(00……23)    5 m2 ^$ G. M5 v( ^- x/ j
 %k 小时(0……23)   
( @  e% J; R2 g5 g1 M %h 小时(01……12)    $ B; W! Z7 l- W2 ]2 G
 %i 小时(01……12)   
  M) f/ u2 a( g1 w$ C( N$ E %l 小时(1……12)   
5 R, R5 G+ w, W9 k, F; ^4 N %i 分钟, 数字(00……59)    & T# h( B; P; d. S0 S% h
 %r 时间,12 小时(hh:mm:ss [ap]m)   
% f4 P' ?- Z; I( X) a% a' E! P %t 时间,24 小时(hh:mm:ss)   
9 G$ x! L5 H' c6 {2 M$ i %s 秒(00……59)   
, X, F# \6 O5 y) z7 P %s 秒(00……59)   
6 S: C; T2 Y. m/ q %p am或pm    $ H/ L. K* j: n8 d! R, h( r
 %w 一个星期中的天数(0=sunday ……6=saturday )   
4 @* L. O' _0 k# N. O %u 星期(0……52), 这里星期天是星期的第一天   
! H; R) ]4 [  C% {: z %u 星期(0……52), 这里星期一是星期的第一天   
' y& e0 r, r: X5 i %% 字符% )  
8 c& w* e# U2 \3 ]+ ^2 a3 r9 lmysql> select date_format('1997-10-04 22:23:00','%w %m %
$ H* D" g2 d. U$ l5 Uy');   
8 d/ o. D  [: V. m3 S! E2 l  -> 'saturday october 1997'   
: x- @2 g5 _) \7 }6 j2 X, s5 ]mysql> select date_format('1997-10-04 22:23:00','%h:%i:%
2 B6 v5 {* D- q7 ]s');    ( O7 q' L- H- Y* @
  -> '22:23:00'   
) N3 x4 B8 Y4 F- \  ymysql> select date_format('1997-10-04 22:23:00','%d %y %a
5 ~9 n8 z( d) `! J  k9 X5 l; r1 G%d %m %b %j');    9 t$ y; ~' j4 \
  -> '4th 97 sat 04 10 oct 277'   
3 B4 r( H1 A; M+ pmysql> select date_format('1997-10-04 22:23:00','%h %k %i
6 R6 e" T6 x" ?4 K%r %t %s %w');   
, v4 w: q, N& ]6 b/ m. I  }+ x  -> '22 22 10 10:23:00 pm 22:23:00 00 6'   
2 v( v. G* g5 |. q5 J8 Y( n% C ' s5 n2 w6 q) d+ \/ D* c6 ~
time_format(time,format)  
8 b, _+ I  b; O& j 和date_format()类似,但time_format只处理小时、分钟和秒(其
- ?4 C; T2 n4 s+ r4 K1 X" |余符号产生一个null值或0)  $ l/ y9 G, y% K5 X- |% P1 _8 i: b
' t3 `  J; v; F. v* p1 r
curdate()     # @% P0 u2 o& Q7 s0 o
current_date()  2 v" n1 j% @9 o$ w5 [
 以'yyyy-mm-dd'或yyyymmdd格式返回当前日期值(根据返回值所
! }, U, y2 O3 `2 k' K  q3 l  |) T$ @处上下文是字符串或数字)    . n4 U" T4 n( x8 T; e- ?2 ?
mysql> select curdate();    # I$ w; k6 [) A; J4 g8 u" E
  -> '1997-12-15'   
4 J6 s9 k5 U" G) Zmysql> select curdate() + 0;   
5 [1 J) ^% v& q) v  -> 19971215    # m( S5 }# g8 l2 u, \, V; K
, O5 T; f5 [( u- c: X2 X
curtime()      K1 d' w7 Q! u1 u  A
current_time()  & X9 C" U9 R( {9 R' X! Q
 以'hh:mm:ss'或hhmmss格式返回当前时间值(根据返回值所处上+ d; e: O( ]  l/ r+ H# R
下文是字符串或数字)      
' b5 t/ o8 ?, R; Y# |7 zmysql> select curtime();    0 j+ j3 q. @1 q: L8 Y/ `0 c
  -> '23:50:26'    1 \/ w5 n' O/ h
mysql> select curtime() + 0;    , R1 B& p1 c* H' z
  -> 235026   
9 f$ \, I' w, l0 [
, W9 U' Y' W2 I5 {. S/ C9 [now()    1 O4 \& D' }" |2 n+ |5 ]
sysdate()    
* @& P7 J( ^2 h5 mcurrent_timestamp()  - `; O; O. p& R
 以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回当前日期
# S: p2 }( S% y) {+ m时间(根据返回值所处上下文是字符串或数字)     4 v( A. J% k; a/ I# m7 w
mysql> select now();   
' R, q4 k' l: Z) ~  -> '1997-12-15 23:50:26'    " K& @% ]9 g! [
mysql> select now() + 0;    $ C# @; l" ?5 t
  -> 19971215235026   
2 ?% u( [& y- t; p5 D7 G
7 w7 ?: \$ M' A0 G2 Vunix_timestamp()    % ^/ g  [7 k7 ~$ k
unix_timestamp(date)   
. s& D( L; u6 h! W3 M返回一个unix时间戳(从'1970-01-01 00:00:00'gmt开始的秒: r! [4 Q2 D; ^- K2 |# ~+ j
数,date默认值为当前时间)  
/ x5 L6 i: [1 K6 ?+ a5 ymysql> select unix_timestamp();    ; R* r" t5 H2 T9 Y
  -> 882226357    , A) e( ~2 g0 k( B3 U
mysql> select unix_timestamp('1997-10-04 22:23:00');   
' U. K) I. O( `+ }5 b! p  -> 875996580   
/ m4 v/ l) ^2 r5 V( G5 Z" C8 Z4 d9 F
$ i. R0 K% R+ z4 k% U1 V8 tfrom_unixtime(unix_timestamp)    6 I) I  g. N: O3 i! p
以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回时间戳的
) o! W* C$ A" s5 [0 C& X+ n值(根据返回值所处上下文是字符串或数字)     
6 h0 U5 X5 D: Xmysql> select from_unixtime(875996580);    " t0 l+ H2 V2 t$ x: ^: B! P
  -> '1997-10-04 22:23:00'   
5 C; i# F- n& V( ?mysql> select from_unixtime(875996580) + 0;    + j% A% L. I8 D3 b8 l/ w6 x
  -> 19971004222300    3 I, f3 q# ~, r) B" i$ ^$ G+ F8 H

% B/ Z, a2 z/ x5 |" Efrom_unixtime(unix_timestamp,format)    
. o$ g, p' u1 i! A0 Y  t) e1 @: `以format字符串格式返回时间戳的值  7 V4 H- U! ?- N9 u9 A
mysql> select from_unixtime(unix_timestamp(),'%y %d %m %1 i0 G" v: K# U0 u" u' b
h:%i:%s %x');    : T. l9 `1 E( x' W
  -> '1997 23rd december 03:43:30 x'    , x" p4 W- y7 d  F3 t$ N

% a6 o% f7 \4 v1 Q3 g" e) w7 ssec_to_time(seconds)   
2 M3 |& V  R% L3 R以'hh:mm:ss'或hhmmss格式返回秒数转成的time值(根据返回值所处上下文是字符串或数字)     
% V: S% w( T1 z$ b1 D) L2 k& }! ~mysql> select sec_to_time(2378);    4 [0 A3 P9 D$ K
  -> '00:39:38'    # J$ O# `7 m! R5 C, n% V% I) u+ S0 S
mysql> select sec_to_time(2378) + 0;    " L8 O2 E- D5 m* Q3 k; W
  -> 3938   
& s3 H+ B* ~- m, r) q+ @' s' h
( K' A- M1 R/ g6 o* A( ktime_to_sec(time)   
, N" R! ~4 E% ]4 p: e! q5 L返回time值有多少秒    4 P, b3 r3 V1 O6 f6 k( |5 N( s
mysql> select time_to_sec('22:23:00');   
0 n# i1 {2 [' G, H6 y2 w  -> 80580   
. `! S" L6 q: i  S% tmysql> select time_to_sec('00:39:38');    $ L3 }, h0 M4 Z, u! C: _1 q
  -> 2378
8 E- W* c1 [0 G: o& L" i: X
( {% R3 b$ ?( z/ a0 }& w, y1 l转换函数. F3 L& J0 {& s. }# i
cast) o/ N1 I: k0 |* h3 l% V; G
用法:cast(字段 as 数据类型) [当然是否可以成功转换,还要看数据类型强制转化时注意的问题]6 E* v" T4 H" ]
实例:select cast(a as unsigned) as b from cardserver where order by b desc;9 c& S  f, h5 ^4 L
convert:
" g5 |; b4 a. H8 O2 W5 @# J' y用法:convert(字段,数据类型)
4 F1 G) E( e' T0 x0 u/ `实例:select convert(a ,unsigned) as b from cardserver where order by b desc;

8 H5 r$ g2 \6 C6 r: B/ `6 j
回复

使用道具 举报

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

本版积分规则

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