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

mysql常用函数大全

[复制链接]
跳转到指定楼层
楼主
发表于 2018-3-21 16:07:02 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
1、字符串函数
' \* P1 i% h7 A2 Cascii(str)   
8 v: ^& j; G- ^2 E返回字符串str的第一个字符的ascii值(str是空串时返回0)  - x0 D9 {) C2 P5 K3 O; h  x' Q9 q6 {; H
mysql> select ascii('2');  - {. ^* j! K* g8 s5 u# t
  -> 50  
- V5 B$ S" }$ }" vmysql> select ascii(2);  
7 a2 I. J6 t- `  }* {! o  -> 50  
& f3 m% a( ^, G5 Xmysql> select ascii('dete');  
) p+ n. x9 {0 C' x7 o( [  -> 100

. u& o5 i; f; ^; F! L8 |* yord(str)   
3 X/ L& ]4 T9 \  X8 P5 c0 z+ C如果字符串str句首是单字节返回与ascii()函数返回的相同值。5 w3 @$ w* j5 `& d/ M& v

- T) V. f4 @* ?- {7 Q9 e4 Z如果是一个多字节字符,以格式返回((first byte ascii code)*256+(second byte ascii code))[*256+third byte asciicode...]  
5 C' R; E- Z0 Z5 G1 ]7 @) cmysql> select ord('2');  , p# S" \3 q7 s  r" O# T) W" C/ V5 R
  -> 50  * B# P" z% {% k  ?
   : c/ s! q( m6 D
conv(n,from_base,to_base)   
( O4 U! Q* r* [4 X对数字n进制转换,并转换为字串返回(任何参数为null时返回null,进制范围为2-36进制,当to_base是负数时n作为有符号数否则作无符号数,conv以64位点精度工作)  
! o! D. W8 ^/ m2 s" I( k# n- I- Xmysql> select conv("a",16,2);  # U9 g8 t0 d3 @" J8 B5 x& D+ w
  -> '1010' 1 S; A" j! e* K3 i9 H7 w
mysql> select conv("6e",18,8);  8 w. b0 u  f; J! f
  -> '172' : z/ }& m8 B  Z$ |
mysql> select conv(-17,10,-18);  / N9 R, d1 w- [- x7 D# h
  -> '-h' 0 o2 c8 J- _5 y" _: w' J6 S
mysql> select conv(10+"10"+'10'+0xa,10,10);  6 S* Z* o1 }7 W& ^
  -> '40'
. {6 i! ?; N6 S. F/ N- d4 z& y   
  q! B1 H8 Y$ ~bin(n)   2 Q( x- a6 c. ]8 o
把n转为二进制值并以字串返回(n是bigint数字,等价于conv(n,10,2))  ( Z  z6 c6 c; N- H- A$ ?
mysql> select bin(12);  
/ t) W0 h: ~0 N. g! l- U6 d  -> '1100' , N7 Z, }- C' S. }' Y& f

$ Z7 }/ p) n5 F. poct(n)   # T2 x* I  K- c4 @: Q
把n转为八进制值并以字串返回(n是bigint数字,等价于conv(n,10,8))  + H" t" d0 [9 L# l
mysql> select oct(12);  ( R  u* A; K% ~' m1 E/ V# K7 g# R
  -> '14'
( x. G  l" u# e; `8 X% C  u9 E# F   
3 O, h' a! R% X* m7 l. s4 o; m. Zhex(n)   % p& M* k, w6 E8 [6 w
把n转为十六进制并以字串返回(n是bigint数字,等价于conv(n,10,16))  + c! u- M/ ]% \: o4 T, z
mysql> select hex(255);    b3 r6 L& m7 o4 g/ Q
  -> 'ff'
% o9 Z5 R1 p( C   
8 B0 F- l1 K/ k9 qchar(n,...)   
6 d/ h8 c& b' i3 t返回由参数n,...对应的ascii代码字符组成的一个字串(参数是n,...是数字序列,null值被跳过)   
4 V9 n: g3 J! m  W. P; imysql> select char(77,121,83,81,'76');  
2 b& n$ i0 c9 G. d% C  -> 'mysql'
7 w. E2 ]# s' {mysql> select char(77,77.3,'77.3');  9 Y' i- a& I, H' u+ H( \9 V
  -> 'mmm' 8 F7 b: S- }0 Z( O1 p+ Y3 h
   
4 X$ P3 g  u3 P) Rconcat(str1,str2,...)   0 F$ D8 r% ^+ L! F! W- f! I$ s
把参数连成一个长字符串并返回(任何参数是null时返回null)  4 G/ P7 p! _% r' S3 r
mysql> select concat('my', 's', 'ql');  ; M, B1 }2 J1 |$ [; w+ y/ c8 F
  -> 'mysql'
* r( @& O' I* |) K6 cmysql> select concat('my', null, 'ql');  
, S! C8 m: N0 q  -> null - S7 n# I. @2 u& ?% w* B
mysql> select concat(14.3);  . {; Z& l1 s1 e# p
  -> '14.3' ) f- x, O6 I, S4 o- R' Z

8 t% y$ N: }0 ~2 i  l" hlength(str)   
- t- _1 a- ~$ f. B# [octet_length(str)  
. y0 N5 k  Q0 y7 f2 B/ t$ Fchar_length(str)  
5 f* c" P$ {  r$ T; N+ O5 o$ g" \2 @character_length(str)  ' f9 k2 g9 u6 g! X9 {: g
返回字符串str的长度(对于多字节字符char_length仅计算一次)
3 Q  c- q/ E! L4 Nmysql> select length('text');  
, r" w2 n" V8 [  -> 4  0 a  O9 l9 m1 w# X  W
mysql> select octet_length('text');  * \: N  {* \$ h, w; U# ]0 Y
  -> 4  3 m% _8 t1 t. q7 L) i* U* @

" L! g" d/ V/ |; elocate(substr,str)   
7 y: `3 F( A, l* _2 w9 W: hposition(substr in str)   7 G" T: s$ A& K3 ~
返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  $ i# l8 p  }3 L  Z7 O. E
mysql> select locate('bar', 'foobarbar');  ' a* _2 \& L5 b1 g! Q
  -> 4  
$ y+ ~( V- X( rmysql> select locate('xbar', 'foobar');  
2 K3 k) s( J! c& e$ k  R. L+ W  -> 0  
' E0 e0 j2 l# }3 g* Y1 ?& c   
* h7 Q+ D/ Q1 g2 T: s# u5 B/ }locate(substr,str,pos) , @3 Y& S" L( B+ `; S
返回字符串substr在字符串str的第pos个位置起第一次出现的位置(str不包含substr时返回0)  
) U* l/ _( f* w# D1 @! Kmysql> select locate('bar', 'foobarbar',5);  % s1 G; p& _) X: M! d3 Y" i
  -> 7  
; d8 f; a- |5 c
2 m& Z. C! k8 Z  \+ R3 N! Rinstr(str,substr)   , g# L% o3 o; w5 l' ]" d
返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  
' t6 W$ H( y4 smysql> select instr('foobarbar', 'bar');  
5 [6 U. N9 ]9 }  -> 4  1 l2 M1 V, B: C4 V' A
mysql> select instr('xbar', 'foobar');  & c# T6 x6 [% _0 y( E
  -> 0   
" ^: J' [3 Q+ b# D 1 q) I, r6 j; p
lpad(str,len,padstr)   1 ^4 M4 u0 K. p5 y3 U
用字符串padstr填补str左端直到字串长度为len并返回  
( s: S3 d8 _* J8 E7 ]( E5 omysql> select lpad('hi',4,'??');  . U" s" i8 O! Z+ e' @& L4 ]: o8 c1 G
  -> '??hi' + q) p0 t$ g: j, M" A( |& s
   
! r5 C9 ]' Z* o6 j9 lrpad(str,len,padstr)   . R8 {% a* `- I" L) J$ k5 p3 I  H
用字符串padstr填补str右端直到字串长度为len并返回  3 Z$ i; U) ^( Q2 ]0 ]# w- S
mysql> select rpad('hi',5,'?');  
0 G9 g4 S5 n. c8 d* D) e" Z  -> 'hi???'
; M* @" l' Q- D; ~ 1 h+ m0 q# W! W7 F" r( t; [0 A
left(str,len)   
9 [- j1 L4 i* @& U1 N$ X返回字符串str的左端len个字符  8 c% k! [3 u; I1 P' p% d( l0 J
mysql> select left('foobarbar', 5);  6 }: A/ D( k9 x  N! a" |0 d
  -> 'fooba'
. R# P8 b1 y, C% o  ] ) f! L" x: [* I( h5 U3 r
right(str,len)   
6 E+ ^, ?2 m+ r& }3 P3 V/ p8 i9 ~9 J返回字符串str的右端len个字符   
" Y. p5 {- D' ], y6 i( A" e; Imysql> select right('foobarbar', 4);  
0 Q# o- V- [9 o+ f  C! q  -> 'rbar'
9 N4 Y! b4 d8 ~! `; J5 j
- c9 I; M( V: E+ a: k5 nsubstring(str,pos,len)   # Y; B' d6 ^6 D2 A7 b
substring(str from pos for len)   6 U, x& B% c; h/ m- i
mid(str,pos,len)   
8 i1 D) n3 a- c. n" R) S% w6 X返回字符串str的位置pos起len个字符mysql> select substring('quadratically',5,6);  
- n' `+ f2 c" o  p) |9 [  -> 'ratica'
0 ~0 y' Q9 D: d/ R2 |* G+ f' d
$ K, Z. A4 Z$ e/ k+ p# a7 A8 Y- lsubstring(str,pos)   7 S# c  i" U0 D# B- x" e4 @2 j* Q
substring(str from pos)   
3 k( v$ _6 o: B返回字符串str的位置pos起的一个子串  ! U: S3 P( Y: D! k$ h
mysql> select substring('quadratically',5);  
  S9 Z: c( D, I, k$ h  -> 'ratically'
9 d& S3 A6 ]6 H- |# \: Omysql> select substring('foobarbar' from 4);  8 |: K2 L6 Y5 P% B, I
  -> 'barbar'
  b' I# V3 s  F" J# m
- R6 K. e; r7 S% I! r7 gsubstring_index(str,delim,count)     {- X& Z0 B9 w$ @$ ]) h: F
返回从字符串str的第count个出现的分隔符delim之后的子串$ _/ s4 x9 p  V4 [7 D+ A
(count为正数时返回左端,否则返回右端子串)  
2 L2 R3 h7 X+ ]+ b. N8 X9 amysql> select substring_index('www.mysql.com', '.', 2);    T" B4 ]2 m& }& [' e$ f* G
  -> 'www.mysql' 4 l  F; s6 }+ `! z0 K4 J# ?# G8 m
mysql> select substring_index('www.mysql.com', '.', -2);  ' D& u* C4 E+ e& W- \$ o
  -> 'mysql.com'
8 [/ ^/ u2 [3 W0 O! A 3 R9 ]* A4 L7 E- {7 s
ltrim(str)   $ h$ b5 i  Y% [) q& h. a! l
返回删除了左空格的字符串str  
1 o0 ^' z/ e/ n6 Umysql> select ltrim('  barbar');  % q' p, P) ?6 t" {# J/ [! p
  -> 'barbar'
6 i1 V1 S) [- r% U( O  @; n
, s$ L% D/ v( c8 _  D; P! m1 urtrim(str)   
! {/ u' v1 U4 ?2 q  s3 c返回删除了右空格的字符串str  8 w8 o; P3 R" f0 X9 E- _1 B4 @
mysql> select rtrim('barbar   ');  
& a. g/ S) o3 S2 C+ G; g  -> 'barbar' $ |+ V5 W3 ~6 m
0 T" ?& k: f; _! L6 {2 j/ R8 @' O
trim([[both | leading | trailing] [remstr] from] str)   7 W( N" G7 K. F5 Q
返回前缀或后缀remstr被删除了的字符串str(位置参数默认both,remstr默认值为空格)  
# }) q/ h5 d  F+ n' {mysql> select trim('  bar   ');  
  M  k. S) O7 \" s  ~9 Q  -> 'bar'
5 a  Q' q5 Y( Y4 z) imysql> select trim(leading 'x' from 'xxxbarxxx');  
. H: R& R& C8 @2 i8 m6 Q+ M  -> 'barxxx'
8 t5 _* P" T+ a3 \% B) ^# @; g5 f  Zmysql> select trim(both 'x' from 'xxxbarxxx');  7 r- I' I( S, P% ~& s
  -> 'bar'
( R7 I& r; `, z( Z* Pmysql> select trim(trailing 'xyz' from 'barxxyz');  2 h2 z9 M* L' X" }. h6 Q4 J& j/ V7 ]
  -> 'barx' 4 ]# F" X! D: o8 C4 r: Z: i$ N

3 g7 j1 {- @& b0 zsoundex(str)   & n. ^* F, |. n5 ]" D
返回str的一个同音字符串(听起来“大致相同”字符串有相同的
; U+ i  ~0 E2 @3 m% V' R% v2 v1 W同音字符串,非数字字母字符被忽略,在a-z外的字母被当作元音)  
9 E1 s- V4 I2 F" m$ M1 Ymysql> select soundex('hello');  2 }' e* Y' g" J, @/ n! e- K) a
  -> 'h400'
( v( L1 Q+ Y6 b, imysql> select soundex('quadratically');  ( U  G" F; K& \+ M
  -> 'q36324' # @) w2 w$ w3 f  E' z
   4 `  a* }/ C7 ]
space(n)   
$ b2 I" R" M0 q% F( \返回由n个空格字符组成的一个字符串  8 ^# I7 `# k- @2 q$ s& Q4 I  t
mysql> select space(6);  
, z7 s; d+ x- Q0 q$ g# A  -> '      '
1 K. {+ _6 q9 Q# o& p3 s   
7 Z5 A: \  ?4 W* F9 sreplace(str,from_str,to_str)   
8 b6 G: G: W8 F& O. {用字符串to_str替换字符串str中的子串from_str并返回  
1 B. \- r# r! d1 z+ B, Nmysql> select replace('www.mysql.com', 'w', 'ww');  
7 q" K3 y3 K9 s1 g  -> 'wwwwww.mysql.com' 2 I* \0 O! p+ o2 s

9 W6 f: }1 F6 ]2 J! i/ p( m7 ~/ `repeat(str,count)   0 v9 f% V3 Q( A. i/ J& q6 k
返回由count个字符串str连成的一个字符串(任何参数为null时% ]: L% T  v( q0 ?  f7 X, I8 G
返回null,count<=0时返回一个空字符串)  
$ N6 ^6 W/ p- ^mysql> select repeat('mysql', 3);  
- @% f& v! b( N: Y% P3 c, F  -> 'mysqlmysqlmysql'
: v7 K7 e0 y3 J   * P. ^4 U  \8 ^# U% F8 _4 e& b0 L
reverse(str)   ; b  ]6 Q" m) K4 y4 \! S( P
颠倒字符串str的字符顺序并返回  ' s, P6 ?$ T9 r2 H' o# u
mysql> select reverse('abc');  0 l1 L* V* N6 F. A6 Q: t( H
  -> 'cba' ' c/ |! ~# L$ f  b: r; Z1 ?6 C
9 j0 j1 U4 v) n& M& O* }
insert(str,pos,len,newstr)   , u7 p/ I/ R2 z$ J& ]2 Y0 h
把字符串str由位置pos起len个字符长的子串替换为字符串
, J; n6 O6 H/ a: G( X/ Inewstr并返回  
8 N3 r" q4 d, r) J& @% Y5 Ymysql> select insert('quadratic', 3, 4, 'what');  
! P" Z$ y4 k  I  -> 'quwhattic' " Y' ]& J% C) Y0 `% I; ?# k
' [3 r! X9 }' Q: T$ @
elt(n,str1,str2,str3,...)   
3 D* _& G% O: m2 s& ~9 Q3 {! a返回第n个字符串(n小于1或大于参数个数返回null)  3 ~5 o! v4 K5 V8 K  \
mysql> select elt(1, 'ej', 'heja', 'hej', 'foo');  % Z; r) V2 H* T# h. n) n
  -> 'ej'
9 n9 C6 ]9 p* r# `) Q6 umysql> select elt(4, 'ej', 'heja', 'hej', 'foo');  5 b: z! w1 |" v9 J* Z0 {
  -> 'foo'
0 l3 x/ ]; f0 w: X% @7 G ; a$ |7 C; {- R# F& F+ |
field(str,str1,str2,str3,...)  
" N; Y  b# v; O7 n+ P  }返回str等于其后的第n个字符串的序号(如果str没找到返回0)  * q6 B/ D! Y3 u7 F. J
mysql> select field('ej', 'hej', 'ej', 'heja', 'hej',
, K! g5 f* Q! ^1 h'foo');  
+ f5 |- V6 `# h1 J# `" p, t# Y5 ?  -> 2  
2 f0 P+ E6 m4 Z  q5 g. vmysql> select field('fo', 'hej', 'ej', 'heja', 'hej',1 s* U. {8 y" X! V2 {' r
'foo');  
9 C) s7 J, R: n) P  -> 0  
, x( {( i) @. k( N2 V- p9 X) v1 N " q# [1 `3 e) l2 w. o
find_in_set(str,strlist)   $ S" R: ]' G' w2 }' O
返回str在字符串集strlist中的序号(任何参数是null则返回
  Y1 D% p' ^; X. @& Enull,如果str没找到返回0,参数1包含","时工作异常)  
; W* L$ T9 D2 o6 G6 Emysql> select find_in_set('b','a,b,c,d');  
  F# I: f2 i# [( N" C7 w  -> 2    [; M6 Q( V  |+ N% g
   ( |1 }1 {9 ?6 z
make_set(bits,str1,str2,...)  
! B& ], @2 v+ k4 ^把参数1的数字转为二进制,假如某个位置的二进制位等于1,对应
5 E6 P7 D2 h) ~7 i位置的字串选入字串集并返回(null串不添加到结果中)  1 p1 h# l' l7 s
mysql> select make_set(1,'a','b','c');  
4 T+ r8 E! ?3 T; i2 C5 s2 s  -> 'a' ! ~) h7 B% n0 C$ @/ U2 L
mysql> select make_set(1 | 4,'hello','nice','world');  
/ B+ g& n4 l$ \5 W/ y  -> 'hello,world' ; F# f3 O" d# {2 e9 z
mysql> select make_set(0,'a','b','c');  ) F% `6 t! ^$ N& e7 ?) g) x! W" S. w5 A
  -> '' 4 D) x3 x: \* Q+ G  p
5 x! b2 l& R/ z+ `
export_set(bits,on,off,[separator,[number_of_bits]])   * U9 L9 ^  D  v4 b+ V. z. K/ K: Q% D
按bits排列字符串集,只有当位等于1时插入字串on,否则插入
3 r" K5 c: c; n8 Eoff(separator默认值",",number_of_bits参数使用时长度不足补0
. U1 E  l/ ]! [6 u, |& i7 Q而过长截断)   5 \& {3 ?5 S, H' C1 p7 D7 [
mysql> select export_set(5,'y','n',',',4)  
! [1 [3 C. M, O3 h2 g  -> y,n,y,n   
+ t: U, ~8 G! S" V/ c
: Z% a- i! [5 P( flcase(str)  
! ^% I: Z' |2 k' A8 Slower(str)   / y5 Y4 i( @+ Z3 T; h* G6 m! D
返回小写的字符串str  
( J+ \; d" T: w" L' N  B% f) a1 Mmysql> select lcase('quadratically');  $ v4 R/ G( v/ t5 o7 P* K
  -> 'quadratically'
2 i- I9 }( `- a# e9 f. ^/ l8 U   
3 x2 u! ]! F7 l3 T( Jucase(str)   5 p  I6 h& b1 z# D) `+ B7 L
upper(str)  
# ]  V9 z$ a, L$ h8 q8 v返回大写的字符串str  1 W/ m( A* l* \+ }5 |
mysql> select ucase('quadratically');  
8 i* v. [5 \+ B2 D, z5 ]/ x  -> 'quadratically' 7 K8 ~& j7 K: A/ g: S# T- r0 D( H$ }

" l* C, k5 d: D# w9 [' wload_file(file_name)   ; ]7 Y: f+ p' h  L7 M. |
读入文件并且作为一个字符串返回文件内容(文件无法找到,路径
, y  g, r/ t2 m不完整,没有权限,长度大于max_allowed_packet会返回null)  * R0 {" t& t* ~' P' V
mysql> update table_name set blob_column=load_file! K# p5 d6 s; C' L$ f# {; H0 H
("/tmp/picture") where id=1;  
1 N5 {7 p- c9 `4 z% a- b4 G
4 [6 _0 z6 c& K" p) ?, X! m2、数学函数
' `% n9 t, F: c  {3 n; }" pabs(n) & W* d5 t! A; C" `
返回n的绝对值  
4 [) ~& ~1 U% c4 smysql> select abs(2);      {5 ]2 k, R" ?$ t4 B
  -> 2    . J! \$ [$ t! `6 `6 d- W# g
mysql> select abs(-32);   
0 t: V# I2 U0 p5 j. @, h/ y7 V  -> 32    ' R. i% v* G5 a3 M+ r
   % ]! \; V& ]( _+ p3 Q& h
sign(n)  
4 c. n+ t5 t: ^返回参数的符号(为-1、0或1)  $ i( R5 `$ x4 O* e
mysql> select sign(-32);   
6 ?3 R) r! r" l/ ^  -> -1    ! H4 j) J; A8 a
mysql> select sign(0);   
9 W' h0 K0 }$ T2 @$ Z/ q  -> 0   
" J- l; e8 `3 b6 C+ ~mysql> select sign(234);   
& I+ T5 @$ U8 w& j6 l6 Y  -> 1    8 p) E" b. O! `7 c; x

/ Q8 h  f9 U& T& Y, fmod(n,m)   
& W, v; l6 n4 l取模运算,返回n被m除的余数(同%操作符)   
9 G  s1 I$ E: Umysql> select mod(234, 10);    # _% y1 ?* h9 g1 ]
  -> 4   
7 ?) ~% [9 Q9 Jmysql> select 234 % 10;    ) t6 h* O$ @" |5 ^/ v8 v# }- K
  -> 4   
/ f" p( j& l. V1 lmysql> select mod(29,9);    & \* e  {$ n' U
  -> 2    4 Z1 V' }! ^6 i. W( I5 ]  G
" K  t+ P- x( a/ Q! K
floor(n)  % K$ _# f2 k% q1 I5 p& d( T9 ^) {
返回不大于n的最大整数值  
5 z* j9 I# R! `' r, p" c# W2 Jmysql> select floor(1.23);    * |& k9 K$ E% ]# I4 h# u2 c3 R
  -> 1    * F: K8 v# _9 ^9 `* F2 h9 z" x
mysql> select floor(-1.23);   
8 F5 l* t/ ]: D  _9 g* L- w* _  -> -2    6 ~# L1 o$ ]7 i5 n+ r

1 ~: ~' }; k" tceiling(n)  
2 P3 J0 W7 Z- Z7 b返回不小于n的最小整数值  . i/ N' Y+ E9 q
mysql> select ceiling(1.23);    ; R  d* R( C( ~! `$ b8 D) Z+ ~6 R
  -> 2    ) |6 E2 C" C" J5 j, }
mysql> select ceiling(-1.23);   
! s7 a2 L2 D7 N, b% ]6 M, U+ K  -> -1   
9 j: H$ V9 W+ f
- A% s* h+ z! lround(n,d)  & E' a6 I# Z% Q  d5 F8 w7 C* q* Y
返回n的四舍五入值,保留d位小数(d的默认值为0)    Q# O4 {3 d, y  V
mysql> select round(-1.23);   
, e4 g) `" l8 x+ H1 s; i$ p  -> -1   
  n/ h2 g9 h0 r7 m+ Omysql> select round(-1.58);    * i6 U# N" j5 p$ |( Z; [
  -> -2   
0 E- E+ m- j, _9 ?* B6 Vmysql> select round(1.58);   
! H! c' E; S- ]1 Z' ^4 M  -> 2    6 }8 w( B+ c/ k7 N/ |
mysql> select round(1.298, 1);    5 W& o& t0 n! ~2 e0 d  q
  -> 1.3    ! t' ], O3 b/ H1 i+ c" I% ~
mysql> select round(1.298, 0);   
2 n! S, ^. v- }/ B  -> 1   
1 }0 w1 R+ o1 G2 p) a7 `* _( X  | ( ^0 S+ [: [1 |: w6 Y+ S
exp(n)  * s1 g# ]" k9 C" d1 R
返回值e的n次方(自然对数的底)  9 Z& ]& C/ Q! m1 }) n
mysql> select exp(2);    : @: {9 }; D  T9 F" J- v7 b
  -> 7.389056   
" L: F2 F: s( S% fmysql> select exp(-2);   
9 c2 M8 o8 c4 }/ Y) q0 S  -> 0.135335   
! q' T% M0 J8 q+ W& v' u # J. l7 I6 R/ ?! f( j
log(n)  # S4 C: T/ c9 G# {$ `+ b
返回n的自然对数  ( }+ Z1 k" {& X& O' f" S
mysql> select log(2);   
6 R, v+ L, H4 S4 ]& S; m  -> 0.693147   
: T, {1 I0 g+ w& I3 W5 `) hmysql> select log(-2);   
" M. u# `/ O' o% [) D  -> null   
: j6 G  [- R8 u3 c9 ~. {7 `
" z. J& O* i% M* d5 slog10(n)  
+ b* y& }3 Y1 h2 ^6 k: X) w% k6 z返回n以10为底的对数  8 U& _: e( Z6 u6 I9 [& T9 n+ |
mysql> select log10(2);   
- X% W3 z3 R: b! i  -> 0.301030    0 g* q; W& d  Z$ e( J7 ]+ p
mysql> select log10(100);   
: L; e) t; c$ N4 V7 B9 U  -> 2.000000   
0 l8 \+ k% P* W/ p6 bmysql> select log10(-100);   
4 E$ P, r+ _3 O: [  -> null   
" t% y& ~" J; ?0 G1 h( K2 S/ P& ]
3 j' S' h; o- `7 upow(x,y)   
1 H1 `* Z$ z/ J8 L7 Npower(x,y)   
! X  d+ G3 M8 I3 } 返回值x的y次幂  1 ]/ V; C) g" Y, ?9 _+ G; Q0 ^
mysql> select pow(2,2);    & H" U) @2 ?- D/ t9 Q3 T
  -> 4.000000   
  _, v5 f, Z1 N6 |mysql> select pow(2,-2);    $ m: ~6 }) ]% o9 H
  -> 0.250000  
( y' M9 y, k4 z$ d! }
4 I3 a" V' H# ^sqrt(n)  " W0 l5 _* `, X* C% g9 g
 返回非负数n的平方根  % [+ e* ]3 U# I9 f
mysql> select sqrt(4);   
! G6 _+ u# ^5 ?/ j- Z  -> 2.000000   
  C; M( T* x+ N) {: z6 lmysql> select sqrt(20);    9 }3 ]! i5 f( h/ z: z6 r  o: S. W
  -> 4.472136   
+ g6 `% q" L5 d. Z9 w: i * ~) T% {/ o- c
pi()    3 q1 h5 U+ ?* e7 e
 返回圆周率   4 ~2 D3 K8 E6 [! l7 F4 l1 g: h
mysql> select pi();   
- G8 c8 n. R6 p4 L( n+ x  -> 3.141593    , U) }$ e' z: K  o$ D8 W& H

' N9 m2 L4 i1 m5 X+ d; pcos(n)  ( i/ U4 n3 q& j
 返回n的余弦值  
( C; p, n, `8 {8 kmysql> select cos(pi());  
1 ]  {5 x; a! A  _; E! @. @  -> -1.000000   
) W2 e* Q  F4 } % ~. v1 S# C; |5 f" e4 |
sin(n)  
5 ?) o2 _2 d; y$ g" { 返回n的正弦值   , P& P$ q* P2 g) [* C% o0 O4 A, |
mysql> select sin(pi());   
" O! m/ `9 a: b- Q- m0 `  -> 0.000000    & f4 y' {$ ^0 R" s9 P

# a: Q8 R3 _. |( s6 s' B5 q0 qtan(n)  
5 R3 q, t1 f5 p返回n的正切值  
3 T9 L; ]1 J+ r  [mysql> select tan(pi()+1);    , A8 I! }  j4 x; |: x# f4 `# |
  -> 1.557408    3 l: k# i+ c9 b# A1 f

# `& _; q/ j+ }acos(n)  
8 j* k( v; R( y6 C 返回n反余弦(n是余弦值,在-1到1的范围,否则返回null)    D( f9 Q4 s+ A1 H" j3 P/ \
mysql> select acos(1);    ) Q( P+ e7 [$ c+ I+ j1 F
  -> 0.000000    1 P, {% u1 {; D$ A3 n3 G
mysql> select acos(1.0001);   
5 ~( A2 j; a* b4 m/ v: Y6 X" A  -> null   
+ K/ |% ]0 I# C# z+ T: d" J) mmysql> select acos(0);   
+ N1 i0 U9 k, L6 z1 g" |) i  -> 1.570796    # n- I6 U" r- h7 t9 P7 D2 T( E
9 M% c. ]+ E' m+ e( o
asin(n)  8 S% l& _. L0 J8 |* t; G! P9 [. }
返回n反正弦值  
) B" s* r9 |4 J4 e5 o7 D( h+ |/ wmysql> select asin(0.2);   
5 [& p- Q7 y$ k! @; u$ g3 }  -> 0.201358   
: ]2 g3 r# b8 y! c% X% ymysql> select asin('foo');    ( t* N; e+ @3 `. t
  -> 0.000000   
4 h9 m4 A+ n: o1 p, K- e + i  E. v$ }0 _
atan(n)  
" X' X4 }6 ~/ q4 J7 p- \# E' L' p/ e返回n的反正切值  
; _' z* o  @' Y/ @4 }2 dmysql> select atan(2);   
$ r- N- D' |# b2 Z  ?( ^2 Q  -> 1.107149    - }* A9 Z0 H/ H6 Y4 T
mysql> select atan(-2);   
6 X1 e8 Z: N' T) o- `' `1 c  -> -1.107149    7 J& ^: P/ H2 l4 t) L9 J
atan2(x,y)   
' A' y0 m3 J# |1 h! L8 r 返回2个变量x和y的反正切(类似y/x的反正切,符号决定象限)  4 d! N6 O% U- @
mysql> select atan(-2,2);    ! H. h& X1 Z0 w' B$ U( B, O* t
  -> -0.785398    ! k3 q- h8 K- ~% l8 O: y" L- [
mysql> select atan(pi(),0);    " l3 o3 ^3 B& D1 ~" J) _
  -> 1.570796    5 [1 V5 I1 I  ?0 c5 F: ^( [& t

& Y: w# w6 S+ c; B# jcot(n)  , c$ K, \% I9 R# b/ l8 y
返回x的余切  
; m, a1 q( n* q7 Z( Bmysql> select cot(12);    % w7 r) ^6 r3 e8 A% ]9 l
  -> -1.57267341    $ U$ X( }& j, v8 y5 Z6 x" m
mysql> select cot(0);   
+ \# H' u- s* q: W5 [  -> null    $ d. o" m, g3 p6 i3 \, o
5 `0 V( F7 ^/ r1 t- d  x& j4 X
rand()  - D4 ~5 m4 j" t4 ~6 E+ I& N1 C
rand(n)    3 o# q* H  v7 ^
返回在范围0到1.0内的随机浮点值(可以使用数字n作为初始值) $ x( o7 z8 C" ~7 O

4 [. m% l: N5 dmysql> select rand();   
9 F3 D- l' Z7 q8 K* K9 F  -> 0.5925    " d; T; I1 ^9 n0 ?( A& u0 T
mysql> select rand(20);    $ Q; a- _6 r. z' p; h' \# l$ W! q
  -> 0.1811    * E  Q) `% D: E( b9 M
mysql> select rand(20);    : K) R" Y" t5 ]8 X- R5 F" @
  -> 0.1811    ' S/ L( \: C; Y! ^. g; u5 j  _
mysql> select rand();   
7 }6 O0 \3 a" L& y- n9 x$ g  -> 0.2079   
% L8 H1 r6 }  n9 p: O  W/ Wmysql> select rand();    % P% N. S( {! {: L8 s
  -> 0.7888    2 b+ z! |+ x+ l& k& G- c

6 h+ y$ e; k! m# jdegrees(n)  7 c: G7 g$ h/ K8 g, @5 ^( u9 ^
把n从弧度变换为角度并返回  
9 |. Q$ ^! H8 X2 Jmysql> select degrees(pi());   
& d4 `+ d- s3 F3 y. _- l: z- _  -> 180.000000    # C3 h' H7 |$ D( J& g4 `8 K+ s
2 s5 z$ g/ f% q8 j# k. ]5 ^2 S
radians(n)
' X& p' g& T) u) m" @把n从角度变换为弧度并返回   
, ~0 I% b$ c' v: \- p+ {mysql> select radians(90);    : K) H3 {( K$ c1 I+ K- H! D
  -> 1.570796   
8 O3 \4 o( [% ^5 G4 O! X0 z, b' ?9 `9 ^5 j0 `
truncate(n,d)   
. L/ k3 c9 N; G6 P- y保留数字n的d位小数并返回  
: x5 k# C0 Y6 u( p# omysql> select truncate(1.223,1);   
  z- Q" ?# r# {2 J; Z; l  -> 1.2   
. q$ _( h6 [0 d8 H5 p# rmysql> select truncate(1.999,1);   
" r# O( b2 n! A' u0 X  R  -> 1.9   
0 g. u/ q4 e$ S+ O( w3 ^mysql> select truncate(1.999,0);   
2 y" B4 u* K6 I3 Y  -> 1   
. j& B3 U! ]* z
. X( G2 g4 Y8 H& |+ N6 Zleast(x,y,...)    4 n9 j1 A- ?4 I
返回最小值(如果返回值被用在整数(实数或大小敏感字串)上下文或所有参数都是整数(实数或大小敏感字串)则他们作为整数(实数或大小敏感字串)比较,否则按忽略大小写的字符串被比较)  
7 w$ \9 y1 |9 J, u  ?1 t$ A% qmysql> select least(2,0);    & H, _% Q, G0 V: J6 s9 Q
  -> 0   
9 v/ c* e- @( L/ B! ]8 Ymysql> select least(34.0,3.0,5.0,767.0);    ! b! P* O9 \4 R' [' Y
  -> 3.0   
4 j/ X4 @* B2 W! m2 Nmysql> select least("b","a","c");   
4 C' C4 m+ m4 B8 a  -> "a"    4 x9 a) V/ |; @+ N- C% U

# r4 h7 S4 Q: f- ~* q/ Q/ Zgreatest(x,y,...)    8 e9 L- f  ~- |) G, h" v- q
返回最大值(其余同least())  9 l  Y- I2 V! {* R
mysql> select greatest(2,0);   
' A7 ]% L) f. x  -> 2   
& Z. ?; e: d6 nmysql> select greatest(34.0,3.0,5.0,767.0);    : d8 X  T& M9 w9 h4 M  r6 \( c
  -> 767.0    . F6 Y+ ?* o% z
mysql> select greatest("b","a","c");   
" n7 \. V6 v3 C% Y  W6 ]  -> "c"     , `5 z7 u7 n$ h2 @

* R  }4 ?& t" m+ b3、时期时间函数
9 S% y9 M" Y$ M" r; _dayofweek(date)    " L) v( Z0 p. \2 v
返回日期date是星期几(1=星期天,2=星期一,……7=星期六,odbc标准)  ; }8 H0 A; c5 A! P+ }7 j
mysql> select dayofweek('1998-02-03');      n+ ]2 X, z& R+ g
  -> 3   
  q; z) l0 u. C+ R2 M7 X1 k! g 1 R  \# e  a. v
weekday(date)   
( n$ \+ Q' k5 o/ J: x& S返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。
9 q6 J2 O. B, M6 S% H' x  + z% y# A, r+ k+ V* @- s* D
mysql> select weekday('1997-10-04 22:23:00');    : d- E9 [1 ~: ]9 w
  -> 5   
6 t$ W6 X4 z# z* V! `mysql> select weekday('1997-11-05');    7 B# A) J5 Q5 E& V' V; p1 Z
  -> 2    & X, A# U! ]; `% g9 c

: G3 ~) o9 p) E, zdayofmonth(date)    # g- u( L! _4 C! j. Z
返回date是一月中的第几日(在1到31范围内)   
3 O1 o9 ?# Y' S; _; tmysql> select dayofmonth('1998-02-03');   
  v5 K4 R& I2 _6 {6 s0 W; u  -> 3   
+ g. }, S2 q/ i: @3 g; X 9 V  Y" t/ T9 Q' h. L7 _
dayofyear(date)   
1 g& Q8 |" }( |& o$ }/ Y! L$ E$ E  X返回date是一年中的第几日(在1到366范围内)    4 W: G3 b" ~  P2 r5 n. }
mysql> select dayofyear('1998-02-03');   
0 T8 A0 u  `5 \" @3 ~  c! q  -> 34   
  p# r0 C9 b4 g" Y
' t. M+ R- J  p- @month(date)    % Y- b- [$ T; p! h
返回date中的月份数值   
9 }3 J, z$ W7 B7 ?' t1 K0 U$ @mysql> select month('1998-02-03');    1 I; x8 d% Y3 s9 f- y8 X6 e) c4 U) b
  -> 2   
) F0 w( b4 y1 }+ |% M+ o: y
2 c: i8 i7 K; M$ t) |; fdayname(date)   
4 c! ^. d( @  Q$ @3 t7 c返回date是星期几(按英文名返回)  ) d/ b! }$ y( Z$ ]  j. j" e
mysql> select dayname("1998-02-05");    , w! d' F4 P$ v
  -> 'thursday'   
5 w: z* w) _! h# `0 l' @ * B% ~$ p+ t2 J, S3 `/ m( i
monthname(date)    
+ B2 Q& t" q/ c6 i返回date是几月(按英文名返回)  
! _$ z; |; m7 cmysql> select monthname("1998-02-05");    : W# n# ?* _* y. B2 a. ?
  -> 'february'    6 C* N5 S' B$ S
& [: F8 A  {6 L8 W! h- @+ E( n
quarter(date)   
6 V# m' V9 K2 H  b/ @. L返回date是一年的第几个季度   
0 |1 d) [" X% _8 wmysql> select quarter('98-04-01');    $ t9 q$ \- V' T7 l" C6 W: \6 {
  -> 2   
4 ]- Y2 m8 N9 F$ Z' U: x' E 2 c+ ^* T( y0 i2 L, ~1 L+ h
week(date,first)   4 V  J" j: n5 q" R9 |8 L0 {! g( K' i9 R/ ?
返回date是一年的第几周(first默认值0,first取值1表示周一是
& \/ x; [& o* T: _2 K周的开始,0从周日开始)  
0 w2 A6 ~7 r; ~" Xmysql> select week('1998-02-20');    / P: Y. j$ T( F% |/ `7 H4 B
  -> 7   
9 }& \6 O6 Y* U2 w, H* Wmysql> select week('1998-02-20',0);    2 s5 F! I- C( r) C# l2 E
  -> 7    6 v: f6 w0 b7 @- ~
mysql> select week('1998-02-20',1);   
4 S* m$ M( G: A8 `0 {! e/ s; t; o, v; r  -> 8   
; G! N$ u) [4 x& j) @; R5 d4 j
+ G% B& G& L5 {3 O( X' nyear(date)    - V3 u5 P3 d  A3 ]7 T6 H
返回date的年份(范围在1000到9999)   
) h6 O9 l% w5 |: O( V3 hmysql> select year('98-02-03');    + ~# t( y- b( E% i# \2 o
  -> 1998   
! x2 A( |# p& F) P0 K8 E 4 E  l" z  b& F% J1 R
hour(time)    & ]0 J4 g' B4 Z4 \5 k" O
返回time的小时数(范围是0到23)   ( h1 |; t1 o2 a. Y
mysql> select hour('10:05:03');    1 y4 F" D( K2 ~0 d3 b
  -> 10   
1 _5 I1 w. E* R9 r' U 8 i/ d! \+ g! Q" f" f  s. f  M
minute(time)    , P) I5 w7 e  ]8 @/ a
返回time的分钟数(范围是0到59)   
# \0 `. U* o* ^mysql> select minute('98-02-03 10:05:03');    + k" E3 {) c: J. |( W. E% r# ~
  -> 5    ; o. x9 a0 q+ m; D. n

  R. X( r2 @' csecond(time)    ; W% o! ^2 A7 f. s; F0 M
返回time的秒数(范围是0到59)   0 o0 i& I  |( G4 R
mysql> select second('10:05:03');   
( c5 x$ E, e7 J1 V3 t$ w1 S2 R( q  -> 3   
2 v7 t( u9 y+ i% ^9 |0 h$ Y
. ~9 G0 J/ b! `2 n: Kperiod_add(p,n)   
& U% L% S& d; J  L" l: `) Q2 u增加n个月到时期p并返回(p的格式yymm或yyyymm)    $ V- O$ i2 w8 ?4 m$ ?- F
mysql> select period_add(9801,2);    6 `) r% K/ }; B3 g
  -> 199803    - b; U1 d9 }* P/ _2 P

; t3 g  H4 u) Gperiod_diff(p1,p2)    & T( b; V7 U, H$ k! U. ?
返回在时期p1和p2之间月数(p1和p2的格式yymm或yyyymm)  
. |/ |, A' [( `+ Wmysql> select period_diff(9802,199703);   
) B; W; L% g) o  -> 11   
* H! a8 e- Z" K
7 {1 Y- x9 K+ D' F. }; P7 udate_add(date,interval expr type)  
4 b2 C, \; z1 J* i, d  cdate_sub(date,interval expr type)    6 z* E  Q2 O) ~5 w9 }; X
adddate(date,interval expr type)   
3 |8 d/ o+ _" G, H$ lsubdate(date,interval expr type)  8 e) d$ }6 ^( G/ Y* A3 t
对日期时间进行加减法运算  ) I3 f2 L/ N* ~
(adddate()和subdate()是date_add()和date_sub()的同义词,也
% w/ e" v. b" V" y可以用运算符+和-而不是函数  2 d; Z$ w& H' Q+ q# ]+ N
date是一个datetime或date值,expr对date进行加减法的一个表8 N( \1 }9 u# J+ h: [
达式字符串type指明表达式expr应该如何被解释  
4 n4 b8 Q0 P4 z4 G6 r% s0 g6 U [type值 含义 期望的expr格式]:  
1 |2 F: w& e# v2 R9 a second 秒 seconds    7 N* D1 ~4 ~* K0 O) A
 minute 分钟 minutes   
& q1 t8 C# p8 d3 Q, T# t; B hour 时间 hours   
4 t% Q7 p8 p4 E* L. o$ H day 天 days   
# ?4 \4 M" S. [, j. l# p month 月 months   
  l% ]% f" L/ R( w% C3 l# _ year 年 years      t2 Y) |$ V  p/ h2 g) l
 minute_second 分钟和秒 "minutes:seconds"    1 t3 [$ w4 s3 H: u) Y
 hour_minute 小时和分钟 "hours:minutes"   
2 H+ d2 P  F, t+ b$ W9 E& A day_hour 天和小时 "days hours"    ( W: B3 X; y$ H& k5 B7 |
 year_month 年和月 "years-months"   
( C) T1 E# Z% y( m! W hour_second 小时, 分钟, "hours:minutes:seconds"    $ a4 N" o6 |$ P! C7 A
 day_minute 天, 小时, 分钟 "days hours:minutes"   
  t; @" t8 k  E# \% E  P day_second 天, 小时, 分钟, 秒 "days& U, C+ q' N3 ~. a0 [8 X9 C
hours:minutes:seconds"
; Q) @  p. X* P/ j& d$ b+ Z' J. m! F expr中允许任何标点做分隔符,如果所有是date值时结果是一个
/ T9 t/ Y  |/ P) L  Fdate值,否则结果是一个datetime值)  
$ Z# s, w# f6 i 如果type关键词不完整,则mysql从右端取值,day_second因为缺
4 f0 n1 e) o, P* ^& M' Y2 d$ V少小时分钟等于minute_second)  3 N3 x% v' T2 o9 f
 如果增加month、year_month或year,天数大于结果月份的最大天1 P, \9 r2 m5 s# Z4 e
数则使用最大天数)    + b' J: Z! y- Y8 f$ n+ `
mysql> select "1997-12-31 23:59:59" + interval 1 second;  
  |9 P2 _1 n" }8 w) j' o( t. M% M 2 |. S' x2 @: S0 k8 Z5 K
  -> 1998-01-01 00:00:00   
4 a* a% v' k: d7 U0 @# _mysql> select interval 1 day + "1997-12-31";    6 i4 D. Z4 P5 h6 l0 A( k
  -> 1998-01-01   
- g, T$ Z5 _# D5 R. z8 Qmysql> select "1998-01-01" - interval 1 second;   
, D5 H* U7 q4 D  -> 1997-12-31 23:59:59   
% ]8 {3 H  B2 J' k/ E4 R' q( z( wmysql> select date_add("1997-12-31 23:59:59",interval 1
3 s8 R9 v+ B) ?5 E7 ^second);   
. s) R. P( ^# u$ v  R7 h  -> 1998-01-01 00:00:00   
$ A6 f: }( r; i, Imysql> select date_add("1997-12-31 23:59:59",interval 19 i. B5 x0 q6 u% i6 W/ E7 F/ u* V
day);    & _; D5 J7 O$ X4 i; r
  -> 1998-01-01 23:59:59   
$ u/ c: E7 N$ ~. L- H" _mysql> select date_add("1997-12-31 23:59:59",interval' \2 @$ d& O9 F
"1:1" minute_second);    0 t4 A" N7 Z6 o5 M) a' L
  -> 1998-01-01 00:01:00    7 Z: c5 T* N7 `0 R
mysql> select date_sub("1998-01-01 00:00:00",interval "1
) i: w( N  b( F) ?1:1:1" day_second);    4 {! M4 c! m/ Z1 j
  -> 1997-12-30 22:58:59    2 J) i2 t6 d* h5 }& \
mysql> select date_add("1998-01-01 00:00:00", interval "-1
9 P9 d. n9 G1 k- w; c10" day_hour);  8 r1 c" ^4 ]$ w/ U, n8 K
  -> 1997-12-30 14:00:00    " K8 y4 i  D1 i; e% O1 g
mysql> select date_sub("1998-01-02", interval 31 day);   
  k6 J, u' s4 a7 F  -> 1997-12-02    & H; B) f( ~# g
mysql> select extract(year from "1999-07-02");    % D% d* T/ \) L1 w  _1 V
  -> 1999    5 C% R8 f2 C& \" n5 |  o9 `
mysql> select extract(year_month from "1999-07-02) f) W( t' T  `( Q- Z  t0 s6 W
01:02:03");   
4 B! k& f" x' x# F+ `  -> 199907    3 f$ x# s! B2 L
mysql> select extract(day_minute from "1999-07-02
% Q! r# F: K! X8 I! R7 h01:02:03");    # @4 d! F, c* ~) |
  -> 20102   
+ @' h$ p1 R+ Q  |# a - ]$ t; I- g1 B% F3 D
to_days(date)    5 U8 y" H2 c' X  ?
返回日期date是西元0年至今多少天(不计算1582年以前)  
; O6 @: ]( I" Z0 |- i  f0 [# imysql> select to_days(950501);   
+ s1 e3 U: n2 d9 Y: n+ c  -> 728779    1 j2 t6 b+ Z. o8 u/ n  S8 i
mysql> select to_days('1997-10-07');   
; _4 g( y! z" Q4 a% g  -> 729669   
9 B: U! B2 [" B9 T; A6 o. h+ M
% K3 q( _; i2 V8 [2 F" ]from_days(n)    ) c/ P( W! Y  l9 S4 ~9 s
 给出西元0年至今多少天返回date值(不计算1582年以前)   " p1 v  o( {) A! Y; p7 R: d
mysql> select from_days(729669);   
: S8 `8 P/ B( L; @4 s7 P6 u. L  -> '1997-10-07'   
& D5 L7 a1 ]0 n2 p( X
' u+ q( E. O$ d7 }! A: l, o9 Tdate_format(date,format)    ' W) H: R2 N& E5 C5 I5 R1 z4 X3 t9 y
 根据format字符串格式化date值  7 U+ R7 b# P; m9 l& R; O
 (在format字符串中可用标志符:  
, n) Q: `. e  H( b, \1 _: c %m 月名字(january……december)    # b0 j% C6 q+ u8 O1 B
 %w 星期名字(sunday……saturday)   
6 N, ^& @! n/ I# B %d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)    ( m5 C$ \# `$ X
 %y 年, 数字, 4 位    5 L4 Y! t( L) b" ^. P
 %y 年, 数字, 2 位    0 L+ z! S3 H+ ~! z3 }2 F& z
 %a 缩写的星期名字(sun……sat)    * {) G: L; P3 A- V% `3 n% T' B
 %d 月份中的天数, 数字(00……31)    ' Y0 w! ?" \, d4 }9 g
 %e 月份中的天数, 数字(0……31)    1 \2 x. G  B9 T
 %m 月, 数字(01……12)    " P6 v9 G# w" J
 %c 月, 数字(1……12)    / |5 F, B  \# g$ C7 i
 %b 缩写的月份名字(jan……dec)   
' z  E' U+ q1 L %j 一年中的天数(001……366)   
! f# W7 P; E, W$ Z& e$ w3 P %h 小时(00……23)    1 o3 T3 w% T0 R; k" c; T. Q5 {
 %k 小时(0……23)    $ E7 g" f, p, I) R
 %h 小时(01……12)   
; F. U3 f2 ~6 C %i 小时(01……12)   
! l3 Q9 O$ V$ k: T( s# v3 X; ? %l 小时(1……12)    , j+ i. d8 ]7 r. {8 B( x
 %i 分钟, 数字(00……59)    ( c' i: V: l8 d+ O8 ~
 %r 时间,12 小时(hh:mm:ss [ap]m)   
* _" i) t- u5 s/ o& c# g" r" x %t 时间,24 小时(hh:mm:ss)   
% P! J/ ^* r( d5 t' Q3 f %s 秒(00……59)   
2 }! C, T! \& ]0 X! H- L$ V %s 秒(00……59)    - Y$ u. f8 @. @$ z: s6 N
 %p am或pm    ; t% L! m) T  v5 ?( Z
 %w 一个星期中的天数(0=sunday ……6=saturday )   
: x3 U$ d: m& F% C %u 星期(0……52), 这里星期天是星期的第一天   
. \9 h3 r/ H! b4 B$ E1 d% C) j+ s- M7 P %u 星期(0……52), 这里星期一是星期的第一天   
$ B( x" L9 k. I7 m %% 字符% )  
0 O  ]- a6 R8 P* K9 a5 ]- Tmysql> select date_format('1997-10-04 22:23:00','%w %m %' Z7 S  t, h. [/ O: U$ u2 E  \# ^
y');   
& e5 V2 G' V7 k  -> 'saturday october 1997'    $ Z6 K3 [6 h4 h
mysql> select date_format('1997-10-04 22:23:00','%h:%i:%
. f5 n4 x( K1 z" D3 S" e4 I/ js');    2 D" n% w6 v( S3 |
  -> '22:23:00'    $ l  N" k: q5 Z: p
mysql> select date_format('1997-10-04 22:23:00','%d %y %a
! Y" S8 ^8 e9 |4 b5 d1 h8 M%d %m %b %j');    * K1 c" Y/ }; I7 b' v
  -> '4th 97 sat 04 10 oct 277'   
* ]. i7 p0 `* E, J4 O( ~" }mysql> select date_format('1997-10-04 22:23:00','%h %k %i- K! z) g1 D* v  s  K) `
%r %t %s %w');    * G1 {1 `/ V! X. L
  -> '22 22 10 10:23:00 pm 22:23:00 00 6'   
8 k+ e0 a3 Q8 v, p/ F6 \7 N& X. D
% z& ~. {  m( G- ~; {: Jtime_format(time,format)  1 u) O1 K6 N9 m$ `
 和date_format()类似,但time_format只处理小时、分钟和秒(其
/ y. ?6 E3 i# s  T余符号产生一个null值或0)  / I$ g: _( c# }* Z! h# U2 \7 _
1 n6 B9 c. r- u/ p8 x
curdate()     
% P8 \) e% `2 U' _7 V9 Hcurrent_date()  
' q: J4 Q* n$ w( ^; |9 p' @ 以'yyyy-mm-dd'或yyyymmdd格式返回当前日期值(根据返回值所
! _" N( t" a4 j处上下文是字符串或数字)    1 q  P1 G) [. L7 `
mysql> select curdate();   
7 T2 ^$ u: G9 c9 C, Z  -> '1997-12-15'    * }( T& J' a2 l6 K6 l
mysql> select curdate() + 0;   
, A, F, d' K. f  -> 19971215    1 K) r( B5 d( e  E8 y2 H2 `
' V0 L- u4 B4 o9 ^5 y
curtime()   
' S: h; D% b6 `$ P; e$ jcurrent_time()  
5 {9 }$ m9 H, b1 t+ u! Z* M 以'hh:mm:ss'或hhmmss格式返回当前时间值(根据返回值所处上
4 J. |" S( ~/ m9 w下文是字符串或数字)      
5 ^2 |3 i" {8 i5 tmysql> select curtime();   
7 ~2 D5 R1 ?) [/ A0 i  -> '23:50:26'    3 A: \! g/ H3 ^+ ]6 S# w9 p
mysql> select curtime() + 0;    % L9 {& |. j$ W' M9 ]  X5 N
  -> 235026    4 R5 D" f1 D* }! J% R

' f! q1 I! `! C: T) f; @5 Ynow()    $ i# W+ w5 H( f0 U4 q
sysdate()    0 E- |* f" X; j0 @7 o
current_timestamp()  
% J. q! M. q  c2 P 以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回当前日期
9 {+ s# I; @! r0 g时间(根据返回值所处上下文是字符串或数字)     : }  o0 s  R- x1 h9 x
mysql> select now();   
0 L3 r6 H8 h3 e5 o. k! ?6 s, L  -> '1997-12-15 23:50:26'    + Y7 n+ Z( m% r& Z7 @' e6 Z
mysql> select now() + 0;   
" q8 v2 G+ [7 w  ~% Y& D  -> 19971215235026    # r+ B! U0 R  Q6 D! P" |8 ]

# r3 |' `: x8 U4 P# k1 Hunix_timestamp()    6 n6 V) w3 k5 M$ p$ a, I
unix_timestamp(date)    4 K! g$ R) l/ ~0 I  U
返回一个unix时间戳(从'1970-01-01 00:00:00'gmt开始的秒8 Y3 I  U  i2 s
数,date默认值为当前时间)  
& O* b. n% \0 w6 a, F& L7 b; E- _mysql> select unix_timestamp();   
/ F+ D2 e2 m: v. ?  -> 882226357   
8 S# K7 @+ N, }: [mysql> select unix_timestamp('1997-10-04 22:23:00');    4 X& p' _/ _2 o4 G% f
  -> 875996580    $ ~8 U  w1 H% J3 M, x1 C. J
! K5 q4 s2 E6 o
from_unixtime(unix_timestamp)   
: f+ U# h9 g& g* Y+ b, P" d, r以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回时间戳的( e1 a+ l* ~; A) ]; |6 `
值(根据返回值所处上下文是字符串或数字)     . L) ~; l, e  [* W% h4 H
mysql> select from_unixtime(875996580);    0 ^* r: J4 Z" a
  -> '1997-10-04 22:23:00'   
4 b2 C1 v1 R+ b% P& j. a7 w  gmysql> select from_unixtime(875996580) + 0;    & I0 ^/ z6 [! }' g9 ]
  -> 19971004222300    ; u2 q! b$ r3 |4 b- s

, s- J0 Q* r: r4 f0 S* q' Vfrom_unixtime(unix_timestamp,format)    7 ^1 s: F1 c& ?: _8 x. t" E7 H0 q6 k
以format字符串格式返回时间戳的值  
* J3 T. p* H* {9 d$ smysql> select from_unixtime(unix_timestamp(),'%y %d %m %
2 ?# h# s  {" b0 K: j% t9 _h:%i:%s %x');    4 a! e* n' h3 y% U% k- D. E
  -> '1997 23rd december 03:43:30 x'      A+ c" t" V0 ~" ?. m* w

6 q2 Y' Z- K+ x' T* X! [sec_to_time(seconds)   
7 f9 g% y: }8 g% C* n以'hh:mm:ss'或hhmmss格式返回秒数转成的time值(根据返回值所处上下文是字符串或数字)     7 f$ G$ k, K! m7 F  d
mysql> select sec_to_time(2378);    ( N7 h4 X) u8 u$ E
  -> '00:39:38'    4 P4 \4 J5 O( ?  i% S7 H
mysql> select sec_to_time(2378) + 0;   
: n6 Z% g$ \3 H) s+ S  -> 3938    0 H" y( S: t6 J" _, |
0 O8 M. d1 R, G4 R! H5 Q! d
time_to_sec(time)    9 R, i) i4 c* g8 `4 n
返回time值有多少秒    9 C$ e: A( k% a3 h% S8 @
mysql> select time_to_sec('22:23:00');    2 O7 x9 Q/ ]6 _: Y
  -> 80580    - _, K8 s5 |; q7 X, R
mysql> select time_to_sec('00:39:38');   
3 k: a" M1 w, \0 m! g/ U  -> 2378 2 h& A4 g4 k0 X' u7 A) u. V

  i  }* P- e- P( m& F$ f9 w2 e转换函数
5 p7 u! _) j$ ]5 a$ C; n( ^9 ?cast5 D0 X" D4 K" O) U, g* s
用法:cast(字段 as 数据类型) [当然是否可以成功转换,还要看数据类型强制转化时注意的问题]+ ^4 `" G$ I3 b7 n' h
实例:select cast(a as unsigned) as b from cardserver where order by b desc;% }: h) _2 y% R8 m
convert:
6 P( d8 ]* O  r4 C+ N用法:convert(字段,数据类型)
' n' F/ ~  V+ z实例:select convert(a ,unsigned) as b from cardserver where order by b desc;

' d; W0 C. I5 G4 i+ v
回复

使用道具 举报

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

本版积分规则

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