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

mysql常用函数大全

[复制链接]
跳转到指定楼层
楼主
发表于 2018-3-21 16:07:02 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
1、字符串函数  j* f+ G0 j1 K& ~" L
ascii(str)   " Q2 m3 y) D9 w
返回字符串str的第一个字符的ascii值(str是空串时返回0)  
; J' P. M4 S9 n; Smysql> select ascii('2');  
) p, r) C+ O7 }. Y0 \  R0 j7 |  -> 50  # e" K$ ^0 n1 b
mysql> select ascii(2);    s$ V7 ?" v( ?, ]: p
  -> 50  
& P$ ]# v2 D& smysql> select ascii('dete');  
  {8 P7 u" ~  w( E/ [  -> 100
1 [0 R$ g* p, s2 H3 W$ Q5 P
ord(str)   ( ^! G3 }/ e+ `9 [% a# q; O3 F
如果字符串str句首是单字节返回与ascii()函数返回的相同值。
8 w; _: y  D7 I' n! t0 l# q   m3 z7 j8 n4 K# G5 V$ S0 U
如果是一个多字节字符,以格式返回((first byte ascii code)*256+(second byte ascii code))[*256+third byte asciicode...]  ) X& U% u. F5 C- s5 b5 U+ ~
mysql> select ord('2');  % J6 e2 K6 `! M  b# h9 [1 K+ ?# j
  -> 50  4 F6 h9 l& y# q5 y1 r  \) r
   
3 _% y- d) n# {conv(n,from_base,to_base)   
. F, y, ~+ @5 s2 S+ x对数字n进制转换,并转换为字串返回(任何参数为null时返回null,进制范围为2-36进制,当to_base是负数时n作为有符号数否则作无符号数,conv以64位点精度工作)  9 D7 b7 p% I3 o) a4 s
mysql> select conv("a",16,2);  6 G9 b, V9 ?* T! A  [: Y5 m
  -> '1010' ! s1 c" w& d% |; p, K. Y; C4 U9 t
mysql> select conv("6e",18,8);  % `. U4 |& h0 ^- \, @8 O, _1 P% C
  -> '172' 3 Y, w7 n, R8 c! j
mysql> select conv(-17,10,-18);  7 y7 A7 g. }! r# z
  -> '-h' 3 Z; o' Q$ _9 K
mysql> select conv(10+"10"+'10'+0xa,10,10);  & z  }( o3 n5 }
  -> '40' 9 K, t) r6 w" d; Z
   
# L4 c, n7 z# ^  h2 [bin(n)   
: L; q% @! ~- E7 C6 J! ^5 A把n转为二进制值并以字串返回(n是bigint数字,等价于conv(n,10,2))  
$ W# d: \8 r; O* b3 V- N' mmysql> select bin(12);  0 d$ T8 V" w1 e  S: ]+ d2 x( f
  -> '1100' 2 c& M, l6 H* i- L7 V
& v* H, Y% h7 ~0 e
oct(n)   4 b( `  E9 g4 \
把n转为八进制值并以字串返回(n是bigint数字,等价于conv(n,10,8))  
* n$ X/ L) g1 b/ S2 j' Mmysql> select oct(12);  8 m% T. o3 y# E( I, X5 D
  -> '14' 3 J; |: s8 T8 d& q/ R
   
4 ?- B* F9 v7 f0 I2 Q) zhex(n)   4 z. R$ i1 h5 p1 E) L8 P, o
把n转为十六进制并以字串返回(n是bigint数字,等价于conv(n,10,16))  
3 V2 ?0 l" V! ]& l" B; Hmysql> select hex(255);  0 ^* {, f8 b' E2 Z2 p0 _
  -> 'ff'
- I- X# u  G2 }. h9 |* [* e$ T/ N, E   
4 K# L) k# w2 K; E3 L+ uchar(n,...)   
6 |7 A7 |/ C8 x$ m返回由参数n,...对应的ascii代码字符组成的一个字串(参数是n,...是数字序列,null值被跳过)   - L# @# ]& O. R: u! S2 K& @4 Y! y
mysql> select char(77,121,83,81,'76');  " x# n+ r& E7 b( R+ O# b. R
  -> 'mysql'
' t; B4 H+ r* T" C3 u: A9 v5 rmysql> select char(77,77.3,'77.3');  
/ A! `7 b7 f% t6 V$ F  -> 'mmm'
& e$ }+ {4 V2 O+ B/ l   & U! j  F) j. h: c" U2 ~; f2 P$ @
concat(str1,str2,...)   * b9 c# ^/ ~8 N2 o, T' F
把参数连成一个长字符串并返回(任何参数是null时返回null)  
" q4 O+ Y2 A0 b( ]2 umysql> select concat('my', 's', 'ql');  
  \) d3 P4 u. B8 z/ O  -> 'mysql' % A7 n+ l$ B! h
mysql> select concat('my', null, 'ql');  
( e' Z8 K- ?8 w9 a. a  -> null " W  N; V  G# _8 l* d
mysql> select concat(14.3);  : {5 J0 v$ h4 w1 B/ |
  -> '14.3'
3 V% p' N7 G2 d# P# n! e) @" Q7 g" _ 7 `5 w% {4 d+ ^
length(str)   
4 e3 u# K2 K3 V/ N/ Eoctet_length(str)  
, u" B! V/ I: vchar_length(str)  
  D1 f# v3 _! e& P( F+ Y- Ccharacter_length(str)  
. S2 `$ [; A+ R' \" h( f返回字符串str的长度(对于多字节字符char_length仅计算一次): t8 I5 U! Z/ x* P0 p
mysql> select length('text');  
# J# R0 c( M' g1 l$ E  -> 4  % Y* c) I; }9 F* ^2 S  q2 E
mysql> select octet_length('text');  9 ^3 K6 x1 P) O# {, X' K
  -> 4  
: T4 K3 N& b  p ! f* z2 B% k# }/ o& a. g& M/ M
locate(substr,str)   
0 C0 A, d( x/ Dposition(substr in str)   7 D$ [6 p- A* K/ J6 o* x
返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  $ f7 B- u9 {/ o: _
mysql> select locate('bar', 'foobarbar');  8 r; U# \' I/ c5 Q; z
  -> 4  
* s7 z1 V. B5 b0 w) wmysql> select locate('xbar', 'foobar');  4 R2 J  G/ t3 ~% a
  -> 0  
  F$ q  n  }  m8 e% a   
: q  r/ E! b; W- @/ b- d' [locate(substr,str,pos)
1 i8 [9 ?5 e" E% X! {- C5 F) U! L返回字符串substr在字符串str的第pos个位置起第一次出现的位置(str不包含substr时返回0)  * {+ m  O* ^2 Z7 `6 t+ e
mysql> select locate('bar', 'foobarbar',5);  , ]) J" J1 ]5 Q, |' T5 l1 m
  -> 7  
5 ]! K! e) ~8 Z/ w7 T . S! T' z- s5 @  K/ p0 x6 e
instr(str,substr)   ! B# \% _( y" p" j4 `
返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  6 J7 P9 L- O5 p8 x2 @' {2 n( j
mysql> select instr('foobarbar', 'bar');  9 S4 S0 R- A1 f; d& z, w$ r( n& p
  -> 4  7 R/ W0 J5 I+ l! }# Y1 r' f9 G
mysql> select instr('xbar', 'foobar');  
3 {" [8 m% A; k/ z. L: l. V) s+ n  -> 0   
8 e* u: P  E( g. c5 e * T& B; l" @) t  z$ x- f- P
lpad(str,len,padstr)   
8 f* M8 ]0 E$ g5 ^6 }3 Y# C! c用字符串padstr填补str左端直到字串长度为len并返回  - }; A" I0 R0 y4 P  ?
mysql> select lpad('hi',4,'??');  
! A2 A2 K9 A) x% O2 t  -> '??hi'
1 E5 E1 Y4 {. ]* E7 e. @: {5 ]   $ g# ?* h, q0 B" P5 _- y$ X
rpad(str,len,padstr)   
4 q5 c2 v& Z) c- e( C  X用字符串padstr填补str右端直到字串长度为len并返回  
1 ^+ c6 _* h& _; _; Lmysql> select rpad('hi',5,'?');  
; K+ ?% ?5 J  b# I$ u1 ?# K$ Q  -> 'hi???'   D! \8 ^/ O: M* @3 Y- t
, t- G$ O/ U* A6 @
left(str,len)   * Z1 ~8 U6 ^; s4 n: G: p
返回字符串str的左端len个字符  
' K. D2 \1 S& I! G6 u) j" e9 J# Tmysql> select left('foobarbar', 5);  
9 }0 L5 x5 z, w& ~3 j* j$ @  -> 'fooba'
+ R; c+ y! T+ }
) v$ R. G- n" ]/ H1 e5 A1 _* r# `right(str,len)   * N" S6 C# r) S. R* W$ ^
返回字符串str的右端len个字符   8 A7 |+ e* k: o# }  P: m8 _
mysql> select right('foobarbar', 4);  7 f# g( T" Q5 ^4 V3 P8 E  U$ ~, M7 Y
  -> 'rbar' 9 O6 c4 r+ @* I) f. @; \" c

7 y7 w4 p: U0 V6 H$ \: p9 wsubstring(str,pos,len)   ) H- n, k, J) P$ B
substring(str from pos for len)   
& M; C" z% k) M4 E1 _% imid(str,pos,len)   % h$ B. {1 p' o: @- {3 {& t
返回字符串str的位置pos起len个字符mysql> select substring('quadratically',5,6);  
* @- J+ ~5 A8 ]7 A  C" o& `  -> 'ratica' 6 K7 B* a% x8 \4 Q# [8 Q( Y0 V
0 f1 n' r5 B" R! N% U% {+ g
substring(str,pos)   # m1 U- @3 p1 l) o3 T+ j+ k! V1 P( c
substring(str from pos)   ( S5 H/ g& F# q" V7 O& F) Y
返回字符串str的位置pos起的一个子串  7 G0 I9 c! o3 @5 S. k" D9 S: S
mysql> select substring('quadratically',5);  
/ S( b9 S) O. c  -> 'ratically'
% v  `# j7 o# zmysql> select substring('foobarbar' from 4);  
/ `! Y3 @; L5 }0 L" X5 B* j  -> 'barbar' % s( t0 e8 D  i5 j7 d7 Q) O
# l. w; V) Y( j9 @
substring_index(str,delim,count)   " u% G+ ^# u8 E7 \% N- i
返回从字符串str的第count个出现的分隔符delim之后的子串' c) G) k7 ^, p( h  r# f. }
(count为正数时返回左端,否则返回右端子串)  6 @% l  U% L/ y1 F
mysql> select substring_index('www.mysql.com', '.', 2);  
" z0 |0 c; W% {+ t" }, {! N# M  -> 'www.mysql' 9 z/ L% r6 G* s' c9 I0 L5 Y8 H
mysql> select substring_index('www.mysql.com', '.', -2);  $ `) r' [, c; Q: {8 O  W
  -> 'mysql.com' 8 b) d) g1 R, S
3 |3 N" n1 L% z* n
ltrim(str)   9 r+ c" Z8 j3 f! H; @6 o
返回删除了左空格的字符串str  8 ^3 Q3 M. {* \7 A- X; @
mysql> select ltrim('  barbar');  7 ]2 V- _9 q5 T: _2 e& F
  -> 'barbar' . L9 I' t% |6 e$ I  B
7 Q0 l+ A0 @# W* F: u$ [% ~
rtrim(str)   : X: N8 s* S' F6 K4 X3 \
返回删除了右空格的字符串str  / R5 d/ |% W  m) _. h7 u: a
mysql> select rtrim('barbar   ');  
8 n- o- j& a, C! \. [5 I, t  -> 'barbar'
" \* Z) x! z* |; s) @   @7 g3 `6 B0 y) ^# ~
trim([[both | leading | trailing] [remstr] from] str)  
- x! l" w( _( g2 W, p返回前缀或后缀remstr被删除了的字符串str(位置参数默认both,remstr默认值为空格)  7 S3 @$ s; C* s9 ?' G
mysql> select trim('  bar   ');  
( F( S3 |* s$ I2 x  -> 'bar'
6 m! M0 g8 A8 b, F  Amysql> select trim(leading 'x' from 'xxxbarxxx');  6 j+ m' X% f  Z9 Q  {/ K0 g
  -> 'barxxx' 4 h5 c6 b- |2 k! _' z+ s
mysql> select trim(both 'x' from 'xxxbarxxx');  6 e6 s7 \. d0 H2 S( a
  -> 'bar'
; v- `+ Z7 K; v* m7 g% U- J8 Nmysql> select trim(trailing 'xyz' from 'barxxyz');  
' ?2 d/ q8 ]/ C( l5 x, A  -> 'barx'
2 |- ~: e% Q6 ^
  `1 X) }0 X' P" r. fsoundex(str)   9 M, f% B5 [* d1 D& V3 E! s
返回str的一个同音字符串(听起来“大致相同”字符串有相同的
) F$ {# @+ P" `2 P* _同音字符串,非数字字母字符被忽略,在a-z外的字母被当作元音)  
& C* _3 W  Q. F( ymysql> select soundex('hello');  
7 n  O4 V1 T/ Q$ q  -> 'h400'
% E5 k2 v6 ^8 ^7 U1 o2 q& B( l4 emysql> select soundex('quadratically');  # g, v% v( N/ g, {
  -> 'q36324' % D  [7 c) w1 L( |& w- k) B$ \7 m$ `
   
% F  n- |7 y$ [3 c- S& rspace(n)   
6 S! W" A) ?! U% O: }8 J6 x返回由n个空格字符组成的一个字符串  # R) Q8 t2 p1 g6 Z/ }3 M
mysql> select space(6);  
- C) r2 {+ H3 ~' k7 \$ c  -> '      ' 3 P( t& S. P! m. @
   
/ k- Y7 ]- C0 F, C2 M2 Wreplace(str,from_str,to_str)   1 o% l# G$ A# y
用字符串to_str替换字符串str中的子串from_str并返回  
$ |' k3 J+ `. e, `( emysql> select replace('www.mysql.com', 'w', 'ww');  / a$ n% t' l- T
  -> 'wwwwww.mysql.com'
/ n) M# o5 `  Q( n
0 T+ ?/ Y" \4 J9 d; n8 Srepeat(str,count)   7 _& @5 M; d1 }% \  y
返回由count个字符串str连成的一个字符串(任何参数为null时
. }/ v' L- ~/ V3 c/ N0 H- b: `4 p返回null,count<=0时返回一个空字符串)  3 a6 B$ v( p* d4 [3 W
mysql> select repeat('mysql', 3);  ( U! C& m, Z0 D& e( c
  -> 'mysqlmysqlmysql'
. ~- t" C: X' b3 p  u, [; O   7 t3 h. w; W8 L, k- `9 j. w- j
reverse(str)   
' T3 ?+ c( A/ z6 n颠倒字符串str的字符顺序并返回  . X5 b$ ]. S3 T: A8 I
mysql> select reverse('abc');  . Z. O  ?7 c9 L: N0 x/ v* |& g
  -> 'cba' ( ^3 e4 k* G$ H! ]1 k) K

. |/ g0 e  C" F7 V$ U) ^. F) xinsert(str,pos,len,newstr)   9 ]% ^" X) {4 ^% ]) J5 t
把字符串str由位置pos起len个字符长的子串替换为字符串( g5 z! \+ R% S2 ]8 ]& b, c
newstr并返回  
% ~2 w0 K. p2 Z' I# Kmysql> select insert('quadratic', 3, 4, 'what');  * m$ N6 H4 I7 `& c, m6 Y
  -> 'quwhattic' ) m: J1 `" S( d: O! K

9 N" q) q) ?6 R: @; |, ielt(n,str1,str2,str3,...)   & `1 G  ^1 F2 I2 e8 h
返回第n个字符串(n小于1或大于参数个数返回null)  & p; R6 G/ Q0 N+ x& w- d8 a
mysql> select elt(1, 'ej', 'heja', 'hej', 'foo');  . ?/ ^; \* Y0 V0 X, Q
  -> 'ej' + o2 \8 c4 N1 z, p$ I+ n6 e: W; ^
mysql> select elt(4, 'ej', 'heja', 'hej', 'foo');  , \' V( o9 ^- I
  -> 'foo' ! B; P! j- g5 T( H- B8 X/ b

) ]" [# y( j, y8 p5 X4 Dfield(str,str1,str2,str3,...)  
; t5 A9 ^9 D. K! y4 q" T  G, O% @$ W$ @返回str等于其后的第n个字符串的序号(如果str没找到返回0)  8 v+ x  |, a' {# M9 L& P
mysql> select field('ej', 'hej', 'ej', 'heja', 'hej',, {/ [2 ~) ]* {5 \: u  R, n" J
'foo');  
0 G$ J" \, K8 v+ a  -> 2  
% v3 }6 e4 w, c6 i* o' m6 Imysql> select field('fo', 'hej', 'ej', 'heja', 'hej',
  h7 U! R3 u6 _# d! O; X9 s7 ?  E'foo');  
/ R1 ^& O' j, y+ L  -> 0  
4 K# B# B1 o' |+ p* _% ~, S
; W4 n  [) ]  e! yfind_in_set(str,strlist)   6 g% i* Z0 U3 E7 ?
返回str在字符串集strlist中的序号(任何参数是null则返回  K6 G4 @! y, J* h3 f+ @
null,如果str没找到返回0,参数1包含","时工作异常)  
" Z/ \, o0 B, e0 l  o5 Fmysql> select find_in_set('b','a,b,c,d');  
, J; \4 N) G" p# j" n! _% @3 c  V  -> 2  
8 A; s1 g* I& p! ^! [9 N  H   
' r' D) o( v0 omake_set(bits,str1,str2,...)  
4 `( d1 H9 z& l- T; `2 T7 y把参数1的数字转为二进制,假如某个位置的二进制位等于1,对应+ K: f& @; I* z- n0 m& T* I" y: ?8 n
位置的字串选入字串集并返回(null串不添加到结果中)  * k$ C; }: u/ U5 P# m8 e
mysql> select make_set(1,'a','b','c');  , X4 A) p' \, d$ D2 y5 N4 E6 J) _
  -> 'a' ( S: o7 C7 x& i8 Z1 a; S+ o- m5 A
mysql> select make_set(1 | 4,'hello','nice','world');  
6 _4 S! Y  D  [1 @  -> 'hello,world'
4 k. a9 ?2 [0 h9 n5 B% H- N# z3 C4 @mysql> select make_set(0,'a','b','c');  
* \* H5 U" I' x% g/ @" U( Y$ R  -> '' 2 J# b' ?# y3 W, z

1 K$ u* m" v' m) A) Vexport_set(bits,on,off,[separator,[number_of_bits]])   
" X* N2 s) V" x- |/ @. w3 z& e按bits排列字符串集,只有当位等于1时插入字串on,否则插入# p- G1 @3 A+ ~# S) e2 ~
off(separator默认值",",number_of_bits参数使用时长度不足补0+ P- Q+ B# `4 h0 p. [# r9 H8 e+ A
而过长截断)   8 ~% ~* B; V2 P+ T
mysql> select export_set(5,'y','n',',',4)  2 [' d& A% T4 Q0 l* T
  -> y,n,y,n   - `$ K1 b# c/ K

+ F: _2 w2 V: l' D' vlcase(str)  
6 i$ h* G: X# `6 ~; Slower(str)   
# A1 s& c! s: T! X7 e返回小写的字符串str  
! q' V8 {# p: c" q7 h; x, Pmysql> select lcase('quadratically');    u5 ~# q4 B) R9 _& j/ F
  -> 'quadratically' % [3 C1 g7 Z+ |# m, N1 t6 O# E
   # M. e  e  R( b, a9 c
ucase(str)   ' c- p% X2 |) w! s: ]
upper(str)  
! [1 c) v- w, x7 t! D& K  s# `3 C返回大写的字符串str  ; |7 _8 V: z: p- ]( ]
mysql> select ucase('quadratically');  - a9 Q. s$ u2 c
  -> 'quadratically'   K2 K5 J+ p  j3 s- l7 w0 Z
- `2 Q0 L$ Z, z1 c5 h
load_file(file_name)   " v5 ]1 u; |# P2 j4 e( A+ Q
读入文件并且作为一个字符串返回文件内容(文件无法找到,路径
( F  O& Z* r7 j2 J: G+ v8 H不完整,没有权限,长度大于max_allowed_packet会返回null)  
$ C2 Q2 T2 g6 m( e3 umysql> update table_name set blob_column=load_file
: X: D% F2 k, d0 A("/tmp/picture") where id=1;  % ]5 C* f+ R' L" b0 _9 K0 C9 F& a

: Q  S! _7 s. k9 j/ Q& [& P2、数学函数
7 h# R9 v# i- P/ ^) G$ D! }abs(n)
1 k: D/ W+ B# H& }返回n的绝对值  
8 h3 e1 D8 z& G7 U- omysql> select abs(2);    * J. k/ }) H9 v1 r7 l3 }) I
  -> 2    5 W9 |0 ]# J, I# P, j2 U' T( d
mysql> select abs(-32);   
' @9 L5 @2 t" ]1 d# C- {  -> 32    9 B( A. c% V0 g# b" Z
   
! F/ @9 P$ W5 R2 |. q, hsign(n)    Q' X8 n3 D7 H
返回参数的符号(为-1、0或1)  " J$ h% ]$ N8 M9 e$ o5 i1 i$ t
mysql> select sign(-32);    ) D. R) ]2 o+ o" a3 B* Q
  -> -1   
% g' _( o4 H: fmysql> select sign(0);   
9 @4 E! y3 u# w7 i  -> 0   
  w1 e8 u) }2 u7 H% g2 p9 {1 _mysql> select sign(234);    ! ^' I( W3 P0 U6 p5 O" K! z' y
  -> 1    % @0 }5 L/ g& s$ @/ Y

2 _5 X5 D4 L( W" Q8 r9 jmod(n,m)    4 q# T5 o% Y' F7 _  C. H" u
取模运算,返回n被m除的余数(同%操作符)   
3 Q. k8 l5 f) Z+ b% f, B$ B+ i8 G/ kmysql> select mod(234, 10);    3 G7 w) ^+ O3 M; [- L6 q  D
  -> 4    % I: X  ?9 @9 @" d- Q  k
mysql> select 234 % 10;   
$ f9 l: p+ Q- f3 Y- B  -> 4    ( L' i: Y+ o8 e; A( r
mysql> select mod(29,9);   
/ R- l' K& D: O  -> 2    " {3 P- x( l: @" A

6 S1 k0 _+ s0 ]7 Nfloor(n)  
$ t% q5 D* v  b返回不大于n的最大整数值  5 {; o/ F5 G3 g! B+ l) I: m& Z
mysql> select floor(1.23);    2 `0 W5 ]1 U1 X  V! ]4 s
  -> 1    ) w3 D! p8 ?- v* ]7 T( I9 x: R
mysql> select floor(-1.23);    ) L. u3 k) z7 Q
  -> -2   
/ R% ?; Y% `4 ~/ Y  n) l
8 X1 M5 g; n$ O+ l! t) L4 T2 Aceiling(n)  ; i8 ?3 x& i2 {4 D5 W" ?
返回不小于n的最小整数值  5 K3 T" U- t% L
mysql> select ceiling(1.23);   
6 F0 M$ ^/ E4 }+ l  -> 2   
. I* q) r; \/ n8 v3 n, H8 D. bmysql> select ceiling(-1.23);    1 q2 f, `- K9 Q; L7 {
  -> -1    3 ]* e% l) W, m' M
( n- z3 N5 G; Y5 U
round(n,d)  
7 F4 }- O$ P5 x返回n的四舍五入值,保留d位小数(d的默认值为0)  
, E( b8 Z% j6 t1 ~mysql> select round(-1.23);   
: k) y6 w$ F4 M5 J# Y  -> -1    $ o; i* @0 G4 |, @1 V% `/ s
mysql> select round(-1.58);    : _! x6 d! l* ^3 F
  -> -2   
% `. k0 \3 K& ]5 o9 x) }7 Pmysql> select round(1.58);   
6 b" E! M/ g/ d& x  F: Y2 W! r  -> 2   
: p' A; W1 t9 f& c' ?! Zmysql> select round(1.298, 1);    ; `2 r, o- ?; _1 `/ G3 {% X3 b5 b
  -> 1.3   
& c$ a* c* T8 v& @- Bmysql> select round(1.298, 0);    8 K. ^4 t1 {! |) A
  -> 1    & {4 H5 @: D5 ~4 z+ \7 \* b. O
, V" }: b+ q8 @( k5 Y
exp(n)  $ G* [1 i, I% x; o  _0 {
返回值e的n次方(自然对数的底)  + m& A$ d. q! |
mysql> select exp(2);    . k$ V. P- U5 b2 u
  -> 7.389056    ; ?" H; Y. A8 ~
mysql> select exp(-2);    . _: g5 q, {) O, F2 Y' a
  -> 0.135335   
& j! p) H  ?2 D  H 8 V$ f2 m! ~- o- q! f% {0 G, ^8 }9 ~7 C
log(n)  
( B* P8 j% u3 K& c6 ?7 C0 W7 n返回n的自然对数  
# ~" k8 i4 _) M6 v8 r' Smysql> select log(2);    8 G) g; a6 @3 N$ t9 [: a
  -> 0.693147   
0 {; c# w7 K# ~) t* s3 a+ y% \mysql> select log(-2);    8 o' X' @9 g* @; E, ?
  -> null    ) ^( S, b) _" W/ w5 c5 ]  V- H

& {# a3 e3 O. Z" ?- rlog10(n)  , z8 V3 F) V. B! T+ [
返回n以10为底的对数  " o& T& K# T  z
mysql> select log10(2);    + n! L: S" I7 V; i* I7 E
  -> 0.301030   
, A# e% w3 H* k5 tmysql> select log10(100);    $ v1 s6 L: |. R
  -> 2.000000    $ E" T6 R7 Q7 F& `5 G
mysql> select log10(-100);   
5 I5 h9 }0 S7 {, J5 g  -> null    ( `  M- }4 }7 ~8 V5 c& K" y/ ?

$ d& S8 u; V" Dpow(x,y)    # J# S; [% S/ b5 Z; w/ f: P
power(x,y)    / R& [* j$ y( T' M2 z6 u
 返回值x的y次幂  
, S/ W* Y7 c% y5 }3 k) d7 _mysql> select pow(2,2);   
; U" X2 L, f! u1 z  -> 4.000000   
3 Z7 }- w% v1 a$ E4 smysql> select pow(2,-2);    + o0 b/ }' [) Q$ a
  -> 0.250000  6 Y) @( |# y1 e$ P. B7 c4 n! N
9 T0 V; a3 p' O8 X; E
sqrt(n)  
1 Z+ {. S# g: H+ n% m 返回非负数n的平方根  
$ }2 q" p* R3 b( J0 imysql> select sqrt(4);    $ J% n# G: e" _' g2 I
  -> 2.000000    % l% N0 r2 _" h1 m/ ?) T5 O3 U8 z
mysql> select sqrt(20);   
8 w( l1 e' f* y3 H' a1 T7 Z  -> 4.472136    - R- O4 D7 a1 A- O0 f( q

% p: u- E# s6 L5 epi()    : X% X3 L* Q& W; l6 j7 I
 返回圆周率   
: `8 k' y( A9 L. E& P3 L9 f$ vmysql> select pi();   
) J8 Z# G) R) H3 h( @3 P2 L  -> 3.141593   
/ r. _' t' R6 g1 I; q   `9 p+ S  g) y4 m& [- [. p# ?" Q
cos(n)  
. E% G# t# U& y, @ 返回n的余弦值  
2 Q+ l% _: L! [( l- S, q9 jmysql> select cos(pi());  1 h& X4 m/ Q$ U4 R
  -> -1.000000   
+ Q8 x; x1 N  j4 g2 z: j ( E8 q  P, c7 G/ O
sin(n)  
( T6 t8 E4 u& |. b1 {; @ 返回n的正弦值   ' M8 t- f7 G" R+ ]% p; V
mysql> select sin(pi());   
6 |' M) }  h. C, m! L7 `  -> 0.000000   
2 E, C8 f4 v6 O3 u+ e % u% W7 g; E: g. q* l  y/ }
tan(n)  3 J( ~2 Y, n1 }( ~
返回n的正切值  * V9 \5 \: l6 G, U/ M, _
mysql> select tan(pi()+1);    6 x/ d" r8 {/ A6 o6 z
  -> 1.557408   
* j, u3 o' ?: G' N. Z- e
0 l: m0 }- M$ nacos(n)  2 o2 X4 A& M- e9 o
 返回n反余弦(n是余弦值,在-1到1的范围,否则返回null)  
- i0 G3 \5 a: W# Q/ ], C, zmysql> select acos(1);   
- M/ X4 J9 |+ g% d$ D* U& V$ v7 i# |  -> 0.000000    " R6 j+ V# r+ r% ]) u: x
mysql> select acos(1.0001);   
% z3 G+ H! y" W1 H6 n  -> null   
3 Y( `3 k3 |& K7 y2 zmysql> select acos(0);    * }* e4 F. J  ~* s9 g1 X( g5 y8 P& B
  -> 1.570796    ' S9 H: L5 K! O! y. r# j
7 j2 R/ D, [+ u$ x
asin(n)  2 E9 t, h# `: h0 d0 M; _& U) Q8 F5 s
返回n反正弦值  
) [3 A5 _# H& Z0 }( e# `( Q4 vmysql> select asin(0.2);   
1 f2 o/ b, d$ r: }$ Q3 f  -> 0.201358   
7 P! K8 O$ Q* v) T4 ymysql> select asin('foo');   
# b* A  a" c0 d) x: w: o  -> 0.000000      F/ s9 A( D6 V- ]" I& c; x/ J

; t! [+ Z. b5 V& A* ^7 c6 F/ uatan(n)  
* L8 V" g/ f$ U" P$ @5 x' p- F+ L9 a返回n的反正切值  
$ E0 X( a) S( z  z" h* M1 hmysql> select atan(2);    6 h) d5 y% Q) R/ J: f" R' y: Q: D+ }
  -> 1.107149   
1 v$ i0 {! i. W( k$ Zmysql> select atan(-2);    ' _  m/ [* r0 ^+ Q5 d6 G
  -> -1.107149    / q- t6 J  j' w* i9 ~
atan2(x,y)    & Z5 m: a) h$ T1 O0 M4 T
 返回2个变量x和y的反正切(类似y/x的反正切,符号决定象限)  
4 Z. @3 c6 l' r) jmysql> select atan(-2,2);   
: |( p4 }' x0 A( n$ Y! u' z2 @  -> -0.785398    4 S9 W0 p) j' T. Z/ d
mysql> select atan(pi(),0);    4 ~5 }  X! U+ B' O
  -> 1.570796    , ~2 C, y; o; l  V4 s

7 Z% q" ?6 O" h# K3 L) Q# icot(n)    u* s) Q4 a7 I. {2 B
返回x的余切  7 z+ `) }; t) z7 y
mysql> select cot(12);   
  }+ p0 U" d& j  }$ r. a! `3 e, K& T  X  -> -1.57267341   
1 x/ O3 I& j) R+ umysql> select cot(0);    ( ^7 f2 s! h6 F2 \
  -> null   
8 k8 q: |7 p: O; ^9 n 9 t. @4 z( a. i
rand()  ; Z( m$ A8 p$ q" s; r
rand(n)    
9 ~$ z& g. D) R+ R- _. M返回在范围0到1.0内的随机浮点值(可以使用数字n作为初始值) . \& f" L7 i% _. A. A

+ ]$ {# Y& Y! u* z+ }% t9 x2 bmysql> select rand();    4 w* O5 \7 P% R; Q3 k
  -> 0.5925    1 q: G# e- Q. B& F) ?8 r
mysql> select rand(20);    * m# @( D. R5 R
  -> 0.1811    ) ?6 w+ Z1 U8 _& u2 W
mysql> select rand(20);   
1 ?! }0 q1 z( ^9 s& {  -> 0.1811   
) P3 ]) U. m2 Imysql> select rand();   
; s6 ]6 i( b: W0 |# l" o  -> 0.2079    - n- l% a+ x* J5 {- E
mysql> select rand();   
2 v' g. D  O! y3 K/ E  -> 0.7888    & P7 P3 C- ^6 a4 {' M9 N9 i# i

$ r9 U1 a; k6 E* B* rdegrees(n)  . k8 {* a5 `" ^. r9 K- u
把n从弧度变换为角度并返回  - X0 w! X* Y8 O& W. F; _
mysql> select degrees(pi());   
6 F# W! x* E6 g! m0 Y1 \  r5 h  -> 180.000000   
: `* A! J& U% M8 H7 N& f0 ]
. s& o$ J8 ]. u. K' N4 ]radians(n) # I- x( a2 J) e) E. D4 [
把n从角度变换为弧度并返回   ; n" j0 O0 l: ?! R5 Q! _) E; ]4 y
mysql> select radians(90);   
0 y2 O& M9 x; J- Q; d2 f  -> 1.570796    8 O  v: \3 z% ?. K) f% b. s0 j. R

1 Z9 A+ `; b! {; H3 Ttruncate(n,d)    : K# t1 Q) M) }
保留数字n的d位小数并返回  1 y5 k% V9 B" a  n
mysql> select truncate(1.223,1);   
" r; v4 }: J, R  -> 1.2   
! ?* _  L2 m! B6 Y& ~# Wmysql> select truncate(1.999,1);   
/ p$ L' p. |  P4 k  -> 1.9    4 F' ~1 ?( i$ r5 |
mysql> select truncate(1.999,0);    ( G4 _! e9 x' q* b- m
  -> 1    3 r4 S% y( B1 }' c, e
' }) L# u* h. {& u" P. p8 K/ S
least(x,y,...)    * \; }$ W! S, n! J$ j7 J; r" f+ A
返回最小值(如果返回值被用在整数(实数或大小敏感字串)上下文或所有参数都是整数(实数或大小敏感字串)则他们作为整数(实数或大小敏感字串)比较,否则按忽略大小写的字符串被比较)  
, B6 m) T/ \6 i3 qmysql> select least(2,0);   
& x% T: x* l: z$ f) S  -> 0    " R9 P% y. Q6 C, N/ r4 N' u' l! ^
mysql> select least(34.0,3.0,5.0,767.0);   
1 J0 M- l8 R2 o7 l! K4 G! q% j+ X  -> 3.0   
  r" \5 O. W2 rmysql> select least("b","a","c");    7 {5 ]9 g4 q$ G1 e, g
  -> "a"    4 e  l9 h$ d8 x

/ m' i8 Q% Z0 N2 x$ n- `greatest(x,y,...)    ) M: T5 e6 t# ]& Q! h
返回最大值(其余同least())  , j2 M$ `9 d1 [
mysql> select greatest(2,0);   
; S2 |. e0 G" U9 o6 k  -> 2   
% J/ w6 c) Z; m" H  Emysql> select greatest(34.0,3.0,5.0,767.0);   
3 e/ B7 ~4 q  }  P: H9 j7 b  -> 767.0    4 h* R( A# b" {, e* t2 r
mysql> select greatest("b","a","c");    ' G/ G; m: ?0 Q6 [! Z! t
  -> "c"     / s8 i/ C4 v" f2 \. l3 z  I
- o. ~! P* ]- f/ |/ |
3、时期时间函数 2 P& n, `; H9 Y' i
dayofweek(date)    / e  s. e/ ^8 w4 x- I
返回日期date是星期几(1=星期天,2=星期一,……7=星期六,odbc标准)  
5 s' }& F/ P8 H5 a$ h& x, p9 D$ o/ qmysql> select dayofweek('1998-02-03');    : C7 }9 _# H$ ?, }
  -> 3   
: R5 h% g4 W4 B6 \  k. p
# t/ M: d2 J+ J" J, u: Wweekday(date)   
7 P+ t; A5 A" `* `3 V( {3 u# @% K  K返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。
$ [3 L) j5 I  j/ I  7 \" b6 {/ z/ G  a3 j# U
mysql> select weekday('1997-10-04 22:23:00');   
7 t, H6 O1 R) R: ?4 h  -> 5   
; ^& R& x! m5 Cmysql> select weekday('1997-11-05');   
& F; `" x0 f. F' S) v' W2 r  -> 2   
, [: P& Q+ {% b' m
! @7 N0 z7 V  w* ^3 k" M+ ~dayofmonth(date)    
/ \1 d& ~: \/ q返回date是一月中的第几日(在1到31范围内)    : ]+ Z" I; _  O
mysql> select dayofmonth('1998-02-03');    9 a6 F7 O1 o% x- L( N
  -> 3    0 B& Y8 ~% K& n$ c1 \

4 |. j% ]$ X1 w+ ~* X% @dayofyear(date)   
8 W+ v7 t6 T* N7 G& y' g返回date是一年中的第几日(在1到366范围内)    : s# P2 d: X* Z; T$ E0 I
mysql> select dayofyear('1998-02-03');    ) Q- N$ x; i& @& C$ V4 ~
  -> 34   
, o: R2 n1 O0 E4 G
" T. V& U+ J2 L6 L1 Q/ `# h" z. gmonth(date)   
. z3 ^/ e8 M$ r9 Z6 V返回date中的月份数值    8 b( `. {. f/ X9 d4 C7 _. n
mysql> select month('1998-02-03');   
  G- s( O5 ~9 F9 ]+ v  -> 2    1 m* k5 h, \$ N+ ]% E% N: [

6 Q+ E; z- [" A/ i! Q- ]. [! Udayname(date)    + ?4 W$ W6 S; b6 q9 B
返回date是星期几(按英文名返回)  1 s# o) i: N: j* ]- s
mysql> select dayname("1998-02-05");   
3 h; O  e2 ]# M% U  b% T. h& U  -> 'thursday'   
- S1 k  o% L% o
7 T/ M. J5 y1 }) t3 ?" A& Mmonthname(date)    ( U( a- U/ Z' I* U1 l: O' _9 J
返回date是几月(按英文名返回)  
/ {0 Z; O. C: Rmysql> select monthname("1998-02-05");   
! U' S6 t2 |- @  -> 'february'    2 k, |- s1 ]& L5 S
. r" i$ d% g0 I) X5 b. q2 h+ @) c
quarter(date)    2 j. q! J. S+ y8 |: _0 L& c
返回date是一年的第几个季度   
! H" Z6 X! w: U- amysql> select quarter('98-04-01');    # ]. s2 ~7 [. }9 d/ X
  -> 2    , [9 D6 v" W8 H% r  J

" @: v: [. t, O4 l! |% S3 N$ F  H5 zweek(date,first)   ) q$ s; r- I% C* g3 l
返回date是一年的第几周(first默认值0,first取值1表示周一是
2 U  y! g! {2 g7 \# C8 ?周的开始,0从周日开始)  ; L( j4 }% U# H
mysql> select week('1998-02-20');   
% w- X7 p1 F* b7 Q2 P$ X# n  -> 7   
$ Z$ I' S) E  R  Amysql> select week('1998-02-20',0);   
; n# A6 W  }+ l/ k6 a% @  -> 7    8 E: d9 `) T, J& J/ A" ~; S* f9 \
mysql> select week('1998-02-20',1);   
  u4 t% q5 [; l; e7 ^  -> 8    ; ~( C9 [# i, f
7 j8 _: h+ `: Z: J; F; p7 W: `& e4 q
year(date)   
% i+ Z7 b' \; ]+ K6 s返回date的年份(范围在1000到9999)   
. t+ a( `  `5 R! A; g' n7 G" _mysql> select year('98-02-03');    + {' X- r" u8 J1 U5 H
  -> 1998    " ~, P, T8 l+ {. m9 s2 Z5 N
% i+ U; u1 k! ~( e, P, k
hour(time)    8 i+ u8 O. b4 V
返回time的小时数(范围是0到23)   
, _. e, O# p1 ~/ z0 xmysql> select hour('10:05:03');    0 e" K2 o, a" I% W( ?8 l  Y  ]2 X
  -> 10    / M  [$ M0 }) y" |: g, L
  y1 y! V0 T0 e: K* D; H
minute(time)   
) @( G. D! Q' O- l" T( z- P' B0 C返回time的分钟数(范围是0到59)    0 n) l9 O. Q1 ]; g
mysql> select minute('98-02-03 10:05:03');    . S8 T/ |8 u0 k
  -> 5   
1 A' A: J0 Q& n: S
: A7 g# v' B+ B4 m+ r* h$ Z0 p( Zsecond(time)    
2 K3 i4 R; N0 D! m% h5 Q. r返回time的秒数(范围是0到59)   2 f& L. a, I) `% b6 \; \
mysql> select second('10:05:03');    . H( ?" t1 _; G2 w/ l8 k
  -> 3    8 j0 l9 P4 C& A8 Y* _. m; j

/ Z: a  e4 o' Y. pperiod_add(p,n)    2 Q% T5 L2 @* k- R3 k, g
增加n个月到时期p并返回(p的格式yymm或yyyymm)   
2 T0 M9 w) G! Q4 t2 d. c" zmysql> select period_add(9801,2);   
: S' F2 ^0 B/ B( x5 C& t4 x1 e  -> 199803   
, `  u2 c9 f! k6 w! ]
) `5 z' ?* B6 s/ \6 l# E$ n6 mperiod_diff(p1,p2)   
3 l) p! r4 y3 ^7 e5 O返回在时期p1和p2之间月数(p1和p2的格式yymm或yyyymm)  
3 s9 q1 r" f" q: d0 Nmysql> select period_diff(9802,199703);   
0 g: j' u9 |8 b  -> 11    + ~% v  _# S  H* R+ p& i" D( X) L
4 v/ `& j! F5 u) t" n" P$ Z6 U2 n5 r
date_add(date,interval expr type)  2 R! x7 f& Q" n9 }' K' b& M. \
date_sub(date,interval expr type)    8 h/ I+ U% {+ m/ K$ A
adddate(date,interval expr type)      G# E: r8 v5 r
subdate(date,interval expr type)  
- C4 Q' \* U7 P. L; Q* L0 P7 O对日期时间进行加减法运算  
3 s: g, t3 i# y, A(adddate()和subdate()是date_add()和date_sub()的同义词,也9 Z. y6 ]7 a' l1 O, W& B' \
可以用运算符+和-而不是函数  . j2 ]/ H. x+ p2 Y& V
date是一个datetime或date值,expr对date进行加减法的一个表: K, D, s8 V! Q* c4 `4 H7 T7 T8 G
达式字符串type指明表达式expr应该如何被解释  : l, H: \# u% G# K: I2 U
 [type值 含义 期望的expr格式]:  
! n. F6 i1 |/ _ second 秒 seconds   
0 n! E0 I+ K& j" h$ q' r/ V minute 分钟 minutes    & _9 N( X4 M; T! S  e
 hour 时间 hours   
4 t$ G" h5 ]* M! k( h day 天 days    . l& Q7 J7 P1 F0 w/ m6 ^2 w6 Q6 I
 month 月 months   
6 v" ^* F, E, Q6 e year 年 years    5 y) @4 Q, |1 K* l, p; K3 u5 y2 r
 minute_second 分钟和秒 "minutes:seconds"   
& L& S' _9 M+ a4 s hour_minute 小时和分钟 "hours:minutes"   
! m5 S9 |. J1 d5 y6 l* B* ^# i3 E day_hour 天和小时 "days hours"   
" @& R% B. d6 s) m3 }7 U7 ?; l9 Y year_month 年和月 "years-months"    + n% O8 S: F! Y4 d- W9 `
 hour_second 小时, 分钟, "hours:minutes:seconds"   
; |* U; L6 Y; W2 L  o  q day_minute 天, 小时, 分钟 "days hours:minutes"   
' p- s  v: f* r0 u& t# w day_second 天, 小时, 分钟, 秒 "days) h4 B; q( u/ J5 N
hours:minutes:seconds"
8 X* S4 }4 U! ~5 h8 |5 x+ K expr中允许任何标点做分隔符,如果所有是date值时结果是一个) s. [  }! [/ V) f) d' C# Q7 P
date值,否则结果是一个datetime值)  : |8 k% B8 ?3 `% {/ L/ o! N+ _
 如果type关键词不完整,则mysql从右端取值,day_second因为缺, ]1 Y6 M: N: z6 s  W0 Q
少小时分钟等于minute_second)  
6 |- C8 {0 }! g4 f4 ^; F& g 如果增加month、year_month或year,天数大于结果月份的最大天
5 U6 x% T+ R) k- t. B数则使用最大天数)   
0 M+ }& ]  e# S8 m+ e) [mysql> select "1997-12-31 23:59:59" + interval 1 second;  $ g# O2 Y. S2 ?' G  {

9 o' x" L" B  r  -> 1998-01-01 00:00:00    + V# v, d3 A# d! o4 i
mysql> select interval 1 day + "1997-12-31";   
7 s1 b( L3 y1 R  -> 1998-01-01   
$ W( k+ `$ i& I/ ~# S- j6 o. Smysql> select "1998-01-01" - interval 1 second;   
  O9 {4 U+ F. ~. h  -> 1997-12-31 23:59:59   
' `% }9 k% s6 f& [mysql> select date_add("1997-12-31 23:59:59",interval 1
& c- Y2 L+ ~3 \9 [3 K- u' \' l2 ^! e5 {second);   
& K5 k. h" `9 n) F( B& p  -> 1998-01-01 00:00:00    & y& @% f2 H/ J+ o
mysql> select date_add("1997-12-31 23:59:59",interval 1
$ z7 d7 V" j% G4 n- k3 ]  aday);    1 \& Q5 `1 I' i8 J
  -> 1998-01-01 23:59:59    8 r& n( l$ {: ]5 e9 d* f4 W
mysql> select date_add("1997-12-31 23:59:59",interval: L+ }* J( i- p) @0 M
"1:1" minute_second);    ! Q( r# q, ~+ d2 G4 T+ z' F4 g
  -> 1998-01-01 00:01:00    $ O) T& x7 }2 c& A0 y  b
mysql> select date_sub("1998-01-01 00:00:00",interval "1
" l; K; N9 v& e8 J* I1:1:1" day_second);   
5 j: Z. x3 b- P! K0 {  -> 1997-12-30 22:58:59   
( D0 A# f- g- P5 o4 ?. Mmysql> select date_add("1998-01-01 00:00:00", interval "-1
' \6 W" N+ A- [. W, a10" day_hour);  
! i$ c) p, K) G( e5 k! I5 y# Y: m7 N  -> 1997-12-30 14:00:00   
( y& k% {8 p6 `& Fmysql> select date_sub("1998-01-02", interval 31 day);   
4 L0 I- ^) ]; `: X& A  -> 1997-12-02   
! R5 h; I& T1 E; @! y/ H/ |! amysql> select extract(year from "1999-07-02");   
( |/ ]( \2 ]; ~; `  -> 1999   
8 ]5 F2 Y( n( C& f. A$ xmysql> select extract(year_month from "1999-07-02  a4 ^3 u$ b7 R# D
01:02:03");    ; y6 ~. e3 T) E7 [# L6 I
  -> 199907   
6 l6 `) Y/ g6 kmysql> select extract(day_minute from "1999-07-02: V$ j4 M! q. {9 f- f
01:02:03");   
) Y! i6 n2 S' N0 Q6 J$ l& {! y  -> 20102   
3 }: d. `4 M5 W3 k $ @: N! F& t& I) X9 ?
to_days(date)    + i# c0 e. `) F* X0 P& ~6 G+ f, c
返回日期date是西元0年至今多少天(不计算1582年以前)  
0 T/ G- ]/ ~# c) pmysql> select to_days(950501);   
6 ~4 Q; B- f- V: h5 e/ M$ s  -> 728779    ( ^. K; ~# Y; T" f$ u5 y
mysql> select to_days('1997-10-07');    $ Q' y3 k0 e2 ^/ J2 G/ L& G
  -> 729669    1 Z' c8 j  N. j

4 E+ A& a9 C# D& f" X8 w1 Vfrom_days(n)   
5 ^, l. j/ }9 F. ?1 B 给出西元0年至今多少天返回date值(不计算1582年以前)   * L* i1 j! G2 S: Y& w
mysql> select from_days(729669);   
' x0 |9 U0 ^: z+ l  -> '1997-10-07'    ; I8 A6 Q6 U+ b9 I+ L: B
" Q: H4 Y! y* I
date_format(date,format)   
+ e* Z- \5 T+ ?% ~3 I8 S! F7 d 根据format字符串格式化date值  ' F0 [8 K5 j, c9 n' q
 (在format字符串中可用标志符:  % }( S8 ^- s/ d  b/ e( I0 o, _
 %m 月名字(january……december)   
7 {  [4 [( Z) s2 T %w 星期名字(sunday……saturday)   
& S) w- R0 v8 H% J %d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)    / I- o! l% m: L; {# G
 %y 年, 数字, 4 位    2 K) R; r$ x0 u
 %y 年, 数字, 2 位   
8 L. g: ?4 t1 ~- R8 j. x# J+ V %a 缩写的星期名字(sun……sat)    ) g5 q$ N+ \' b
 %d 月份中的天数, 数字(00……31)   
: V$ A' Y' x; O: f9 R %e 月份中的天数, 数字(0……31)   
+ M$ G- G- l% D6 l %m 月, 数字(01……12)   
1 \+ r6 E, ^( `! O9 k) X0 k %c 月, 数字(1……12)    3 D5 \, N' V+ s+ B) c' O6 }
 %b 缩写的月份名字(jan……dec)   
# W* |8 ?# }# Z/ s %j 一年中的天数(001……366)    ' S" @) F" @$ X3 P
 %h 小时(00……23)   
  x3 h. j0 B1 E( Q* e %k 小时(0……23)    ) N0 a  R/ G1 U% ~0 }) b  n& \
 %h 小时(01……12)    ( C4 p/ q! m3 y" B) o
 %i 小时(01……12)    " W7 ]2 Q- O" X/ G
 %l 小时(1……12)   
% }" K' e3 d! u %i 分钟, 数字(00……59)   
4 E+ x! B& t+ l3 v" m %r 时间,12 小时(hh:mm:ss [ap]m)    $ ~9 L& r) }) d- B5 M" q
 %t 时间,24 小时(hh:mm:ss)    ) `. ?' N/ ]) z$ e
 %s 秒(00……59)    6 t. K  }: K5 n# c1 i, W/ M
 %s 秒(00……59)   
4 K9 q  l4 {6 S4 `5 ~0 f7 n %p am或pm   
- M- d  u. f( |: N, l, S7 Z$ q %w 一个星期中的天数(0=sunday ……6=saturday )   
* {* {5 K4 }. W0 y8 ` %u 星期(0……52), 这里星期天是星期的第一天   
4 c# o" X2 p& E# l8 o %u 星期(0……52), 这里星期一是星期的第一天    $ X5 c: p5 s. b) H$ ~+ y
 %% 字符% )  
. ^% J' I5 J* U# Rmysql> select date_format('1997-10-04 22:23:00','%w %m %
+ Q* I4 |' N* F; Z  e- H' ny');   
6 b% Z8 w! Z1 z; m+ C1 ~  -> 'saturday october 1997'   
& e% k1 q6 K* B6 z5 _mysql> select date_format('1997-10-04 22:23:00','%h:%i:%
9 M7 W1 _8 W  o+ J7 E6 S& vs');    ; k0 }9 D  g; u/ F2 N
  -> '22:23:00'   
6 a' T0 h) b" m' B$ E: r2 U6 dmysql> select date_format('1997-10-04 22:23:00','%d %y %a* x. J2 O. E. `: G
%d %m %b %j');    , ?8 B9 L+ J! t% W1 Q3 b
  -> '4th 97 sat 04 10 oct 277'    ! M7 S  D5 t- ?
mysql> select date_format('1997-10-04 22:23:00','%h %k %i( O  P: x$ d& \  e
%r %t %s %w');    ! a4 u# R) y" K' \3 Q
  -> '22 22 10 10:23:00 pm 22:23:00 00 6'   
7 X( S/ M7 q' a2 Y- A
2 A% K$ @/ A7 stime_format(time,format)  . x1 L- T$ m6 R; ]/ ~- y
 和date_format()类似,但time_format只处理小时、分钟和秒(其
& j$ i- J9 E$ }/ z7 X余符号产生一个null值或0)  2 @( ~4 f9 G6 L
& t" N/ _9 k- ?  ?9 V2 R* h
curdate()     
1 _8 j, y( I2 f9 v+ o) ^8 Rcurrent_date()  
& z7 ~; q3 }, A7 g) K, y 以'yyyy-mm-dd'或yyyymmdd格式返回当前日期值(根据返回值所
- g* a( j. Z* D处上下文是字符串或数字)   
6 }7 y2 u/ c0 U8 i; S! c+ E6 A4 Pmysql> select curdate();    ; X5 U! M, `# ?
  -> '1997-12-15'    3 t3 {; L! l0 F# M' z' V
mysql> select curdate() + 0;    9 Z: N5 \: ]- Q* H  x
  -> 19971215   
# W7 P; s( h3 S+ g: l& U
$ q7 t$ ?4 k; x5 }curtime()    2 }7 B. r* {* c
current_time()  ; [& d8 y) n* Q) O
 以'hh:mm:ss'或hhmmss格式返回当前时间值(根据返回值所处上
. @$ e4 ?: P  N1 Q; f2 T7 T下文是字符串或数字)      / U, r) F8 p; ~6 i# N
mysql> select curtime();      C) d' r% C& o7 M. Y" U
  -> '23:50:26'    ; k+ E' O0 K5 G4 F
mysql> select curtime() + 0;    6 f+ H) H: Q, L+ o7 \& i/ F/ S
  -> 235026    - J+ c' Z* q+ m% w3 E: E
  [- G2 a( e, o' Z' H! J& `; N
now()    2 V$ }% a( ^# _6 {& ~, F8 w
sysdate()    
2 e" \7 W% v) J) @' ocurrent_timestamp()  ! }" \) t1 M! ^3 O, G+ X4 n3 M& I
 以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回当前日期2 J) w9 l2 A5 F0 M$ J4 h
时间(根据返回值所处上下文是字符串或数字)     $ r( n" l' I# ^
mysql> select now();   
% ~2 ?2 U. q7 y! U+ m, l! B  -> '1997-12-15 23:50:26'   
6 r- y! |/ t9 N! |8 C# T# _3 emysql> select now() + 0;   
2 V! F' k( y7 S9 X  I" x) y! ~5 F, d  -> 19971215235026    3 f7 ?0 ^* W6 v, d5 z

4 {) h: h* H1 ^5 S/ \unix_timestamp()   
" R3 w/ \$ z0 s/ A" nunix_timestamp(date)   
- k) ?9 Z% E. Z返回一个unix时间戳(从'1970-01-01 00:00:00'gmt开始的秒
$ T0 b8 o) ~+ I# j( f3 D- h数,date默认值为当前时间)  2 z+ [( i4 g& G1 K, L
mysql> select unix_timestamp();   
& ~& @1 M5 R  t- V: t; s1 B  -> 882226357    % d  \- V) U+ f( y2 O# E
mysql> select unix_timestamp('1997-10-04 22:23:00');   
' d( a$ R% V! u7 T  -> 875996580    2 }. y: b, A4 P, \: V$ R

7 k3 o0 T" _* i( [& s; E3 vfrom_unixtime(unix_timestamp)    & ~8 B& K6 R: r4 V
以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回时间戳的
  N. _7 a3 T- t' }% |值(根据返回值所处上下文是字符串或数字)     & R, ~$ l' @+ Q
mysql> select from_unixtime(875996580);    # u  b7 B/ X% n# J. e$ e
  -> '1997-10-04 22:23:00'   
7 u0 b' O! F6 D7 C0 X+ l/ Jmysql> select from_unixtime(875996580) + 0;    ' _) s5 a7 R5 y  V  P) m  @1 ^
  -> 19971004222300    2 W/ X# J9 d, J: u7 r$ s$ I6 {  Z: T

/ ^" H8 @! u8 T& h% \" W. Ifrom_unixtime(unix_timestamp,format)    3 b- r4 U% y- p  x1 t& T+ L
以format字符串格式返回时间戳的值  $ T+ x% U3 N+ S" r- R  G
mysql> select from_unixtime(unix_timestamp(),'%y %d %m %2 ^6 N1 p% j+ U+ E
h:%i:%s %x');    8 o* P. F' x6 J3 |* h
  -> '1997 23rd december 03:43:30 x'    # h  X, F5 ~! j

( v6 ^4 [7 O8 Q/ C5 A# F* [sec_to_time(seconds)   
0 Y! g& |" @3 D- k# _以'hh:mm:ss'或hhmmss格式返回秒数转成的time值(根据返回值所处上下文是字符串或数字)     ' J/ ~1 J3 n& r
mysql> select sec_to_time(2378);   
- p0 @6 Z1 t  s  -> '00:39:38'    # I. I" ]0 X5 L. \: L
mysql> select sec_to_time(2378) + 0;   
9 \/ b* {/ Q# d; J, ^  N  -> 3938    # I2 \, k1 F/ d4 d7 M0 X0 Q

7 T, @7 y. w6 }, h2 c# `time_to_sec(time)    1 ^9 `/ I9 U. ?" h! W3 I
返回time值有多少秒    3 O6 F2 {( z; y+ O  j& r
mysql> select time_to_sec('22:23:00');   
) T6 X1 x( p/ r+ L9 D/ r  -> 80580    7 }- I8 r) O# O1 p) h3 n$ ?* G
mysql> select time_to_sec('00:39:38');   
, J4 B% A+ R. q9 L0 i  -> 2378 4 d3 |# f; U+ G
/ a6 z* D3 J. \6 h% o
转换函数. X; S) s5 @; o( [( P
cast8 U# J# X9 ^- k6 w' t9 n) o* o. e
用法:cast(字段 as 数据类型) [当然是否可以成功转换,还要看数据类型强制转化时注意的问题]
4 o  [, w! L2 ]7 l2 E0 g: x实例:select cast(a as unsigned) as b from cardserver where order by b desc;- V% w6 n' [! q% j4 t2 B
convert:
9 \1 V3 \# S( w  d6 ?1 X用法:convert(字段,数据类型)
2 @" B- m+ o+ z实例:select convert(a ,unsigned) as b from cardserver where order by b desc;
  _+ V9 s" K% k2 L% _/ z
回复

使用道具 举报

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

本版积分规则

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