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

mysql常用函数大全

[复制链接]
跳转到指定楼层
楼主
发表于 2018-3-21 16:07:02 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
1、字符串函数
$ s; C5 h: n4 wascii(str)   0 m' u4 D. [" `0 i  `* G  f
返回字符串str的第一个字符的ascii值(str是空串时返回0)  
% h& s% F) ]1 B# U4 s* v! vmysql> select ascii('2');  
& Q- W$ B  ~6 T5 U0 E! {  -> 50  1 a, r: x) A" d) D6 q. h" T/ a4 e
mysql> select ascii(2);  
" x; ]8 e" v* V6 L" u+ u  -> 50  + B$ g4 R" R+ Y
mysql> select ascii('dete');  
9 f* Y# H# f) ^4 q  -> 100

# ]. z* i- d1 H/ m4 i% rord(str)   
9 u! V5 j# O; e$ x如果字符串str句首是单字节返回与ascii()函数返回的相同值。
% W' I* _' \- _& T+ b
6 {5 N# o7 y: F/ q; v/ I) j如果是一个多字节字符,以格式返回((first byte ascii code)*256+(second byte ascii code))[*256+third byte asciicode...]  # w/ E# @- n1 Z2 |8 Q2 {' a9 E
mysql> select ord('2');  
1 m, \: y: L' ]3 y: G$ {  -> 50  * E# H: i- I# q; F
   
* f& X5 h8 ]3 m4 ?& C& [conv(n,from_base,to_base)   * S8 A, d! R. V6 O; A# |2 b
对数字n进制转换,并转换为字串返回(任何参数为null时返回null,进制范围为2-36进制,当to_base是负数时n作为有符号数否则作无符号数,conv以64位点精度工作)  3 e4 x9 B, m4 M+ p6 T
mysql> select conv("a",16,2);  
8 \3 |, Y" T5 W7 @. ?1 e: ~( M  -> '1010'
6 D) m6 x( f" O) xmysql> select conv("6e",18,8);  3 Y, ^; L- G0 U
  -> '172'
* S1 S4 F( L; n( [& K8 U3 H" a8 k: Umysql> select conv(-17,10,-18);  
# ?9 @4 d2 a. p8 y- P) u  -> '-h'
4 m4 O- s& P, j" }2 c3 G  }5 ~. [mysql> select conv(10+"10"+'10'+0xa,10,10);  # M0 ]9 l' ]+ a0 W' v- N* y+ q; q
  -> '40'
1 }4 C5 o$ b; O) F. d' t   
- |9 H  x" Q0 [5 m- j& C% zbin(n)   
2 d; O* m! R5 Y# a4 f4 C' t+ E把n转为二进制值并以字串返回(n是bigint数字,等价于conv(n,10,2))  # H+ x- W/ i" R2 q* I7 W
mysql> select bin(12);  
& `, o8 ?" a6 V' f4 B  -> '1100'
2 J, X* {$ f6 [2 T& c/ l7 s
6 ]  _: T+ I! b8 Y9 u+ coct(n)   
( ~/ i( |6 K1 [) ?' P7 `- x把n转为八进制值并以字串返回(n是bigint数字,等价于conv(n,10,8))  * d) x$ L9 I7 Q; v9 V( ]& Q; q5 E
mysql> select oct(12);  7 o3 Z7 F0 D9 j9 C. A  [
  -> '14' 0 F0 t+ R4 E( ^* V* d# R: _
   
9 @, X/ @' A* w6 u+ [/ |% G$ Qhex(n)   
" G9 {% A0 `8 \" s把n转为十六进制并以字串返回(n是bigint数字,等价于conv(n,10,16))  
# l, |, D; r, o0 _' H" Nmysql> select hex(255);  , }1 h1 L0 u4 e) f; c
  -> 'ff'
1 C1 f) h: u  L6 ?: J* |   7 ?4 S$ s- v7 G
char(n,...)   2 y% @+ K1 D1 T/ |) P
返回由参数n,...对应的ascii代码字符组成的一个字串(参数是n,...是数字序列,null值被跳过)   
2 |! s% C* }* {mysql> select char(77,121,83,81,'76');  " u5 X0 Y0 c$ S8 A' z4 a3 T
  -> 'mysql'
# S) s+ t- d) ?& l% bmysql> select char(77,77.3,'77.3');  
/ W# j" Q! t. @) \4 |5 `  @8 G6 ~) g  -> 'mmm' 2 L" X0 p& [' x
   4 ~# S: o; C  n. S
concat(str1,str2,...)  
7 \) ~2 A9 z. q4 K% g# S7 k2 F把参数连成一个长字符串并返回(任何参数是null时返回null)  
- M4 ?8 V' d; ^  Y+ amysql> select concat('my', 's', 'ql');  . j. h- K& K4 B# _
  -> 'mysql' 5 ?" p0 ]  m& i' P3 {. Z
mysql> select concat('my', null, 'ql');  
+ S0 |+ x6 D: i  -> null 4 o! f# b, J5 M$ h* g: P. M8 {6 I
mysql> select concat(14.3);  
0 B+ E; x# ~5 R) p  -> '14.3'
, X3 J$ u$ u/ y7 K2 b 4 q4 b# m( W! |
length(str)   
0 j3 s* M1 P1 i( J. Q0 {5 toctet_length(str)  2 K2 ^* |; ?% O# Y4 y$ u$ T0 k
char_length(str)  9 D$ t1 k$ c  P+ ^1 G+ ^9 \/ I
character_length(str)  
# e( c4 p4 ?' L- o* j返回字符串str的长度(对于多字节字符char_length仅计算一次). q% L' [6 k# n% E
mysql> select length('text');  
8 T3 Z5 A1 R. n/ W5 E! Y; X  -> 4  
' f5 {5 Z: M$ s" Qmysql> select octet_length('text');  # m+ f' e% x- v- B1 A! @+ g+ n
  -> 4  
4 K1 ]! E/ _. W# l$ ~9 v9 O
5 O6 d$ U" u0 n2 U) `locate(substr,str)   + a+ ]3 j( U# z" b8 J  Q
position(substr in str)   
2 `- x: d; R  |2 P返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  
# |( a+ u. Z9 P7 S' x6 Bmysql> select locate('bar', 'foobarbar');  
6 a7 t. S8 V. x) D  O+ R$ J6 i  -> 4  7 ?1 p) G! u7 f8 d; k+ H6 T9 e
mysql> select locate('xbar', 'foobar');  
% D% H  j* E& f7 c( `0 w  -> 0  4 ^5 G* g' v% Q/ ]$ v0 P
    0 |( ^# {; K0 q% J3 ~' B  z
locate(substr,str,pos) , w! N6 a/ c5 k
返回字符串substr在字符串str的第pos个位置起第一次出现的位置(str不包含substr时返回0)  
/ z. e  t; R5 u4 ymysql> select locate('bar', 'foobarbar',5);  
  z* ^- i* u* t- K* s0 M  -> 7  & }0 N- _: R+ q* M& d

5 ^1 y8 }6 x2 G2 N2 _/ K7 Tinstr(str,substr)   & \' N$ F% [, |; V0 M* j1 z% V6 v
返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  
+ }3 z- R' g# @5 ~9 W1 jmysql> select instr('foobarbar', 'bar');  
& s. J7 G' Z, d2 u# g# k  -> 4  * f9 A5 |8 F6 C* G7 ~
mysql> select instr('xbar', 'foobar');  
* L- Q" o+ S$ o5 E) v7 y/ o- |  -> 0   9 e& U  H8 n' y! y7 q: C' Q

2 S9 B# M  F4 W  P  W7 }! Slpad(str,len,padstr)   
  X1 F1 z0 \+ |) v% Y( N$ b用字符串padstr填补str左端直到字串长度为len并返回  # U" a+ u. Z7 h
mysql> select lpad('hi',4,'??');  
# V* u/ Z0 ^6 f7 I& p0 c  ~9 U  -> '??hi' - B( @/ H/ b7 _& F2 t. G
   . s% X, b  x5 [, d
rpad(str,len,padstr)   
& X! L" p! h' [$ L; D0 \用字符串padstr填补str右端直到字串长度为len并返回  
& k& e: r9 x: W! b8 M4 ?mysql> select rpad('hi',5,'?');  
, b3 o  p  Q2 x! d4 G* {& G! M: y0 ~0 P  -> 'hi???' 7 p) U8 n# A/ Y3 [! M& P, y
3 w# ?! S$ A- ?# j
left(str,len)   3 G1 k0 u5 L' y0 ^( m( r
返回字符串str的左端len个字符  
1 v5 O0 t# g. o. i4 [mysql> select left('foobarbar', 5);  
  Y! i9 r8 }1 B$ K) n9 F  -> 'fooba' 7 A+ K' G4 G* |% x- v% }- E

' w0 e2 c) V; Y0 A; q1 E* Dright(str,len)   $ _) m4 s. U% Y  ?
返回字符串str的右端len个字符   . V( n- ?- C* X  S0 t0 M- k9 N
mysql> select right('foobarbar', 4);  
( M& I5 f  [+ x1 V, M  -> 'rbar' ; D5 @! ~, i8 I' k7 I

% c  m2 S7 s! U1 `, Zsubstring(str,pos,len)   * F2 K9 l# x! [" R# t. k$ g7 J
substring(str from pos for len)   
0 A  ~9 u  H4 j( G' T( xmid(str,pos,len)   0 t% I& w. t. n& z) ]% O1 s
返回字符串str的位置pos起len个字符mysql> select substring('quadratically',5,6);  
' i) m# F' o# f1 l0 H/ x3 F+ d& ~  -> 'ratica'
! z& l- W- c7 m+ v( O0 N1 q
5 `) Y) [1 p  T- ^( \- V! asubstring(str,pos)   / H% E9 d- u8 _0 _
substring(str from pos)   ; N2 Q$ z, [, h, l
返回字符串str的位置pos起的一个子串  
7 {" E2 _7 P  Omysql> select substring('quadratically',5);  
" O" u2 D( v% R: f  -> 'ratically' 8 H8 {( d' C, j- u* `9 m+ G
mysql> select substring('foobarbar' from 4);  
( ^2 f% ^7 |7 f& o( ]& f, o  -> 'barbar'
5 W6 p" M! i3 u  ` & y; R1 k, @  U
substring_index(str,delim,count)   
2 x7 ?3 n  U/ ?返回从字符串str的第count个出现的分隔符delim之后的子串
1 Z, J; E: a) E0 V+ O(count为正数时返回左端,否则返回右端子串)  
$ H, B& m8 e0 ~1 \* X2 cmysql> select substring_index('www.mysql.com', '.', 2);  
$ A" d3 ^( t* ]! q9 z- P8 E/ I  -> 'www.mysql'
5 I" n8 V8 \- k2 Z/ K) Q' L1 _mysql> select substring_index('www.mysql.com', '.', -2);  
' A* I% Z/ f1 ~( ]4 A" S. C( I  -> 'mysql.com' 2 B0 D& L7 b: n' v+ b; c3 ?! x
- e: `1 y" f" K
ltrim(str)   
& Y. o7 T5 d% q% t返回删除了左空格的字符串str  4 k/ ]+ X/ L2 M$ d  N
mysql> select ltrim('  barbar');  
7 N. j$ @, [8 j& f" l7 a, s  -> 'barbar'
0 N1 V: J7 x7 R/ ^
+ ^- a' V4 X2 L7 x( Qrtrim(str)   ! ^' C# y6 D6 [3 Z9 }% U0 K
返回删除了右空格的字符串str  
* E. v. N1 {# f- e5 ^mysql> select rtrim('barbar   ');  " j, _+ ]' e; J- P; e
  -> 'barbar' , a/ S$ ~* H/ m) w( \) h
( S8 m- h0 m7 k( Z) W
trim([[both | leading | trailing] [remstr] from] str)   6 ]* x! Y, ]( C) F" k% }& C
返回前缀或后缀remstr被删除了的字符串str(位置参数默认both,remstr默认值为空格)  " O; O; V: j' @# M- L3 e
mysql> select trim('  bar   ');  
2 ^8 V: z. T: E6 P) K  -> 'bar'
2 E% ]% A  F2 f" h" amysql> select trim(leading 'x' from 'xxxbarxxx');  7 f* D3 _9 C% x: U' ?
  -> 'barxxx' 1 q; U3 S( a) I9 x; [
mysql> select trim(both 'x' from 'xxxbarxxx');  
: J7 L- y% \# S" C( c4 [  -> 'bar' / E0 Q# I$ }* C+ |
mysql> select trim(trailing 'xyz' from 'barxxyz');  
2 U2 J* S% v* h3 M  -> 'barx'
, |- y3 p6 ?5 v* Q- Z& d9 o+ a 8 X1 z9 L/ n* Z  l8 K" x4 M# k
soundex(str)   
' p/ w/ w7 k; \3 v返回str的一个同音字符串(听起来“大致相同”字符串有相同的
) D! J- _) h' f# @3 p; A同音字符串,非数字字母字符被忽略,在a-z外的字母被当作元音)  
! E  U/ `* u8 s/ p& |mysql> select soundex('hello');  
7 o9 E' K( K: J9 b- i: B  -> 'h400'
3 M9 t1 R3 \0 Tmysql> select soundex('quadratically');  6 j9 C+ F# R) Y
  -> 'q36324' ' W  L7 ]# C5 N( o9 a$ w) v( v
   8 Q, J- |3 M# E# y) E, ]
space(n)   . e  x  F6 y5 E7 O
返回由n个空格字符组成的一个字符串  
# q' m2 _' x0 }: O8 e7 Kmysql> select space(6);  8 z+ N6 H$ D4 R
  -> '      '
6 F: X& M! a2 V! _6 I! {& m   4 x1 V3 N! @6 T% O" q6 S
replace(str,from_str,to_str)   
- q6 n2 R6 ?* @5 W7 T用字符串to_str替换字符串str中的子串from_str并返回  
; g3 p; g  \" ^8 B/ Kmysql> select replace('www.mysql.com', 'w', 'ww');  % K% L( F3 o9 s- f  i% m
  -> 'wwwwww.mysql.com' ) c) Y7 m+ q: E1 H" c9 G6 _: I  O1 D0 N

  Z" N' X$ O3 u: m$ y1 |9 [repeat(str,count)  
8 m7 _$ U' C9 B% G9 Z返回由count个字符串str连成的一个字符串(任何参数为null时
6 b7 {+ v1 B* q$ W返回null,count<=0时返回一个空字符串)  
2 m* I3 X8 H; B/ umysql> select repeat('mysql', 3);  " y% C( J+ W" D" G
  -> 'mysqlmysqlmysql'
  M, V0 y9 f" d5 b/ @$ F   7 ]) t- @* P( O8 n
reverse(str)   3 y1 c+ n* T3 D3 h) @
颠倒字符串str的字符顺序并返回  
6 K( X1 m- X5 a& _4 H/ B, D* wmysql> select reverse('abc');  % ?2 b5 _: D4 d
  -> 'cba' 6 V( [6 z2 c/ a3 F( q) H5 A
3 J- Z$ S& j: B& a: }6 b
insert(str,pos,len,newstr)   
0 G( h0 v" E2 x5 W. g( @把字符串str由位置pos起len个字符长的子串替换为字符串" F( ?. i- w! z
newstr并返回    I# v9 u4 D3 w4 i, m
mysql> select insert('quadratic', 3, 4, 'what');  
+ g0 ]3 \/ r1 R& }  -> 'quwhattic' 1 m1 X7 _+ a8 @

$ _( J5 w5 ~$ \  H) P) g3 d3 E* N" Delt(n,str1,str2,str3,...)   % L' r9 ]! T% v5 n% h
返回第n个字符串(n小于1或大于参数个数返回null)  ! a3 p- q! R  F) h; R) Z3 E+ p
mysql> select elt(1, 'ej', 'heja', 'hej', 'foo');  
, B$ ?' ^, u+ Q, N  -> 'ej'
% l( |( e9 w- D% b: r$ R5 Q4 Smysql> select elt(4, 'ej', 'heja', 'hej', 'foo');  
7 W( Q0 U( `% {4 A( ~# Q  -> 'foo'
0 v$ y( Y/ ^4 D' c0 T8 g % z& D3 q- K! F7 R8 |4 H6 \
field(str,str1,str2,str3,...)   * w# \  {' ^7 |( y
返回str等于其后的第n个字符串的序号(如果str没找到返回0)    ]+ M6 f* X/ g% G7 l
mysql> select field('ej', 'hej', 'ej', 'heja', 'hej',
/ L2 s( b! G( s% S. Y4 `$ |'foo');  ) [9 M7 @- U( y3 l  i# a' r. F
  -> 2  
. l% |0 c$ R* L/ G. g% T$ M- bmysql> select field('fo', 'hej', 'ej', 'heja', 'hej',0 A- p( q6 l* S! ]
'foo');  " I- f2 r9 |+ V1 _2 F
  -> 0  ; j3 U/ M( H4 B" j

( w* j. _0 [! z4 c- Z" A1 }find_in_set(str,strlist)   8 q* p* L7 y5 t+ d
返回str在字符串集strlist中的序号(任何参数是null则返回5 k0 S7 J, d8 V" n/ Z% |) L# j
null,如果str没找到返回0,参数1包含","时工作异常)  4 d+ s" p, M+ ]; M! ?! L7 W
mysql> select find_in_set('b','a,b,c,d');  
) m. z6 S& V0 J6 W& R) [: M4 ~. x  -> 2  ) q+ G( D, c3 O
   . Z: H0 l5 N* \; V: ?
make_set(bits,str1,str2,...)  4 J; V1 T  e) V6 X) C# K4 p
把参数1的数字转为二进制,假如某个位置的二进制位等于1,对应
% C$ @# J& X) Z0 ~: j4 \9 u位置的字串选入字串集并返回(null串不添加到结果中)  1 Q: F7 |: j7 f* O
mysql> select make_set(1,'a','b','c');  
( ]& B" v9 H0 _+ o- c/ c# k$ d8 Z  -> 'a'
  X9 t& ]- U6 Q9 S' |; dmysql> select make_set(1 | 4,'hello','nice','world');  % ]; y7 i, u3 S# \) C& b
  -> 'hello,world' # `: i6 I$ ]5 U% f8 X3 {+ C. U5 I
mysql> select make_set(0,'a','b','c');  
- i, Q8 f( P7 i$ a! O  -> ''
7 a& }! T" s* W+ | 8 A% N9 ]% j* k4 i
export_set(bits,on,off,[separator,[number_of_bits]])   ) V/ X' k$ g! O
按bits排列字符串集,只有当位等于1时插入字串on,否则插入  h  v% N2 l9 {! x
off(separator默认值",",number_of_bits参数使用时长度不足补0
# b# E6 x- N! t5 h: ^( |! s/ S: O0 t而过长截断)   5 A3 b) j6 O- N3 f# l( P
mysql> select export_set(5,'y','n',',',4)  
% J. l. o) z" v% M$ j# c& S8 i  -> y,n,y,n   ; S2 z/ s, S4 V0 R

0 V, X9 j2 _" {5 Jlcase(str)  
" t4 n6 X* e/ A/ \8 Z& P. p4 M# llower(str)   ( Y9 V" W9 v7 [8 v
返回小写的字符串str  
, L# A4 `( ~* b, L0 ~; o6 pmysql> select lcase('quadratically');  
( J! D/ t6 E+ h  -> 'quadratically' 0 l6 v! E, C: q2 S. V
   / v$ B. Y0 h4 o! [3 ^
ucase(str)   ! W0 z$ p) g) R/ H8 N
upper(str)  
- z' q% U* @# V( S. w7 c返回大写的字符串str  
* V1 w/ ^- G! X7 g) Lmysql> select ucase('quadratically');  8 A; @6 @  C, c5 X0 i% q
  -> 'quadratically'
  H2 \  X. D3 `$ D; Y7 Q - m# \# o! G' x0 ^; x
load_file(file_name)   4 ]/ V# t% e' E) i: ?
读入文件并且作为一个字符串返回文件内容(文件无法找到,路径
2 p$ l- Y* y' q# _不完整,没有权限,长度大于max_allowed_packet会返回null)  
5 K+ [; d, a+ L% y- N5 y# Cmysql> update table_name set blob_column=load_file
) Q' Y* z2 P7 d8 y) E) k("/tmp/picture") where id=1;    g. H+ O  M1 G+ t  S

+ a. L, l* n. a" [) O2、数学函数
6 P2 t( T2 |, X9 tabs(n) , \# O5 {" z2 O8 P9 L
返回n的绝对值  
3 V6 t) j9 @) L+ Z; v) wmysql> select abs(2);   
: H5 x% @. \9 [& H$ F+ w) m* f  -> 2    : V( m  E& Q; Z9 u4 W, o
mysql> select abs(-32);   
' o8 L& E' Y/ r% f% ^) E' H( ^  -> 32    % t) E) N( t$ K, Y# o" K- ^
   9 A) V% b# V- M( C$ s
sign(n)  ) o& ]3 y$ I& n- H
返回参数的符号(为-1、0或1)    @  q. Y; s2 ^) N
mysql> select sign(-32);   
1 T0 S5 E: B- O6 W+ C  -> -1   
$ A1 Y0 z2 M6 Kmysql> select sign(0);    , @. ~  }5 w- `  X# Q- }
  -> 0   
! E% Q* j0 R8 lmysql> select sign(234);   
' e. i9 j' O$ C; X3 X( q  -> 1    - Q3 E0 _0 }: `

1 H7 M# z4 j8 C2 b. g: e( {1 |& Fmod(n,m)   
9 g1 K0 s+ I2 }$ s& V取模运算,返回n被m除的余数(同%操作符)    & b. b1 c* `" E  o
mysql> select mod(234, 10);    9 {. [( W) Q7 O, k
  -> 4   
- ^, U4 w9 ?9 V7 {0 \( H* Pmysql> select 234 % 10;   
& X7 S- k- H5 l) F! V( z/ T  -> 4    1 r- o2 P9 ]: \% p7 |. y  q
mysql> select mod(29,9);    ; f6 C' [) c/ Z: R5 `! O" A
  -> 2    0 m: S5 ^3 k* U* v# C/ `1 C) ^0 O

; `2 ^, _2 ?8 j% b8 `# t( Qfloor(n)  
. W: M6 C- R7 q9 ?返回不大于n的最大整数值  5 G9 \1 L' d' j
mysql> select floor(1.23);   
0 L) O# I  g+ C$ M& c3 E1 G  -> 1   
2 ~: V- E( p  V5 k1 N( Nmysql> select floor(-1.23);    " h- T! ]7 w, ]( G' [7 B$ _+ L
  -> -2    4 h! D$ _& @0 d: i; Q
$ w% ?2 J5 `( c4 D( {6 |; j
ceiling(n)  6 o, }8 m$ ?9 o* @! `$ w
返回不小于n的最小整数值  
% A1 a1 C) D. }/ z& Zmysql> select ceiling(1.23);    ( o2 V7 S3 j* A" E/ P
  -> 2    . R3 E5 l6 k7 D/ ^- D- M
mysql> select ceiling(-1.23);    ' P2 d, j& `. ]$ L' {( E
  -> -1    % K/ _$ \1 @, p/ s# s

2 l( o9 u. y( H% ^* D; _0 c5 @( q( around(n,d)  
) R9 [% \5 q9 C) Q  \返回n的四舍五入值,保留d位小数(d的默认值为0)  . y! h4 K9 z. s2 H7 a5 W
mysql> select round(-1.23);   
; V: L1 b  ^  n: g; m( a/ V! @( Y  -> -1      r8 Q6 `$ b" M! |( w- B
mysql> select round(-1.58);    / _- G. L9 z, F; ^3 T- j8 o
  -> -2    ( G. p7 A" }& t8 S7 `$ W4 k3 c; x
mysql> select round(1.58);    ! ]# ?6 H& f5 E( n+ P# l, R& N) E
  -> 2   
; y% j4 t& Z# Zmysql> select round(1.298, 1);    " ~; o/ s% x- a) B
  -> 1.3    # e: z' t7 s9 X
mysql> select round(1.298, 0);   
) q6 |& z/ ?, ~3 A4 n% _  -> 1   
: B8 j1 v: R. _; y4 E# M" \ : o) k9 ~% |- @$ a6 k3 S0 w* L
exp(n)  * c/ Z! t- ^; B5 n
返回值e的n次方(自然对数的底)  : Q+ Z7 K+ g4 s4 ^
mysql> select exp(2);   
& h* U9 a6 R( K& w0 m' i  -> 7.389056   
- v: |! m+ T% Jmysql> select exp(-2);    9 l# l- [# B! _: W
  -> 0.135335    9 L2 u, a3 Z; }
* y: S) \2 w9 X! q
log(n)  4 {) h4 A+ W$ M$ w$ L& d# J( S
返回n的自然对数  ' _) O2 Q  k( }$ B& ]
mysql> select log(2);   
# k# G  ]' c8 y  -> 0.693147   
6 O3 }, c) S* S8 umysql> select log(-2);   
0 w3 N) {. E- V- `5 Y% u, T  -> null   
4 v8 Z& v7 V( I 1 f3 W6 ^! {# q1 k5 b* x, j  R
log10(n)  
6 l6 N' }4 p" _2 X1 @返回n以10为底的对数  ' K" J. `; a9 T9 f( v
mysql> select log10(2);   
4 {5 r' e( b: r5 Y, W, Y  -> 0.301030    1 ]: V; F% F' W3 M! [+ q
mysql> select log10(100);      Q: t' Z4 Z. X5 G, p
  -> 2.000000    ) N7 }* [* |3 B. Q, v
mysql> select log10(-100);   
  L* B0 ]8 }6 Z1 |  -> null    . Z" M& I9 ?# \+ I+ P- a8 ]
, f) l& }) A, y
pow(x,y)    % C6 ~) a3 t7 G6 |3 a. c5 y  V
power(x,y)    0 v' ~$ s; E  T0 G
 返回值x的y次幂  
; w/ s! J' \# A# H7 Q7 nmysql> select pow(2,2);    . X7 {' t& D/ H6 y4 g# E5 d
  -> 4.000000   
/ g+ ?' _- Z: ]: V( ]6 @mysql> select pow(2,-2);    ' Y' ^( |3 k* W, I, C) ]: c& l
  -> 0.250000  # S9 v  T. p* d5 Q/ x' I
$ d! K( O1 O1 u2 L" Z+ ?9 l- w( H
sqrt(n)  
5 x* Z+ @; S* G, q! i  x 返回非负数n的平方根  
! K' K5 g; i, J5 y$ p' [mysql> select sqrt(4);    ! X. ^2 K* ]0 B; n6 L5 C
  -> 2.000000    # S; N" Z3 I( a( ?  `; z2 r
mysql> select sqrt(20);    9 j0 ?4 ?! C- e
  -> 4.472136   
" A! ]8 u1 ]/ c% K  N7 l 9 a1 U/ Z3 Y+ @
pi()    , d! x9 M- a' ^0 x: U! x9 m% p& R$ o
 返回圆周率   ! F% _7 W& L- h1 d8 ?& f
mysql> select pi();   
: g& ~" @7 o, z  -> 3.141593    ! Z& ?" y: R, {
5 j6 G5 e- I1 \/ u) B: d7 w
cos(n)  
5 W# }6 Q3 {! u 返回n的余弦值  
# B; q/ M8 r5 A. }  mmysql> select cos(pi());  3 W( x4 ]' Q* O" l3 `- T( J
  -> -1.000000    7 u7 z7 a5 A! ^' f: Y( ?" j( s

& a* K( [. Y1 u3 D9 Ysin(n)  # P# q0 h9 R+ m' Z4 D
 返回n的正弦值   
+ G7 S) f1 Y7 g; Gmysql> select sin(pi());   
/ ^* i6 X% D2 _1 P6 A  -> 0.000000    " Z/ H+ N, x% u" v6 v+ M! I% R
* F3 z8 Z! L/ U$ J, s
tan(n)  $ j  B" V6 I8 ?; I+ P3 E7 t
返回n的正切值  
% @% M5 q' B  R* x& C+ {' X2 Dmysql> select tan(pi()+1);    & x0 G+ o5 M3 ~' s
  -> 1.557408    + ]1 R9 A3 G. o8 @
" F8 s" C0 D3 o3 k; S9 O. o0 j
acos(n)  ) {5 T, y! @8 n: }  J) }( A
 返回n反余弦(n是余弦值,在-1到1的范围,否则返回null)  
' @  n8 {0 c* Z% @mysql> select acos(1);   
9 U1 ^0 K$ b" {8 \) N  -> 0.000000    1 y) l0 R% w6 k
mysql> select acos(1.0001);   
! _5 [! \9 s" E, c  -> null   
3 @8 B4 ^0 Z: _6 H1 X/ ~, G- Imysql> select acos(0);    " N! j9 L, ~" e+ w) w) y
  -> 1.570796    * D: b3 H/ h$ e+ W

& `( w8 L' P8 o2 Jasin(n)  ; s7 q! H) s% w0 [" G0 d8 E. k
返回n反正弦值  . o8 C9 {# T; {4 E6 H& i
mysql> select asin(0.2);   
8 P) D6 R* u0 M3 b* V  -> 0.201358   
2 r+ J6 L. w9 Gmysql> select asin('foo');    1 {0 h, m: |% f/ d/ Y
  -> 0.000000   
4 H+ J% J/ Y# w3 n: L- M & j3 Y( j8 F1 q& h( I( q0 W
atan(n)  
9 b  a$ r; C  J返回n的反正切值  
5 [8 ~4 Q' w- E1 b, Amysql> select atan(2);   
& w: N5 F1 M. W8 X- a% Y4 E  -> 1.107149   
, Y, H+ q' k: k0 }# Q1 smysql> select atan(-2);    : z: Z& r9 c3 [
  -> -1.107149    + V% o, u. v: ~9 ]7 {
atan2(x,y)   
; ^& _# R& U5 d$ J& z: g0 J 返回2个变量x和y的反正切(类似y/x的反正切,符号决定象限)  
0 E3 W( `% _) s+ K5 E/ Umysql> select atan(-2,2);   
& W% D& i9 T3 y4 C  -> -0.785398    ) f" i0 u7 Q- H8 Q" K+ l3 A
mysql> select atan(pi(),0);   
# e5 n# x: d/ \- O  -> 1.570796   
6 N+ |$ F+ N& N5 i& H# r& {& v : X6 {. P; x" O2 g" M% B
cot(n)  
  a, {* n8 a% u, M9 D9 u返回x的余切  
) Q) @$ P1 |, r3 Smysql> select cot(12);    0 o, Q9 K% E+ i) Y5 L- X, C
  -> -1.57267341   
3 w* y: J! K& i9 Vmysql> select cot(0);   
7 Z+ E7 J, m' w: Z' C6 P  -> null    6 `- x8 R8 `! p& Y6 o% H, c9 U

: [. u5 ]0 d. x/ _3 grand()  
) N  O$ G% ]( V0 L) prand(n)    
# N5 C* n( @! h' ^3 }* V返回在范围0到1.0内的随机浮点值(可以使用数字n作为初始值) 1 k) }# _  g& u2 z7 e% B
9 p2 I9 y* l0 A% y
mysql> select rand();    - V- q8 c* i, j4 S7 k+ }- D
  -> 0.5925    2 I- Q( `! w# D/ T
mysql> select rand(20);   
) l* k/ `0 S6 C+ p$ Y% ^: o  -> 0.1811    2 Z, O7 {7 ^- \5 _0 Y2 H3 {
mysql> select rand(20);    9 M, ]" W2 ]) A  r- K" |, ^+ n
  -> 0.1811   
. w- B, l9 A! ]& wmysql> select rand();    * ^+ q: R; Z! B- }9 ]  W
  -> 0.2079    - A2 ?6 u- X: W) h, f. y3 H
mysql> select rand();   
) V) O, @$ r& V) u* ?  -> 0.7888   
( Q/ Z2 V) m, M6 L6 n3 g0 K& z / Q+ T) j  O8 |- b
degrees(n)  
( w1 T: I$ s! e; y  i把n从弧度变换为角度并返回  
$ ^: G' F# R, N$ A# rmysql> select degrees(pi());    3 C3 N1 _7 G# t  h5 Q- h0 y* y! e
  -> 180.000000    1 `# d! n0 e  e

+ s2 O2 W1 Q. j) B. O* T. Vradians(n) ( g( E1 G- X+ ~
把n从角度变换为弧度并返回   
; N6 y- h' |$ ]  V, umysql> select radians(90);    ) q% r6 v2 u- i) b! ?2 N5 Q6 m
  -> 1.570796   
6 ^: U  n6 ~! \7 V3 S& \' L* f* c6 N6 S/ r. Z& D
truncate(n,d)   
( s' {& P1 {7 {' s1 }. x保留数字n的d位小数并返回  
8 G& Z6 m+ P, s# _mysql> select truncate(1.223,1);    2 q8 b( E% g! L8 L$ E) n% C1 \3 _8 g7 |
  -> 1.2   
! {3 u. A1 ^* i  M- Ymysql> select truncate(1.999,1);    ; s7 D* m- j& f/ c
  -> 1.9   
- A1 W2 z5 ~* ?/ [, \8 Mmysql> select truncate(1.999,0);   
2 E' }/ d5 j* N# u0 S# b0 A/ _# D& j  -> 1    ) t& ^) x3 q& k( w. l* F% R5 g

- N! W- c3 {6 r! Tleast(x,y,...)    , a4 N' J+ Q- f3 p; t1 r! m+ m
返回最小值(如果返回值被用在整数(实数或大小敏感字串)上下文或所有参数都是整数(实数或大小敏感字串)则他们作为整数(实数或大小敏感字串)比较,否则按忽略大小写的字符串被比较)  ) O/ r2 m0 b5 h. W9 f6 _
mysql> select least(2,0);    8 E) a. j* p' @% q% Q$ w
  -> 0    $ |4 c- r" l; u3 ~( t
mysql> select least(34.0,3.0,5.0,767.0);   
5 o) Z! }2 K0 H  -> 3.0    * A1 L6 c5 B, S+ G1 {' n
mysql> select least("b","a","c");   
) _+ K) ]6 E: _( W( h7 M# M  -> "a"    . {* g# j7 b# ?* g0 r

* l7 N' {  K' L* z/ [8 Pgreatest(x,y,...)   
- B! f8 N: y- Y! ~2 J返回最大值(其余同least())  
; f3 H0 g: R2 |( C: {mysql> select greatest(2,0);   
% W" k: r5 R! K) o/ F1 n  -> 2    * S& O/ ]3 l. Z) L6 z
mysql> select greatest(34.0,3.0,5.0,767.0);    8 ?) u  s3 l/ b! T3 b, `! a5 W
  -> 767.0   
3 Y4 `  r. e. e' Fmysql> select greatest("b","a","c");    ( \/ O/ L: R7 {# B! }
  -> "c"       l7 g  p( n& p  x

; k/ U' Q$ Y8 W# T( y7 o3、时期时间函数
: Z6 s1 U$ K, L0 }, C' Udayofweek(date)    ( a' E: B0 ]2 i) }3 j+ h
返回日期date是星期几(1=星期天,2=星期一,……7=星期六,odbc标准)  . }& ~4 @1 a+ j' X: d, e
mysql> select dayofweek('1998-02-03');   
" C4 w* f6 j& F' w1 T# K  -> 3    & o  |0 g$ _5 q" s

1 C5 M. @/ N* [. y+ kweekday(date)   
9 l+ J* p- Q* ^, ]8 \" B返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。 : z3 K( I- E* n" j' u
  2 b5 f- f" s  b8 C# B* a
mysql> select weekday('1997-10-04 22:23:00');   
& K9 \: ?0 H8 @6 ?% t  l  -> 5   
9 f  l3 [! X% i+ X: Umysql> select weekday('1997-11-05');   
5 w6 U# a' L' _/ `7 [$ L  -> 2    9 ?: n, E4 n5 S! c* ]+ N
' e4 e" i2 m0 f9 N) p
dayofmonth(date)    
9 s& n2 O6 {6 }' j6 q6 w返回date是一月中的第几日(在1到31范围内)    % ~0 T+ @; F6 m7 j
mysql> select dayofmonth('1998-02-03');   
- F8 j3 _% z2 `- k2 h" w8 k& _  -> 3    ! A* i5 R( z; z( b3 Q
  l5 y" i( C0 r
dayofyear(date)   
* Z' w- T( c, a: S6 x& _返回date是一年中的第几日(在1到366范围内)   
  t; H  r, y* @4 H/ B* \# emysql> select dayofyear('1998-02-03');    : h4 \0 f7 Z- a) L2 i* Y
  -> 34   
) ~  x8 G& d! Q8 Q# K5 Q
" r' G. E2 f) _7 \' ~# W9 g8 i$ Umonth(date)    . c  A" X1 }7 S* @& _
返回date中的月份数值    0 k/ n: o5 V6 L% S% z, R6 Y. q
mysql> select month('1998-02-03');    9 v7 \4 s1 ]# d" U
  -> 2   
$ n. m3 x; K- B1 D; e
* h% \+ {# ~7 W, H& _; I- ldayname(date)    6 g  W6 _+ l  z* i6 x; b* }% {
返回date是星期几(按英文名返回)  
' f$ r6 V7 i/ O) q& y8 }mysql> select dayname("1998-02-05");    ( p! _( e5 S& ?7 o/ N4 h, B
  -> 'thursday'   
3 Z# V  j; k+ d
4 z" @5 c9 j- i! l) `/ p4 r; }; }monthname(date)    - \0 r. a1 P1 W  R9 j; ^
返回date是几月(按英文名返回)  ) \% X5 ?9 G& k. |, Y' a
mysql> select monthname("1998-02-05");   
# _* f& r) w' o  -> 'february'   
7 @+ s  _1 h( Y
7 U9 v2 {1 a' {7 `: ]2 l" iquarter(date)   
% ?/ j4 f; T! a6 u/ d! p$ Z" h返回date是一年的第几个季度    , R% A( Z5 w3 h4 j" L+ h9 |2 C
mysql> select quarter('98-04-01');    ( |3 W* I8 S8 L! {3 ^5 v
  -> 2    # @9 Q% q! F- L

& O8 \' l6 p- T1 b* i" ]week(date,first)   " d& Q: d3 j* U8 s
返回date是一年的第几周(first默认值0,first取值1表示周一是% c7 H" `$ v' j
周的开始,0从周日开始)  9 c& x" V4 t, j* l5 d/ }/ T, n
mysql> select week('1998-02-20');    * Q2 d' [# \/ F8 `
  -> 7    . v: q9 B0 ]: ^& W* x% q: z
mysql> select week('1998-02-20',0);   
/ e+ b' m) C( ^1 M3 Z5 @4 }: U6 Q  -> 7   
' m1 _* m" K& p( Q" p  dmysql> select week('1998-02-20',1);    # e2 T% B4 ~  W6 v5 [
  -> 8    % c/ H2 s+ N5 z% M  ^* S# c
/ l+ ]2 G$ v0 B6 z
year(date)   
2 G8 ~1 k0 p. M9 x7 F! G, ~返回date的年份(范围在1000到9999)    & N/ v1 k) R' _( j
mysql> select year('98-02-03');   
0 p) k& T; ]! @, e, E3 [6 x  -> 1998    7 V; D9 @2 g7 X5 x% S4 I" F! a, [
5 W2 S& N1 C5 b* ]: E" T+ \/ A
hour(time)    
  |4 q" e: B/ c6 W6 j返回time的小时数(范围是0到23)   
& [0 ], b/ R2 Y  d6 Y7 Emysql> select hour('10:05:03');   
. k4 q% W  w; C' Y- x# X  -> 10    7 Q5 u* R" B' Z9 B# z3 W

! j( B8 @  _, N3 Kminute(time)   
) p3 I4 ?; w* b返回time的分钟数(范围是0到59)    7 T2 m& Q3 ~  @7 J
mysql> select minute('98-02-03 10:05:03');    ; [% a5 U' D( u, K) i- e/ e; W7 A. ^
  -> 5    ' ^" r0 s  h* F) T$ {1 m

: s' p8 V  `2 R  q* a% ?second(time)    2 u$ F8 P% ~& i! B
返回time的秒数(范围是0到59)   , H8 W) t) X  P% |* _5 }! e* L0 ?
mysql> select second('10:05:03');    8 B* p) [8 x/ Q: ^) n
  -> 3    ! J2 o/ T: i, H
- s8 O1 {& A. s& y2 w; d- B
period_add(p,n)   
$ }' o9 ?" D& N增加n个月到时期p并返回(p的格式yymm或yyyymm)   
) f( x1 o; T' Pmysql> select period_add(9801,2);    " U: X* S3 W' I7 l3 t
  -> 199803   
8 \1 `$ G4 e6 @ 2 ^. E5 X" F7 G& R; Y$ E
period_diff(p1,p2)   
6 F; N0 M) ^; s8 f$ r8 _返回在时期p1和p2之间月数(p1和p2的格式yymm或yyyymm)  
4 m5 s' L- M1 g1 C. S# Amysql> select period_diff(9802,199703);    7 X4 `/ E6 \; v. a) S/ H! Q& R
  -> 11    - b: ~( d, J* H

: d6 T$ |  ]0 Q, f- Bdate_add(date,interval expr type)  
* N& a: A% ?# n8 q6 _) N% gdate_sub(date,interval expr type)   
: d" _0 x7 V  Uadddate(date,interval expr type)   
# b' y1 j; g- X4 |9 W( X7 xsubdate(date,interval expr type)  ! H7 B  g2 p/ t2 [( |0 t- x
对日期时间进行加减法运算  
$ k0 m5 ]5 i0 O  ^, _(adddate()和subdate()是date_add()和date_sub()的同义词,也
0 _. {, U. X7 x; ^) W可以用运算符+和-而不是函数  
0 W1 k; z  P- M% S9 Cdate是一个datetime或date值,expr对date进行加减法的一个表
3 h) V  u7 n/ A$ j. z$ `达式字符串type指明表达式expr应该如何被解释  
" `9 t1 N8 \6 w0 F) A: D [type值 含义 期望的expr格式]:  
& H, W1 d7 D5 A; N4 A' x! e$ | second 秒 seconds    0 P, ~+ F/ O, |* S! b, G) p4 o
 minute 分钟 minutes   
8 D; ~: I. t) t$ s& p7 ^' V6 }- { hour 时间 hours   
( d5 p! N! _9 a day 天 days    ; p: K/ r- b$ w9 K# g( |
 month 月 months   
. `4 e  y2 j" |1 T7 e6 Q2 Y0 r year 年 years    3 e3 u  F( k& s
 minute_second 分钟和秒 "minutes:seconds"   
  P* w9 ^# C& g  b6 T hour_minute 小时和分钟 "hours:minutes"   
/ o# x  \9 b' w5 N- T- Z day_hour 天和小时 "days hours"   
4 L4 f7 O: M9 w. g- T year_month 年和月 "years-months"   
& y7 ~" e( b% n; n4 L" `  R% m hour_second 小时, 分钟, "hours:minutes:seconds"    ; X' i2 K- j8 w+ U1 t- q5 i7 r
 day_minute 天, 小时, 分钟 "days hours:minutes"    7 l1 a! u' C# F
 day_second 天, 小时, 分钟, 秒 "days
4 C1 z4 R3 X3 Q: c  D% E# {8 ohours:minutes:seconds"
% U5 n( t' a7 L; }- ~, W expr中允许任何标点做分隔符,如果所有是date值时结果是一个
$ D) ?" T7 k8 I0 C+ n# J; [date值,否则结果是一个datetime值)  
- u& [) t: a2 K; L6 z! p' n/ P0 C 如果type关键词不完整,则mysql从右端取值,day_second因为缺
  J. Q, R) ?8 |: h6 j4 Z; E6 k( B& E少小时分钟等于minute_second)  7 X/ ]1 W+ l7 ]4 p! W
 如果增加month、year_month或year,天数大于结果月份的最大天
; M7 h3 A0 Y5 a6 z) o! C数则使用最大天数)   
; L" \, ]  J. G$ T0 fmysql> select "1997-12-31 23:59:59" + interval 1 second;  ! x, m- T6 P; @: \0 o7 X6 `
3 k5 R: P- F+ F6 H. _8 w7 a
  -> 1998-01-01 00:00:00    8 i$ w+ L1 @; o% d( R
mysql> select interval 1 day + "1997-12-31";   
1 t; o, p5 }$ m& m/ ^  -> 1998-01-01    : g; z# B- m1 c( T0 q
mysql> select "1998-01-01" - interval 1 second;   
# O9 f, f$ u/ A' K  -> 1997-12-31 23:59:59    % C; q+ t9 a: y9 c* x
mysql> select date_add("1997-12-31 23:59:59",interval 1( o) Y& |- M0 W$ R( V
second);   
6 W2 i* p) ?- N, K2 x6 g  -> 1998-01-01 00:00:00    # k6 k3 H9 d. S9 U7 {5 i3 r9 e- J
mysql> select date_add("1997-12-31 23:59:59",interval 14 y) j# _# k, u2 o; {% |# N
day);   
* n! P% H1 M! R  -> 1998-01-01 23:59:59   
% u6 S+ E% R6 B1 e; w0 C) d" nmysql> select date_add("1997-12-31 23:59:59",interval- L1 R3 _& S( d1 W
"1:1" minute_second);    , a# o2 z% `* l9 Q$ T5 |- @
  -> 1998-01-01 00:01:00    5 E% h' v6 ~% F% N
mysql> select date_sub("1998-01-01 00:00:00",interval "1
8 M# e  @& [# p# z  ~' ]1:1:1" day_second);   
  W& L% A+ U  C& o! F- X. s  -> 1997-12-30 22:58:59   
4 J/ ^# i( l/ J: }" nmysql> select date_add("1998-01-01 00:00:00", interval "-1, ?2 k3 K4 N2 Q- A9 l
10" day_hour);  . j7 d3 U# f5 c9 W  V& v  F
  -> 1997-12-30 14:00:00   
/ ]0 S! ?% K$ z5 Z6 }+ S/ F$ imysql> select date_sub("1998-01-02", interval 31 day);    1 h% a5 E+ t0 A" `
  -> 1997-12-02   
. C4 B) g, l; q/ X6 o1 imysql> select extract(year from "1999-07-02");    4 q7 i3 ^' }" T, o
  -> 1999    ; D: B- I4 u2 h- Z& U* Y
mysql> select extract(year_month from "1999-07-02% q; |0 `0 ]2 N9 E6 L. M
01:02:03");   
4 ~% y2 ?8 f8 P7 x  -> 199907    . d1 J; R. g) `4 Q. v4 ~
mysql> select extract(day_minute from "1999-07-02, i) e# c" r0 w: j4 w" p" U
01:02:03");   
4 P4 {- `$ A. F- o4 E  -> 20102   
( e4 \0 I. ~% k# j , ]' k6 P: m' V) _: r
to_days(date)   
, C) H1 |$ k; T1 ^' l# A' E返回日期date是西元0年至今多少天(不计算1582年以前)  + }2 c. J( ?9 T) V  t# _! s
mysql> select to_days(950501);   
" B' m$ O5 z( {. m  -> 728779   
* a; h6 S' P- o. P& r% `3 Umysql> select to_days('1997-10-07');    ( o0 Z/ r% q4 M) P: {( M3 k$ T
  -> 729669    * X0 K; W4 D, _; t, l/ m, h0 r
4 A( n8 M6 m5 V) C
from_days(n)   
5 P# m3 `* H0 R- n" ^% `- N1 } 给出西元0年至今多少天返回date值(不计算1582年以前)   
$ u+ a8 F; F& K8 [% lmysql> select from_days(729669);    / b; l6 w$ k# }# X
  -> '1997-10-07'   
1 A( P1 v7 D9 v
7 ]( b( ]6 a0 X/ Jdate_format(date,format)   
4 W9 Y  O7 _/ Y/ w- a% K. f 根据format字符串格式化date值  
; c; c8 Q6 g5 J (在format字符串中可用标志符:  * R- B8 ^- y  F+ w, N3 \, C
 %m 月名字(january……december)   
( c% p! [( [7 h4 p8 W6 z5 s; \ %w 星期名字(sunday……saturday)    ! M" v4 ^/ o$ Q/ o2 \. z8 R3 O
 %d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)   
: C  @% Y: i' W- C" L0 n %y 年, 数字, 4 位   
0 j$ D5 X( A5 x  _ %y 年, 数字, 2 位    / T4 I7 @* q, U' u
 %a 缩写的星期名字(sun……sat)    2 y# B" X# p' f9 J
 %d 月份中的天数, 数字(00……31)    & f4 G( e# X" y8 }/ B4 r" N0 w0 Z4 C
 %e 月份中的天数, 数字(0……31)    7 e1 W: y; G8 d6 k8 q3 n
 %m 月, 数字(01……12)   
. U! y  w  v7 _: Y; |' a" w7 z %c 月, 数字(1……12)    2 s$ l7 [9 p; e: ~
 %b 缩写的月份名字(jan……dec)   
8 W+ L# p! R/ ^. S& s( L %j 一年中的天数(001……366)   
- M, a$ p. I6 b) G! v %h 小时(00……23)    - N! e" h+ `# c3 g* z1 U& X6 c
 %k 小时(0……23)   
/ d: O4 ]$ H0 S: ^- V %h 小时(01……12)   
3 Y1 B) ^! b3 S6 n! R/ \ %i 小时(01……12)    7 X% Q+ U0 T  J1 d! ?
 %l 小时(1……12)    ) {* [/ M% s7 e* B. e! d
 %i 分钟, 数字(00……59)    2 X& i: V: Q# g" g/ p
 %r 时间,12 小时(hh:mm:ss [ap]m)   
: ~( s: f' L/ A3 \  ^4 E6 {" Z, e %t 时间,24 小时(hh:mm:ss)   
( a$ N: s# T# ^3 ~2 i1 c %s 秒(00……59)    8 z% M& [5 [2 Y' K- g( n
 %s 秒(00……59)    9 E( v. c2 F4 R
 %p am或pm   
9 D. C! Y* K" c5 _4 G5 |3 Z %w 一个星期中的天数(0=sunday ……6=saturday )    ) V& L9 @, a* i; p6 \8 b# Q4 A
 %u 星期(0……52), 这里星期天是星期的第一天   
" h9 L4 X( B8 O0 r/ n) V" _ %u 星期(0……52), 这里星期一是星期的第一天   
  y+ V5 N2 ]$ V, V %% 字符% )  
! y3 s; g1 l. }; gmysql> select date_format('1997-10-04 22:23:00','%w %m %
+ Q: U" w! U& `  u0 Py');    / M$ {3 v9 R/ Y; z( \- R( ^0 O
  -> 'saturday october 1997'   
, t' U$ L8 D) j/ ?mysql> select date_format('1997-10-04 22:23:00','%h:%i:%
* |% X% e# g' d! hs');   
  Y5 B# f+ q) m6 x  ?, J; @  -> '22:23:00'   
! S: i+ H; C5 G$ Z* _4 gmysql> select date_format('1997-10-04 22:23:00','%d %y %a. o" a/ w3 u( c' X1 ]$ h7 o
%d %m %b %j');   
4 C7 Q8 x$ }8 g; K- V" ?  -> '4th 97 sat 04 10 oct 277'    7 F8 e4 e/ ?1 G
mysql> select date_format('1997-10-04 22:23:00','%h %k %i. b$ v' x( r5 p( P  @( n( ]
%r %t %s %w');   
% C3 t8 i  b. y9 S) n0 Y: ~/ ^4 G  -> '22 22 10 10:23:00 pm 22:23:00 00 6'   
( y; y  z; Y$ M3 q; \ + K( ^: v& G; R' C! A
time_format(time,format)  
' N9 v, y6 `9 ~" v1 F# ]2 O 和date_format()类似,但time_format只处理小时、分钟和秒(其
) @4 v& A3 \8 H( i8 x余符号产生一个null值或0)  . d: @+ y! t5 x" ]  P

+ m) V" V& n7 u3 c7 fcurdate()     
* N( N5 U- s& V' Z+ m( Ncurrent_date()  ) V  B+ W. W% |3 U
 以'yyyy-mm-dd'或yyyymmdd格式返回当前日期值(根据返回值所
) K) }: p3 n9 E6 ]" l  i处上下文是字符串或数字)   
8 F# n/ F( W$ \8 i8 O  {4 Qmysql> select curdate();   
; S# B# B& b4 E3 k$ {  -> '1997-12-15'    ( u5 [7 S8 `- k+ z2 B
mysql> select curdate() + 0;   
+ g4 ^' q9 d2 P. W& K! M2 D4 H0 {  -> 19971215   
7 I& ^! N* P, N
" W0 p* ~* }2 t  r, Kcurtime()    * R/ t7 D! m# i: _
current_time()  
. _, F: q- W$ r$ \* M' v 以'hh:mm:ss'或hhmmss格式返回当前时间值(根据返回值所处上' g- I0 o1 k' L! l
下文是字符串或数字)      , e: m& T6 Y# m2 V$ P! G
mysql> select curtime();    7 W! f0 C- V7 u
  -> '23:50:26'   
0 g$ W/ D$ X- imysql> select curtime() + 0;    , f/ }9 Z# m8 v" D6 g
  -> 235026    5 c0 V% i$ |0 ^
& ]2 b5 t# s8 d$ X- o
now()    
" }. c/ f/ N; X6 _" m8 U, Msysdate()    , s' U; M2 j( D! N6 v# n
current_timestamp()  " f- C' y* d; O
 以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回当前日期$ B" @+ R; Y! y7 P" T9 ]& j; f
时间(根据返回值所处上下文是字符串或数字)     
2 t* K  a1 _* K, Q* q$ x4 F6 Umysql> select now();    6 J2 W. X( |8 b5 }
  -> '1997-12-15 23:50:26'   
  L) c4 r* U! a, a$ `mysql> select now() + 0;    3 M8 R8 t+ ]# f: r2 Y
  -> 19971215235026    / G  K0 k$ w6 J- R; m! c2 B+ O
7 D: X5 ?! V" S
unix_timestamp()   
' y1 K4 O% U, }! K( Y- f( V' nunix_timestamp(date)    - F; f  ~8 K' `2 U9 e
返回一个unix时间戳(从'1970-01-01 00:00:00'gmt开始的秒
# p  a; `+ _$ l; {5 N数,date默认值为当前时间)  
+ F  v1 n; _+ ]( S3 F9 imysql> select unix_timestamp();   
9 N! Y: t; U( u& f# c  ~7 g  -> 882226357   
: G7 f' E) m  ~5 z) ymysql> select unix_timestamp('1997-10-04 22:23:00');    ! `$ Z+ x! F/ c- n
  -> 875996580   
7 [. d5 ]2 q7 q( \( i# F3 B9 r * I6 t0 a; `9 l4 o" e  i
from_unixtime(unix_timestamp)    * R0 r9 _% N5 D- x7 U9 c3 _- G
以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回时间戳的9 w( y7 D2 |) }' I
值(根据返回值所处上下文是字符串或数字)     
0 m& f6 I5 }$ p, S- x6 r; }  O6 Xmysql> select from_unixtime(875996580);    . u; v/ g- P! |/ d8 P
  -> '1997-10-04 22:23:00'    0 {, w/ I, U3 h: ^: G
mysql> select from_unixtime(875996580) + 0;   
3 L. O' ~5 x# \( Q  -> 19971004222300   
/ w% A$ j: C4 K& w 6 d6 D8 Q' B$ @0 c. o* E6 q
from_unixtime(unix_timestamp,format)    
7 l6 O/ p8 v4 \; s* j以format字符串格式返回时间戳的值  
7 H1 L3 X* }9 dmysql> select from_unixtime(unix_timestamp(),'%y %d %m %
, I$ e; b8 b% Z2 Oh:%i:%s %x');    1 P; [+ g; @" ]" `$ T% K4 P
  -> '1997 23rd december 03:43:30 x'    ; c: |' r1 [5 v5 V5 ^. ^
& a. ?5 e8 s  N/ ]9 ]5 L
sec_to_time(seconds)    7 f) k0 H0 r; r2 |
以'hh:mm:ss'或hhmmss格式返回秒数转成的time值(根据返回值所处上下文是字符串或数字)     
: j6 ?, u. K4 D0 u* Q& w# U1 S; Mmysql> select sec_to_time(2378);   
& k3 c$ r0 h, v5 _9 Q  -> '00:39:38'    9 q5 N9 }0 y5 P) G' o. [5 s2 Z# n
mysql> select sec_to_time(2378) + 0;    ) n3 I) M# @: _  W6 f8 ~
  -> 3938   
, T# U; P& k: B- Q* k# A/ ~: @4 a/ [
& u3 c1 g' M& k; T9 @1 ttime_to_sec(time)   
: X* J1 E; v$ e返回time值有多少秒   
/ @0 E$ W4 L% j. dmysql> select time_to_sec('22:23:00');   
* Y* D) e% h* W7 q' G" r4 r. K  -> 80580    ' n8 W2 n4 U) t, U# n" e) S
mysql> select time_to_sec('00:39:38');    2 p4 }8 v3 l9 J, b3 x
  -> 2378
2 P4 D# l9 h% h8 o. G3 o 8 \& U  d+ v; c2 y2 r/ `
转换函数% F1 x9 f4 u0 m; \+ x
cast
" g  D$ [) ^& t" ]用法:cast(字段 as 数据类型) [当然是否可以成功转换,还要看数据类型强制转化时注意的问题]" T' t1 ^3 z- x, a6 \) A
实例:select cast(a as unsigned) as b from cardserver where order by b desc;
: v5 m( ~5 Z9 k8 S; @convert:+ h5 o8 p3 R* M0 C* X) `
用法:convert(字段,数据类型)
+ C( T" l6 [$ i9 m1 G0 }实例:select convert(a ,unsigned) as b from cardserver where order by b desc;
: D, F) y( z! c" ]) ~7 k- D
回复

使用道具 举报

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

本版积分规则

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