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

mysql常用函数大全

[复制链接]
跳转到指定楼层
楼主
发表于 2018-3-21 16:07:02 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
1、字符串函数
/ I4 T* t' B0 bascii(str)   ! W4 X# t: E$ [. a3 M' ?
返回字符串str的第一个字符的ascii值(str是空串时返回0)    |/ U# W4 i0 G2 m$ H5 V
mysql> select ascii('2');  % Y8 @8 V$ K* n* l- x$ s
  -> 50  # ]5 f# `, C+ q% M$ E3 g! `. j
mysql> select ascii(2);  ; z+ d2 [  Y  R; U4 w
  -> 50  
  l# B! k  k. [* x& dmysql> select ascii('dete');  
5 ]" ~) r  C* x  -> 100

! C! t: s3 F. g) @- E3 k0 Z2 ^ord(str)   4 f% l. o" ]- Y8 `2 A- U
如果字符串str句首是单字节返回与ascii()函数返回的相同值。
6 b! t. Q: Y$ c/ }+ X. @6 O
; n6 S7 W. ~! o$ i如果是一个多字节字符,以格式返回((first byte ascii code)*256+(second byte ascii code))[*256+third byte asciicode...]  / [1 W) L: L7 [  @# j8 ?
mysql> select ord('2');  8 I) O4 l+ R* Q5 t! c/ r; s' Z, B
  -> 50  
, o/ t2 o4 m3 L* t& D6 J' l5 i. {   
  c) R( o! q/ S* R9 `& Sconv(n,from_base,to_base)   
- h, W2 M; i4 l- x& Q- W* W0 i对数字n进制转换,并转换为字串返回(任何参数为null时返回null,进制范围为2-36进制,当to_base是负数时n作为有符号数否则作无符号数,conv以64位点精度工作)  3 ~0 [9 n  l9 I! {5 _
mysql> select conv("a",16,2);  6 v; ]2 R6 [0 {. |
  -> '1010' # ?4 N) P- M5 M& D) ~$ i
mysql> select conv("6e",18,8);  * T4 Z6 ^$ I" }6 ?* n: a
  -> '172' 7 {6 \" }8 P! U5 C2 a3 T  K
mysql> select conv(-17,10,-18);  
8 q% \" |) Y$ Z  -> '-h'
; g, u0 }9 o+ h: o& c% W3 M; j- bmysql> select conv(10+"10"+'10'+0xa,10,10);  , t' W! C5 s6 O9 I
  -> '40'
1 q/ s5 a4 z# V6 ~/ J/ z; o   2 C; ]8 d# ~* s$ @- }
bin(n)   
- D7 y* z  p" {8 v把n转为二进制值并以字串返回(n是bigint数字,等价于conv(n,10,2))  
$ H* w. T6 @3 f6 W8 Vmysql> select bin(12);  
4 p5 n' B0 x" H# \! m- l1 x  -> '1100' ) H* E3 G( O5 Z$ m4 y
; P# y; C. q" H1 n
oct(n)   
* F) d8 K! f% y6 V4 b  g/ y把n转为八进制值并以字串返回(n是bigint数字,等价于conv(n,10,8))  ! y* Y5 O. E$ q- N5 @4 C
mysql> select oct(12);  
) d. l7 n4 N, G7 F  -> '14' 7 Z- U7 i1 d3 @: F
   
& w- Y7 {, h$ K) B8 w6 phex(n)   
+ l; |) R. {  I* b1 ~) T) m3 l把n转为十六进制并以字串返回(n是bigint数字,等价于conv(n,10,16))  $ k. P( m+ \  D& e+ j% u# `- ]
mysql> select hex(255);  
' ~& e7 N7 z# l: y, Y. `9 J% K$ B  -> 'ff' 5 |4 ]& d( i* X' U! I
   ) c9 z! Q2 |0 G( y
char(n,...)   
! u( Q. v! I7 ~4 w" A返回由参数n,...对应的ascii代码字符组成的一个字串(参数是n,...是数字序列,null值被跳过)   
" ~8 i1 m2 }" n# ?  l: smysql> select char(77,121,83,81,'76');  9 n5 @, p* L0 @1 \- o8 Y
  -> 'mysql' $ m& g$ I3 t  P* i' m: B
mysql> select char(77,77.3,'77.3');  5 x; c& v/ z) _1 N, Z7 b
  -> 'mmm' 0 f9 [$ w* p8 n
   7 |! p/ P3 F; \0 g$ c
concat(str1,str2,...)     l! j) J* n; e5 T# `$ ]
把参数连成一个长字符串并返回(任何参数是null时返回null)  $ B: D" R5 M. r1 z; S! E
mysql> select concat('my', 's', 'ql');  
. W1 a* t3 C  j6 y  F7 o1 K  -> 'mysql'
+ K# Y2 J& T5 v" umysql> select concat('my', null, 'ql');  
) A/ J1 ?4 G6 ~8 U  -> null
: F& Y+ f, S9 g! B! f6 ]* l' tmysql> select concat(14.3);  # T- `, [2 ]( d8 f9 q$ c
  -> '14.3' ( L, i" N* Z. S  R
2 ^- k) }( [; p) R! i" @+ b% T
length(str)   4 s" J/ K& P' v( Z' G8 t- n  w9 \
octet_length(str)    B) y- I( ~4 W2 C" A
char_length(str)  
6 [1 d9 R& K) {# f- Qcharacter_length(str)  
7 u9 c4 U1 k! T( a+ b6 b& u) }  A返回字符串str的长度(对于多字节字符char_length仅计算一次)
6 i  m# ^) \1 q! Imysql> select length('text');  
" H1 S& j. I) s8 R& C  -> 4  
; Q7 \7 i. s1 x% Q7 r( wmysql> select octet_length('text');  
; ]- q: D1 t# _8 P& g- q8 N( ^  -> 4  
& [$ F2 B5 B6 b$ o; y+ \& x7 v
- n  f  W9 z. ~8 {9 Q! G# rlocate(substr,str)   
' q" Y! ]- r7 s2 Mposition(substr in str)   
) X) ]9 J) o) P5 \返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  2 _2 M6 B( V$ C1 E2 ~
mysql> select locate('bar', 'foobarbar');  
9 K3 J5 e  ^6 h' R$ k- V) m  -> 4  
; Z1 b& F& B. S- Emysql> select locate('xbar', 'foobar');  
9 }9 y+ \5 f0 J; M) S8 ]  -> 0  
& e7 L! R2 M; Q" E; V8 E   
$ y& u6 F- I/ V# }locate(substr,str,pos)
5 n+ H* [3 s) ?3 t$ ]# m" O返回字符串substr在字符串str的第pos个位置起第一次出现的位置(str不包含substr时返回0)  
! r6 Y: ^! \8 q0 N+ Jmysql> select locate('bar', 'foobarbar',5);  
  l& D. i  {/ o. p% Y/ F4 H  -> 7  
  q; T$ i  ~: A9 ~5 \' @  a
2 t! V, d% ~  S; a5 Zinstr(str,substr)   $ I# G$ B" T* d
返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)  + U6 t  O" e) _
mysql> select instr('foobarbar', 'bar');  
# i" g" W$ K2 N8 X4 L5 Q# r  -> 4  " J7 {# t! t7 u- j9 g5 Z( h. I2 `: \
mysql> select instr('xbar', 'foobar');  1 D/ b0 k* P. E* a  p& B9 \+ k
  -> 0   
5 n! \" t2 f# _9 P$ F' ~4 M" E
: d  I& [1 y' ?( M. _lpad(str,len,padstr)   , `, Q; N2 n. D* S' w
用字符串padstr填补str左端直到字串长度为len并返回  & T3 l2 t' b% Y* Y6 A! M
mysql> select lpad('hi',4,'??');  5 z4 a9 p4 o4 P* e8 i
  -> '??hi' 3 a# o8 D7 P* B# i* r& J& @% Y
   
4 i1 ?* x  g$ @rpad(str,len,padstr)   
! n5 K* }0 W4 j2 t$ j) C用字符串padstr填补str右端直到字串长度为len并返回  6 O  [  t1 E5 E$ K) V( N
mysql> select rpad('hi',5,'?');  ( Y' |7 A- ^+ W( y
  -> 'hi???'
4 w# A2 [; s2 @! R * U% r3 h8 Z( ^
left(str,len)   
7 k6 f+ ~: {  u" O6 ~3 x返回字符串str的左端len个字符  + m/ _! W+ {* r2 x! H( ^
mysql> select left('foobarbar', 5);  
$ W5 L  [7 h. W# C" Q  -> 'fooba'
; R2 r) R- [7 L( k' U/ V6 c( j* F 9 S" `, e; O% b. |, ?
right(str,len)   1 E& H# h' ?& B+ X* B, C7 x8 j
返回字符串str的右端len个字符   
6 w+ N, Y3 N& _mysql> select right('foobarbar', 4);  
) ]5 B# {" @4 ]2 g+ R4 m- @  -> 'rbar' . p+ ?% b4 q) B8 P. K$ F
9 T- r, W) w8 K, {' S) @$ D
substring(str,pos,len)   
* t! W+ U5 ]! U! w  q) msubstring(str from pos for len)   + J; Y  f/ T$ V
mid(str,pos,len)   6 x1 E& i& {& c. E
返回字符串str的位置pos起len个字符mysql> select substring('quadratically',5,6);  
/ \4 X& [, ^7 p5 P* }: b) H  -> 'ratica'
1 A! c, v! M9 Q% a8 c, L+ R
+ B7 ^: F$ e5 z( P; osubstring(str,pos)   
- e, K! Z, ~) R" @: O$ s  Ksubstring(str from pos)   
& b' U4 r) M) a返回字符串str的位置pos起的一个子串  
& m. P6 N' b% Y0 Zmysql> select substring('quadratically',5);  
/ h6 x/ r' N9 T/ v; e  -> 'ratically'
8 }% D* n3 q$ O$ E2 `+ pmysql> select substring('foobarbar' from 4);  
* b, S& Y, ^2 C* D  -> 'barbar'
& l2 ?5 E# ^" E% C 3 Z3 p1 \# S$ O
substring_index(str,delim,count)   ; d9 ^, _1 ]& i0 ]- l, z
返回从字符串str的第count个出现的分隔符delim之后的子串
/ L% Z( [  G  R, `& a(count为正数时返回左端,否则返回右端子串)  
6 q* |$ n$ p' P/ w1 l9 i. Zmysql> select substring_index('www.mysql.com', '.', 2);  
1 O4 Z5 W$ B( I* y, ?  -> 'www.mysql' 8 `; _! [" B$ e1 E1 `
mysql> select substring_index('www.mysql.com', '.', -2);  
4 [( U( V6 g" a6 |1 `7 G4 G  -> 'mysql.com' 7 x$ r- u: L, W* k5 L, ?1 U
/ Z/ U! k$ i! M
ltrim(str)   
' Z9 s. |1 T, W2 M* ]0 u返回删除了左空格的字符串str  * u/ A3 `$ \0 O7 r2 A9 n+ ^, T
mysql> select ltrim('  barbar');  ) f: e, H, S. Y( v
  -> 'barbar'
$ W2 N# |3 p5 }) j - N+ Q- z& l" H& `- Z
rtrim(str)   6 \9 U5 S0 }# t4 z' _7 Z* j5 ^
返回删除了右空格的字符串str  . r1 Z3 ]1 m( s3 c5 k: _/ q
mysql> select rtrim('barbar   ');  * ?' P( N/ c' `/ g
  -> 'barbar' 5 a. I1 _, B. |9 B9 x: F* G

3 H9 M6 ]2 F* Otrim([[both | leading | trailing] [remstr] from] str)   4 c. i" T$ S9 _9 Z' W& f3 S0 l
返回前缀或后缀remstr被删除了的字符串str(位置参数默认both,remstr默认值为空格)  8 N* X; ?" x5 G) K
mysql> select trim('  bar   ');  6 U/ I0 H( v8 V1 {, X
  -> 'bar'
" o3 }0 j7 @- B7 Z( [mysql> select trim(leading 'x' from 'xxxbarxxx');  
4 j6 V8 {: P" _  -> 'barxxx'
$ f3 a1 `" I; H( `mysql> select trim(both 'x' from 'xxxbarxxx');  8 J& n, ^- U. Y& f* a$ |) ]
  -> 'bar' 5 K, I" v" S* m0 V5 |8 s
mysql> select trim(trailing 'xyz' from 'barxxyz');  
# Z$ X9 t1 F) ?# ?% |2 W  -> 'barx'
( E9 D7 |4 O; C5 q  t7 O8 Q ! `) [9 G3 h) P
soundex(str)   ( M# J+ m- X2 s: e
返回str的一个同音字符串(听起来“大致相同”字符串有相同的4 D) \! m) _- u* T
同音字符串,非数字字母字符被忽略,在a-z外的字母被当作元音)  " _# \4 S* M+ u5 T7 ?& g
mysql> select soundex('hello');  
$ b6 [. w( C' `7 T. \* `  -> 'h400'
6 X' W6 m) l' K+ K4 Mmysql> select soundex('quadratically');  
0 Y1 `0 D( ?0 @' w3 T  -> 'q36324'
1 h- `4 ^- w- f* o" U   : d" X, x3 U, c& N8 ]& K
space(n)   
0 ?4 F; _9 u3 Y7 m& U7 A* k/ W. _返回由n个空格字符组成的一个字符串  
7 H0 U; t$ v3 }  S  l: `mysql> select space(6);  
) B: \" ^' P8 A9 Y0 ?  -> '      '
9 Y( r4 A7 |4 X$ Z! C+ N   8 I9 n3 r3 o4 D  X; x" \. K& w
replace(str,from_str,to_str)   : h6 Z; ~' Y) Q$ ?) L% T
用字符串to_str替换字符串str中的子串from_str并返回  
0 d0 |, A" S( V& u& L- C  gmysql> select replace('www.mysql.com', 'w', 'ww');  
9 }- d+ j1 d% E% t/ G  -> 'wwwwww.mysql.com'
; l$ u7 ?  s7 l" j & ]0 m/ ~- ~0 N4 B6 w# j# P4 v& x
repeat(str,count)  
% d1 u9 w$ Y: L1 `, A0 I% \返回由count个字符串str连成的一个字符串(任何参数为null时
, ]- F0 T) e( t8 R返回null,count<=0时返回一个空字符串)  
; g6 v$ [) g& g8 e; g& q6 nmysql> select repeat('mysql', 3);  - H" k+ \# l7 ?# K
  -> 'mysqlmysqlmysql'
7 M* I9 k! }2 y  ]   
6 |2 K' N* S. K9 \1 {: Dreverse(str)   
" ]- F$ j& ^! u/ E9 V( f7 b) H颠倒字符串str的字符顺序并返回  
& c9 @. z: h7 z0 x8 kmysql> select reverse('abc');    m# u6 t$ x2 X% ^1 v
  -> 'cba'
' Y, A+ f0 m5 X
! q& @( N. Z0 M- e) a5 D! _8 Ninsert(str,pos,len,newstr)   0 y0 j- F/ a% w4 C0 Q6 u
把字符串str由位置pos起len个字符长的子串替换为字符串
# H: X  D* \9 dnewstr并返回  
( Y- V# b$ e1 Z0 G8 Nmysql> select insert('quadratic', 3, 4, 'what');  5 z% n  I3 k, I* m
  -> 'quwhattic' 1 f4 ~# u# c7 z- w: Z- N

1 T1 @  d  `4 g9 aelt(n,str1,str2,str3,...)   
* Y, t) B# p* F/ t& w返回第n个字符串(n小于1或大于参数个数返回null)  
' g4 y" Q$ I- q' S3 hmysql> select elt(1, 'ej', 'heja', 'hej', 'foo');  $ y9 p& X3 u- X4 p$ H
  -> 'ej'
- v1 R1 Q- d- x& d! Pmysql> select elt(4, 'ej', 'heja', 'hej', 'foo');  ( m: e4 o" B3 `0 E/ q0 C
  -> 'foo' # I4 p4 p5 Y7 t# v( d( Q  {4 k/ `

: i" d3 p3 y  r) Ufield(str,str1,str2,str3,...)  
9 K# a0 Z6 ^2 @返回str等于其后的第n个字符串的序号(如果str没找到返回0)  
! Y. ~( M. N' k3 rmysql> select field('ej', 'hej', 'ej', 'heja', 'hej',
. y5 p" R+ t( Q8 T/ a$ Q* |'foo');  7 t$ e  L3 U" j
  -> 2  2 Z2 N( ~8 C/ }3 W% U- U: a  e3 y
mysql> select field('fo', 'hej', 'ej', 'heja', 'hej',
; O* Z* S; E/ U  D'foo');  
5 h2 ?) Q& T5 P2 o6 o; `; I. M  -> 0  % m; A- b/ L6 o& a' ?% f2 W2 Y; F

. A0 e! S( n6 L. J; V( Vfind_in_set(str,strlist)   7 N5 U7 B4 v6 a" j* e6 E2 _
返回str在字符串集strlist中的序号(任何参数是null则返回% \1 H1 d/ ?% |9 ]
null,如果str没找到返回0,参数1包含","时工作异常)  ! I+ n2 e* e9 b5 f- }
mysql> select find_in_set('b','a,b,c,d');  
% @$ r, D: `) N  -> 2  
' G, E1 y/ g  B   7 l4 C* r  V) G
make_set(bits,str1,str2,...)  1 U$ O& p2 N- E6 [" [
把参数1的数字转为二进制,假如某个位置的二进制位等于1,对应
1 D; W. @. c# J) [. o. ?位置的字串选入字串集并返回(null串不添加到结果中)  ( P. ]5 N2 K: d  y+ o
mysql> select make_set(1,'a','b','c');  8 C( g% L! f& w9 [( o
  -> 'a' , n- U  {& W- t. F6 S3 l. I
mysql> select make_set(1 | 4,'hello','nice','world');  
. S) z4 Z" g/ N3 h: Y3 O) A8 \  -> 'hello,world' , L, c. u% _" T! o7 Y, k4 n$ ]7 e4 \& I
mysql> select make_set(0,'a','b','c');  
% j( c5 F7 t3 ^7 _2 z  -> '' $ `- i, c( R7 h$ c7 ?, Z/ o5 o) V
: B! }7 r( P8 c. W
export_set(bits,on,off,[separator,[number_of_bits]])     Z3 ?8 Y4 v# @2 _: d" z
按bits排列字符串集,只有当位等于1时插入字串on,否则插入
! U$ b8 c6 d2 n4 x  g8 J- roff(separator默认值",",number_of_bits参数使用时长度不足补0
5 h# g0 `- R! f; s而过长截断)   
& ~6 h! t1 K; B! amysql> select export_set(5,'y','n',',',4)  
* f" W# |7 m8 |& `" H9 d) U  -> y,n,y,n   
. ^4 ~7 z  B: h( l1 m
" I  O' ~; T  wlcase(str)  3 r9 x+ @! R6 t  O6 |
lower(str)   
8 p# N$ V% a9 ]  U! d+ S返回小写的字符串str  3 k. L4 b6 e! V8 B6 _$ P% a# w8 U$ g
mysql> select lcase('quadratically');  / @2 K+ o7 F3 T" r: p! O3 w7 [
  -> 'quadratically' . Q- L7 c0 S( z7 K# D
   4 a' J3 B0 W; l3 I: Z3 v
ucase(str)   
" D: F7 b1 i9 zupper(str)   , A1 l5 _; p* l5 q8 c+ c
返回大写的字符串str  
; D/ J5 U6 q# ~# b, C2 Amysql> select ucase('quadratically');  & R2 n% ?% o) |, X. G, G+ ]
  -> 'quadratically' 1 {. G/ |& I# A+ c
8 u1 @+ x, Q1 f' i
load_file(file_name)   
0 w+ x3 F1 L6 A读入文件并且作为一个字符串返回文件内容(文件无法找到,路径: ?/ a/ S/ H/ g4 X) [: {
不完整,没有权限,长度大于max_allowed_packet会返回null)    z1 G/ w" F5 F6 w% s' ?# j  z
mysql> update table_name set blob_column=load_file
1 L. x7 u) Z! v- H( I3 i4 `("/tmp/picture") where id=1;  6 i4 z% L5 }: V9 s
( y/ \9 B+ m3 k: Y4 T8 |
2、数学函数
- N, {* _# Z- q8 iabs(n) ) m/ g9 A* K  s; ?; j! l9 W9 r
返回n的绝对值  ( |, O! b9 Q0 y. W% F, t; E  t
mysql> select abs(2);   
6 Q9 |/ a) L# \  -> 2    , e* P9 m/ I" {
mysql> select abs(-32);   
- |0 I( m/ D4 q' x  -> 32   
, W  q3 u& O+ l! l6 H; ~   7 H0 B7 n5 L2 O
sign(n)  & U8 @! L" ]+ F9 F4 N
返回参数的符号(为-1、0或1)  
1 X3 U; I  ]/ jmysql> select sign(-32);   
9 I: Y& w! o8 d2 i  -> -1   
+ V- B/ a9 o3 Y' Y' Emysql> select sign(0);   
; t* M( [" S) _  U+ y) n  -> 0   
& g# j* |) Z$ F- q& e/ C" Hmysql> select sign(234);    & `+ v, Z$ A' o- C6 K* i
  -> 1   
3 [5 M& T, j, F9 |
( F! M2 g% Z/ Zmod(n,m)   
/ E* ^7 _( a$ `. P! o取模运算,返回n被m除的余数(同%操作符)    8 |. p+ H9 G3 m$ L* C' e' C
mysql> select mod(234, 10);   
0 \' q$ P* B  d4 g! c, }  -> 4   
; a! W. b  F1 C; y3 G. M9 nmysql> select 234 % 10;   
$ R% R; ?2 `+ N, q9 [  -> 4    4 u4 `  z$ I" Y3 ]. B$ B& G
mysql> select mod(29,9);    4 {7 L' M+ o. c+ A6 D% U
  -> 2    . B! I' Q8 z$ k0 e$ p
4 K# l( C7 h) w- U3 l/ i" B% V  S
floor(n)  ; [3 D6 n( g% j7 f
返回不大于n的最大整数值  
" D" \1 ^. Y7 r1 Gmysql> select floor(1.23);   
- d) C' t- C# ~  -> 1   
; i% n( ?1 u$ h; h7 ~% j$ u6 ^mysql> select floor(-1.23);   
( U5 u3 N: D5 }  -> -2    1 \, \/ f( r/ p- R6 Q
. ]5 w/ l, f  `2 [
ceiling(n)  , M5 V. ~9 U& O
返回不小于n的最小整数值  ( w( {. M6 ~& U. K% a( A
mysql> select ceiling(1.23);    $ T! c) W+ f  L" }0 Q: S3 i
  -> 2   
% n9 X$ S! l0 `mysql> select ceiling(-1.23);    ' _7 j! n  ~6 Y& n; R
  -> -1   
  s6 Y* y+ K' R7 M# ?5 L
3 ]4 \# \" b# o8 tround(n,d)  
4 z7 E$ B$ s1 H$ n返回n的四舍五入值,保留d位小数(d的默认值为0)  $ q: j' p3 _. v3 y
mysql> select round(-1.23);    / T" t. u! s( n
  -> -1   
! B6 H9 k; D" v' `" m$ @mysql> select round(-1.58);   
" \2 K1 a1 V+ a' h" u# w! _9 Y  -> -2   
/ @7 Y4 C: n0 }/ p- S" h% Y- P5 m+ ~mysql> select round(1.58);    $ X1 a7 k5 s$ r% E* K
  -> 2   
- ]* j3 B0 l% j8 C+ I( L% Q- p8 L7 ?mysql> select round(1.298, 1);   
5 Q) U4 I$ G: d/ E% j% x1 R) t  -> 1.3   
9 ]1 W1 K& h' c2 r' [. W4 dmysql> select round(1.298, 0);    * G: ^; e( m9 Y; j
  -> 1    % e4 u. \3 s# o/ Q
- ]# |" p7 V2 t. n- V4 Q: s
exp(n)  
5 X& G' K2 I$ K6 }( Z  |$ S* H( B返回值e的n次方(自然对数的底)  
$ u& |+ y/ V' ~0 q! \/ smysql> select exp(2);    6 q5 R" r" x" j3 r0 Y' ]
  -> 7.389056   
; W/ Q4 M9 D& ^- ^9 pmysql> select exp(-2);    - @4 [) d4 V: _' m' h2 [) ]1 x
  -> 0.135335   
0 k* j( j4 o. B* s8 X& w" @
+ h* h2 J* Y1 K& dlog(n)  
7 l6 t( v, \) F" b# s返回n的自然对数  1 W/ e% b. L1 @
mysql> select log(2);   
/ r  b$ e4 ~0 N- r3 r6 T1 [  -> 0.693147   
: M: {% S2 C5 O) x; Hmysql> select log(-2);   
  w4 Z+ X0 v& X( Y- @  -> null    - p; m& s* {* t
0 {$ ?: N6 `6 M2 b$ q- f
log10(n)  $ V; p+ z4 M) k6 n7 y8 u) K8 A+ i
返回n以10为底的对数  ' Z9 q, K3 s/ ^9 q( A3 l. l) W5 |
mysql> select log10(2);    8 T  p3 {4 l( @9 `! I
  -> 0.301030    & ~5 z$ P5 f$ U* V0 k8 F
mysql> select log10(100);    6 ^: H2 _9 |0 x* s4 @( S6 H# Q
  -> 2.000000    $ u1 m, B3 [4 L8 u( |& m8 Z: I
mysql> select log10(-100);   
6 s4 D. _* A. o/ ~  -> null   
$ K( X4 Z- x* k5 K) a4 t/ _
5 w. L% V- R$ hpow(x,y)    ! ~9 ~0 [' C2 g* Q$ M' ~4 ]
power(x,y)    0 j+ T4 B7 L! A) K* o
 返回值x的y次幂  
2 O+ X) w' Y0 v, j4 vmysql> select pow(2,2);   
" T2 Z: t( i: I$ A6 H  -> 4.000000   
* Z( F6 _. A7 j" z# emysql> select pow(2,-2);   
- {4 m- a% m+ c$ r- Y  -> 0.250000  
' }& j* g$ K4 h0 N+ A0 B
" [* N1 j, s' }/ @+ W2 ]- Csqrt(n)  
3 t+ O. N! ^( ?3 P) W3 ^( S 返回非负数n的平方根  
; U1 C  H/ J2 gmysql> select sqrt(4);    ) d" c7 M5 u' g2 f* I6 }! o" V
  -> 2.000000   
. V; t! [7 ]9 h) x. Hmysql> select sqrt(20);   
9 T8 j! F! B" w, E9 B0 c1 j$ A0 W  -> 4.472136    ) F, h  x; q9 ~- F

; B. Q( L1 w, ~+ b: @2 S  C' Mpi()   
" O! J+ Y% s0 A. T7 I 返回圆周率   1 U/ N# `8 U' J+ L1 I! u
mysql> select pi();   
. @, g: a+ c* s1 a6 \  -> 3.141593   
$ `: P" z5 _& E* X1 |& {1 E ) _( X# h  A* T2 y8 t
cos(n)  $ N- z7 i' \$ V3 b4 j1 W2 l. {
 返回n的余弦值  
3 o: y8 I3 p& w! Q: h$ _mysql> select cos(pi());  
3 A- p" h0 k4 Q  -> -1.000000    ) r- z0 y: l5 V6 P& p4 s5 ]

8 m4 t! L$ d+ _7 dsin(n)  & ~: P' t# q9 l5 y' L7 Y1 _5 g7 v
 返回n的正弦值   5 p* Q2 d: h+ a9 E
mysql> select sin(pi());    0 b6 m1 T3 y! {
  -> 0.000000   
) j8 J# W2 ~1 Z$ \8 `
$ ?4 j. F+ k( d" W4 P& q" Ytan(n)  
& _* @& M- b2 e9 h: B返回n的正切值  / ^+ U& ~2 H( T' j0 g
mysql> select tan(pi()+1);   
7 t/ M4 U  m3 D# u5 t7 n  -> 1.557408   
. E$ |0 {9 D" B1 h 9 Q, P7 U, D8 T& h- T
acos(n)  - P9 V1 U6 n. ~5 [
 返回n反余弦(n是余弦值,在-1到1的范围,否则返回null)  6 g9 p) G" F* v: P
mysql> select acos(1);    8 l. |! A  Q# p' W
  -> 0.000000   
$ o' t: c9 I2 ^! qmysql> select acos(1.0001);   
8 l% o, T( j7 C  g1 x: X  ?  -> null    2 E5 Y( \9 M+ @2 |/ X
mysql> select acos(0);   
+ L$ M( k$ z# l. x7 g. \( o$ ~  -> 1.570796    & U1 x; E6 k0 q5 W! f$ G

9 Q! k' L9 v2 l  F  G6 ~2 Uasin(n)  6 {9 A" _5 g: Q4 y- |2 }$ e
返回n反正弦值  
4 h* K+ N' N3 M4 b9 W( wmysql> select asin(0.2);   
* e9 V" r2 v8 @  -> 0.201358   
9 p- u: e' f% \# n! Emysql> select asin('foo');    1 x' a4 N7 }7 f! ]5 P/ \- ]( G4 T
  -> 0.000000    * Y1 g( a# @: f2 R# j

4 x' g* H/ b/ r& satan(n)  
  t' V  F% V# k% ?- O返回n的反正切值  
, Q9 b; L* L2 V1 x$ q. gmysql> select atan(2);   
$ {7 q! d5 d$ K' _3 J  -> 1.107149    8 o: T; y: \/ i, j% v
mysql> select atan(-2);    ' h8 v# x7 b$ g+ x+ N2 w
  -> -1.107149      S1 R0 K- l4 v2 C, F
atan2(x,y)   
! d4 n* V' L: k" q. W+ P- s 返回2个变量x和y的反正切(类似y/x的反正切,符号决定象限)  % U$ o  m: C( i" b
mysql> select atan(-2,2);   
9 a" j8 v1 p( J" C) Z9 M5 l4 ^7 d/ t4 j  -> -0.785398   
9 Q; l, }: p1 ^mysql> select atan(pi(),0);   
9 ?7 ~, ?2 D: G" G6 U. D  -> 1.570796   
% h' d- [% r0 P+ F8 M& T4 } % ~2 p7 x% }/ p) l' @
cot(n)  - r7 t# q6 ]3 r, ?
返回x的余切  * ~8 W9 x3 a6 t+ U
mysql> select cot(12);    ; n: q) {5 t2 D! r7 j% L+ H
  -> -1.57267341    ! K# N* U0 O5 u  G6 ~& E
mysql> select cot(0);    , L+ `9 _% ~' ]$ @' q* [5 q
  -> null    4 o5 Q' k: o" M& `% a% j% V

% A4 f% ~$ `! X" M( }+ [rand()  
( ~/ a' p0 ?- X, zrand(n)    4 C- \; Q( d/ d9 V3 k6 |! [1 R
返回在范围0到1.0内的随机浮点值(可以使用数字n作为初始值)
+ H' D. m- V; d# W! y# Z
3 t8 o, E" S4 a% p% emysql> select rand();   
; p- {* R" s4 M  -> 0.5925   
0 L% }+ c. P+ i$ w: Wmysql> select rand(20);    * I# C; X  z- n( n
  -> 0.1811   
4 T% y" \7 h* f3 u, N# _0 Wmysql> select rand(20);    . y# r" n0 l0 s& [6 t/ X7 x% _
  -> 0.1811    . a' `. A' t; y# v' g
mysql> select rand();    9 P- {+ |1 h4 o) k
  -> 0.2079   
0 H- \. n& Q5 X4 Y" n5 [mysql> select rand();    3 R7 D; R. s% N
  -> 0.7888   
/ c0 @: C" G2 m  A1 z! m5 Z
# b, N4 E- |3 }, ^degrees(n)  
( g% L1 H+ A) v( n6 v把n从弧度变换为角度并返回  8 S$ Q8 n9 ]- n) V
mysql> select degrees(pi());   
  U+ [7 b, D1 X/ J  -> 180.000000   
, M+ }/ L" b5 @" M3 ^ 5 q# G* m) \0 x' H: d, F, ?
radians(n) ! s/ F" j! D+ M
把n从角度变换为弧度并返回   4 H0 {! N3 I/ s  v2 k
mysql> select radians(90);   
; F' l/ Z* S+ p. |" J  -> 1.570796   
0 T- r( C/ M" h! \. t# }0 ~, Q) p$ R& ~! b* {$ m  R7 m7 j4 \
truncate(n,d)    - s4 T1 S6 k9 }/ s
保留数字n的d位小数并返回  ; I4 `7 i6 e2 q
mysql> select truncate(1.223,1);    * Z4 D# E% ^) W9 F  R
  -> 1.2   
0 u% w- P) \! ?7 g5 u0 t( s! Lmysql> select truncate(1.999,1);    / H7 i3 z' _+ b
  -> 1.9   
- T+ x9 l5 H6 S7 C/ bmysql> select truncate(1.999,0);   
9 H" o  H. T* T  -> 1    " T- C+ R% [- D# n! Y5 R- L
2 ^$ l+ @9 a! y/ t+ H! @3 R
least(x,y,...)    * I& ]* O: \7 h; c+ j
返回最小值(如果返回值被用在整数(实数或大小敏感字串)上下文或所有参数都是整数(实数或大小敏感字串)则他们作为整数(实数或大小敏感字串)比较,否则按忽略大小写的字符串被比较)  
9 l5 g+ M: O: |! fmysql> select least(2,0);    ! W/ n- {. `2 [/ W0 ]3 W9 N
  -> 0    ! @# E8 H5 x2 x4 J) y
mysql> select least(34.0,3.0,5.0,767.0);    4 X2 q' b( [$ T3 D/ P
  -> 3.0   
4 ?" ]$ T' [+ e) bmysql> select least("b","a","c");   
4 P7 s% M+ |0 ^' I8 H2 D7 r  -> "a"   
9 z" r9 d) w% O! n % M9 m7 Z& B' T9 p1 ~
greatest(x,y,...)    + s, F9 y$ Z' r. k- x4 p1 `
返回最大值(其余同least())  
# j2 P! j% g; m$ E( ]mysql> select greatest(2,0);   
/ S$ I0 E! L1 j) G! a' K  -> 2   
& [7 ]' ^9 m' d5 r5 q/ o# k, omysql> select greatest(34.0,3.0,5.0,767.0);    * V4 y& a2 j# z) k7 a3 u
  -> 767.0   
( _; J7 n/ b8 Q. E, fmysql> select greatest("b","a","c");    ; j' `$ W4 F# Y4 z8 a. r
  -> "c"     
- m- w- V& |% w( ?0 A% O/ ]+ N3 c/ @- ^5 r, n) y- A  r- {* z! {6 b5 ]2 n
3、时期时间函数
( _+ Q- B7 s' u4 ^' adayofweek(date)    ' T+ i/ E( L0 k0 {9 \9 H  V
返回日期date是星期几(1=星期天,2=星期一,……7=星期六,odbc标准)  ( Y9 W* F: E& [) {! M5 P% l) D
mysql> select dayofweek('1998-02-03');   
; W) D; }* E$ R0 X" f6 Z& S  -> 3   
# }9 T' U1 q# ]1 v# W
% z/ D! Q3 ~5 B  a+ Dweekday(date)    9 o- S1 K4 Q, i8 a1 s
返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。 7 W' c, v- m+ l4 l6 C
  ( [( }7 _6 w$ L
mysql> select weekday('1997-10-04 22:23:00');    * E. i1 d! q/ L0 w
  -> 5    / O6 r6 P- i& z/ b
mysql> select weekday('1997-11-05');   
# _, t# J8 A' [2 d9 p! w  -> 2    1 q" j6 S. f# v1 p4 y* n/ z8 }
; V  T5 _! q  l" m9 W, u! u
dayofmonth(date)    
# e" t' h9 C" C/ V4 C返回date是一月中的第几日(在1到31范围内)   
: r1 O% z2 b, U8 L! t+ Omysql> select dayofmonth('1998-02-03');    1 c% u' p* L, c0 x! h
  -> 3    2 i. s( _7 ^1 @# X5 l" K

' V4 N  c0 v( N6 p2 s5 a; [2 |dayofyear(date)    + m/ s1 C0 d9 i/ o$ @
返回date是一年中的第几日(在1到366范围内)    & O0 j3 E) j) i0 r
mysql> select dayofyear('1998-02-03');    7 M4 g5 v) r: \& T$ B/ B9 J: I
  -> 34   
" ~( f: w# R/ Q# q4 t8 l% f% m/ V
4 D& \  f9 T! h$ d( tmonth(date)    . \$ ^3 b8 t' @) q
返回date中的月份数值   
7 X7 M3 C, N, k9 g8 [& Bmysql> select month('1998-02-03');   
3 o8 e+ |6 m2 @  r8 D  -> 2   
# C( a* k9 i, y- `
  D! W3 `2 E4 y' A) L9 Mdayname(date)   
( r3 P( j5 X( m* H9 y! L: T6 G返回date是星期几(按英文名返回)  9 `9 ]$ Q' p4 W. N* @1 x& b
mysql> select dayname("1998-02-05");   
8 ?8 L( M& ~' O- O) C: R  -> 'thursday'   
: u  k* w; Q% D# ]
# E& `8 F5 C9 g" W  Z9 a8 j0 tmonthname(date)    
  J0 w  n# G2 U) X' N% X返回date是几月(按英文名返回)  % ]5 ~: R: L9 _
mysql> select monthname("1998-02-05");   
' t+ @2 s) R, B1 c! ?5 ]- v% S  -> 'february'   
6 R4 ?( Q4 b4 c2 j7 ]
0 {' d& y& g( r! Fquarter(date)    3 a' S4 d9 ^4 q; x
返回date是一年的第几个季度   
2 L' x1 d6 l% I( ~% I$ A3 @mysql> select quarter('98-04-01');   
! g" K: N( n9 {( d8 M% d8 J  -> 2   
* D' i3 a1 q  ?" u7 ^
6 \$ j( F  J  ^) i" sweek(date,first)   * w. i2 t4 V* h0 A/ z& H; |
返回date是一年的第几周(first默认值0,first取值1表示周一是% a' ^+ c' R* k3 {) |3 f
周的开始,0从周日开始)  
' Q# `1 S& x+ A& Cmysql> select week('1998-02-20');   
6 `* j4 n- B5 A# d: A  -> 7    5 L* I3 o  R8 Z8 M7 Y0 |
mysql> select week('1998-02-20',0);    6 P6 a4 }- x0 t5 a5 N4 r4 p
  -> 7    ' h9 @: ?2 Z5 G( k' n3 d
mysql> select week('1998-02-20',1);   
4 P! E, m1 t7 k' Z( J  -> 8    ) y* M! W/ Z% Y7 p/ h1 k) o
# `6 C3 E' u3 {: z
year(date)   
" l8 S  z: o% v) m- X返回date的年份(范围在1000到9999)   
+ N6 T. k+ q, y$ a+ ]/ Tmysql> select year('98-02-03');    % [7 {; T# J2 G  |* u
  -> 1998    0 w5 m0 ?1 t6 e0 {
3 `5 m5 ^' y; r6 A
hour(time)    
7 f: T' j4 R6 @, V  u3 J( u4 _返回time的小时数(范围是0到23)   
1 L2 R2 U8 Y9 v3 M: ?mysql> select hour('10:05:03');    . p% f1 \' d( [7 H. l
  -> 10   
6 U& m2 T. k- M; C& K2 o/ E
2 N. V- S1 N6 N5 }1 w& xminute(time)   
& h9 `4 W" O/ f# z1 x6 @返回time的分钟数(范围是0到59)   
) S& m* t7 T9 h% wmysql> select minute('98-02-03 10:05:03');    0 r2 ]. l: J0 {$ f
  -> 5   
- C: q  e! b1 C* f: a 2 c5 P; m1 _3 z5 K) ?6 H: ?
second(time)    
4 Q0 a9 O( X) i  e8 `返回time的秒数(范围是0到59)   
( `4 Q0 p# X" N" e9 n. I$ ]$ Lmysql> select second('10:05:03');    + X; K2 R# ~' B' g7 {6 g  K
  -> 3    8 h9 C" ]* T  F; O6 T

9 t' \- n3 Z; p8 Y% N) E4 qperiod_add(p,n)   
0 t6 I/ j  w7 E增加n个月到时期p并返回(p的格式yymm或yyyymm)   
1 f0 N* G9 D0 t5 ~mysql> select period_add(9801,2);    ; Q% z9 Y* H& _, h+ M2 Q% I0 n
  -> 199803   
+ h+ n) B+ ]1 a
; I4 ~. C( z& A, N8 t: rperiod_diff(p1,p2)   
) U* Q! q. q! z  {返回在时期p1和p2之间月数(p1和p2的格式yymm或yyyymm)  
1 n% p$ s; w+ j* @0 P0 Rmysql> select period_diff(9802,199703);    9 c9 L; D/ x9 G7 w1 x
  -> 11    % {* ?2 x( J# m0 a, }6 L

6 I( h8 N. S1 r# E+ I# v7 |date_add(date,interval expr type)  4 \2 {% h/ @4 ^0 t" x4 X
date_sub(date,interval expr type)   
- `2 x$ v3 g8 c; ~" `; nadddate(date,interval expr type)   
- F; M. Y  w" V8 g2 M3 |& c/ m/ Psubdate(date,interval expr type)  ; C* @$ y/ U6 ?4 x  g% W; T: W+ G
对日期时间进行加减法运算  
) s: J' f1 |. o* W7 J" h/ _(adddate()和subdate()是date_add()和date_sub()的同义词,也; N5 w' n! A8 F
可以用运算符+和-而不是函数  
" ]8 U9 }5 \3 W; K7 E% K4 hdate是一个datetime或date值,expr对date进行加减法的一个表- E4 K' C2 m- A2 y
达式字符串type指明表达式expr应该如何被解释  6 W  i! ~( }9 T8 G" {0 m6 I8 }
 [type值 含义 期望的expr格式]:  3 j( G! O6 B3 x& z2 x
 second 秒 seconds    6 i7 i% {. K9 {; j5 T9 [& I
 minute 分钟 minutes    ) w& x* M; B2 v
 hour 时间 hours   
: Z! n0 e0 Q! s, I3 I day 天 days    . a8 [6 l8 }$ X) y# \/ a' C
 month 月 months   
# A6 O- G' W8 s& P/ h: `* \, W year 年 years   
' F! H  l. V7 P# y' ?* m7 |8 ` minute_second 分钟和秒 "minutes:seconds"    0 I4 @" D/ Q' g% M  B  w
 hour_minute 小时和分钟 "hours:minutes"    ! K' J) b! c8 o
 day_hour 天和小时 "days hours"   
3 H3 N0 ~8 l6 G, A9 y year_month 年和月 "years-months"    # ]5 C6 h% p& N8 ^
 hour_second 小时, 分钟, "hours:minutes:seconds"   
) f0 k- F& C8 f" i5 D$ b day_minute 天, 小时, 分钟 "days hours:minutes"   
* i- }3 l  N' J7 u! `) F2 K$ K# T) W! K day_second 天, 小时, 分钟, 秒 "days& e) T2 b# S: o5 S) Q: I
hours:minutes:seconds" $ Y* I* S" r' d- ?3 E3 q5 l  d% X
 expr中允许任何标点做分隔符,如果所有是date值时结果是一个
8 p9 R" G4 H" f5 ]- `, m4 N" Qdate值,否则结果是一个datetime值)  5 N( q/ q. W) T# V" d# V6 w. t1 ?
 如果type关键词不完整,则mysql从右端取值,day_second因为缺
! ]3 H/ T/ ?( J! g# A0 i1 ~少小时分钟等于minute_second)  / \; M- l( [% F6 j/ H) ^$ R5 ~
 如果增加month、year_month或year,天数大于结果月份的最大天
1 k  D' k! i+ L' D! j数则使用最大天数)   
1 {' G6 P% {: x" ~# Q* Ymysql> select "1997-12-31 23:59:59" + interval 1 second;  * @" Y! N6 B% M% E( B4 i! z

% `% J6 q# [1 z" O$ Z! w, A  -> 1998-01-01 00:00:00    " F6 U/ Q7 C+ v' S! n5 ^3 J8 e
mysql> select interval 1 day + "1997-12-31";    2 G, l1 n* F0 ~4 f- ~) X3 T" ~& }
  -> 1998-01-01    2 w$ K" m9 k8 O$ ~- Y. ^) {! `+ Z0 b0 ^
mysql> select "1998-01-01" - interval 1 second;    % f2 S" N/ m+ Y  {/ h& J: u( x
  -> 1997-12-31 23:59:59   
1 x, T) u( Z9 D5 V% mmysql> select date_add("1997-12-31 23:59:59",interval 18 {# H. a6 w/ \9 e, L: n& D7 [
second);    8 l8 A0 ?0 z$ v+ C4 a$ O
  -> 1998-01-01 00:00:00   
7 l$ G6 X$ a: Pmysql> select date_add("1997-12-31 23:59:59",interval 1
+ G' S5 g+ d" h+ i! S3 Qday);   
+ m/ O+ ]1 `' B; U8 j0 [  -> 1998-01-01 23:59:59   
& m  r6 [/ E' j4 N8 [% P8 r: cmysql> select date_add("1997-12-31 23:59:59",interval+ E! w7 x6 l, c4 b$ z5 T
"1:1" minute_second);   
. ~8 Y* N$ [, [% q  -> 1998-01-01 00:01:00    5 l8 m0 C. k0 s) P1 `
mysql> select date_sub("1998-01-01 00:00:00",interval "1
5 w5 E/ A7 R* M$ A* K3 n% U( q1:1:1" day_second);    ; ^6 C" s' a8 O
  -> 1997-12-30 22:58:59    . _8 j- q0 ]5 {* _+ R5 u2 S
mysql> select date_add("1998-01-01 00:00:00", interval "-1
! a' i4 ]/ `; {% Y6 S3 u10" day_hour);    b8 M; W  J2 b( \8 r% a+ _" e- l# J
  -> 1997-12-30 14:00:00    8 }# y7 @7 Z4 t) ~+ y/ l
mysql> select date_sub("1998-01-02", interval 31 day);   
2 f& W8 Q8 s! `& p  -> 1997-12-02   
: l' q% ]( ]6 c# {$ Mmysql> select extract(year from "1999-07-02");    ; x& `4 T; v8 B! y) ?0 q1 N
  -> 1999   
9 j8 g' [% s# K: z( p6 Y) ^mysql> select extract(year_month from "1999-07-02& Z1 b: @4 u& y8 n$ \6 j
01:02:03");    # q/ W/ S- M0 T# P- Q: a
  -> 199907   
- _2 E/ Y8 j% M- [7 v3 G( i! O8 I6 Dmysql> select extract(day_minute from "1999-07-02, @. D! ~/ z; {7 I2 t5 @7 P
01:02:03");   
4 N8 y7 R- C, w7 e1 {; P2 X" _4 x  -> 20102   
8 D" s% q1 K- R4 k+ y3 A, m% B  { # e0 _, O. `: a- ?! s) K
to_days(date)   
- ~' n4 O+ A( {$ d& `: T9 R) w8 y返回日期date是西元0年至今多少天(不计算1582年以前)  
3 R- e$ T+ o2 F/ C8 hmysql> select to_days(950501);   
' p7 }! ]& Q$ A6 h" p# n! Y  -> 728779   
9 @  m( |# Q! s4 hmysql> select to_days('1997-10-07');   
: F$ p0 v2 U& G! l3 c  -> 729669    0 g; s1 W8 L, a/ y8 ~9 U
" ?9 p1 x6 h. M% e) k
from_days(n)   
* J5 T5 r5 ~' z& l" { 给出西元0年至今多少天返回date值(不计算1582年以前)   
- R+ O' H: G4 `mysql> select from_days(729669);   
/ U' I. R4 p. J: ]  -> '1997-10-07'      E) m) G& H1 E, t; G2 V

7 L+ i, O" C( \2 h2 H2 K, y" h/ gdate_format(date,format)   
: i) f8 \# x4 C  Y0 p 根据format字符串格式化date值  $ G& p& z, T* ?% f
 (在format字符串中可用标志符:  
# K7 ^, P  \) Y: U  t %m 月名字(january……december)    5 Z. \5 Q& @: Z$ l  S* [6 r7 q1 D# t
 %w 星期名字(sunday……saturday)    % k: m, `0 t' j9 X, h
 %d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)   
, l" K' I& h- Z- u$ w$ ?( J$ G %y 年, 数字, 4 位   
6 E) p! s. z* G7 Y& Z/ d3 K %y 年, 数字, 2 位   
$ K) Z# d& d# c0 _! ` %a 缩写的星期名字(sun……sat)   
: [8 }# o' y, c %d 月份中的天数, 数字(00……31)   
  X. m/ o) v* q# w %e 月份中的天数, 数字(0……31)   
, t( B7 T: e$ ~! L/ @( \ %m 月, 数字(01……12)    + J6 W4 c: V# U# l
 %c 月, 数字(1……12)   
5 Q% e& y: S: D9 q %b 缩写的月份名字(jan……dec)   
: d8 H& w( F$ V2 q0 e' k, P %j 一年中的天数(001……366)   
/ h# c5 r# r' w, ^ %h 小时(00……23)   
* D5 X8 d) B$ X5 T) |' u* | %k 小时(0……23)    1 Q' I' z2 y$ p/ ^
 %h 小时(01……12)    - U, H2 E  y6 |8 o1 N7 q  M
 %i 小时(01……12)   
2 c8 O0 v$ P' o- K8 f %l 小时(1……12)    8 w* `! g/ j, j8 ~/ l6 t
 %i 分钟, 数字(00……59)    % `, j6 `+ {  D, i5 F9 q1 t" |
 %r 时间,12 小时(hh:mm:ss [ap]m)    & q% F% X( L# I: U/ t$ T2 r
 %t 时间,24 小时(hh:mm:ss)   
& ~; T2 E' H: b3 B9 z+ E3 ` %s 秒(00……59)    4 {4 a/ p3 {5 }& V* C
 %s 秒(00……59)   
& I4 B7 Y; Z0 q: m' w7 ?, D3 o %p am或pm   
/ O4 N$ k4 L. r2 F: e# }3 j %w 一个星期中的天数(0=sunday ……6=saturday )    2 K$ M7 O9 p1 l* Z
 %u 星期(0……52), 这里星期天是星期的第一天   
& g! j$ l7 n$ i/ p8 j! j %u 星期(0……52), 这里星期一是星期的第一天    + q  q' D+ D! ]+ Q8 I
 %% 字符% )  + K, }2 c  F2 _- e
mysql> select date_format('1997-10-04 22:23:00','%w %m %; i- }: G8 n+ x4 }/ R% H& Q
y');    - C2 J3 v7 U5 [! Y
  -> 'saturday october 1997'   
/ e% Q% b5 w% q; j1 \# dmysql> select date_format('1997-10-04 22:23:00','%h:%i:%
  Q6 f5 q. X( }+ P( r! ms');   
3 B% _8 T! L6 g0 t$ U1 _  -> '22:23:00'    " t: u! d9 ~4 O( r) j% U7 j
mysql> select date_format('1997-10-04 22:23:00','%d %y %a3 G# `" H6 h% G
%d %m %b %j');   
' A2 R7 w& h) H  -> '4th 97 sat 04 10 oct 277'    ( [1 V/ B6 e* v$ \2 ~; ^
mysql> select date_format('1997-10-04 22:23:00','%h %k %i
+ O! k6 ?& S( v- L%r %t %s %w');    0 n5 Q6 k* Q+ s& o0 c
  -> '22 22 10 10:23:00 pm 22:23:00 00 6'   
+ v7 c1 k! p9 K) r/ `/ W( F ) J$ B0 V2 ~; i: Q
time_format(time,format)  
; Z% D2 {# |$ x- K) g5 g 和date_format()类似,但time_format只处理小时、分钟和秒(其
' }9 s- M: F# v4 D7 n% d! n余符号产生一个null值或0)  
, `) F; Y6 X/ U* g" g , A' ^" W2 @" @. i
curdate()     " m( I% G6 d  `/ C# ]
current_date()  ; W% b" ~0 w% f2 f2 [
 以'yyyy-mm-dd'或yyyymmdd格式返回当前日期值(根据返回值所7 L: {$ m( [/ ]1 q1 M6 \7 G% \
处上下文是字符串或数字)    ! t2 j3 r7 a7 d. r9 J2 x2 O+ X- E
mysql> select curdate();   
/ ?* m: c, d( v0 s1 |" [% P1 _  -> '1997-12-15'    & N  m6 q* M% T
mysql> select curdate() + 0;    , A7 I- u! F! d$ u- g1 R# y
  -> 19971215    , Q  k% W3 R- E2 z1 Z; d" Y
9 S6 |! F) o- m0 A! o/ F( \) O
curtime()   
7 R0 |2 D( u& J' @9 ?/ ]& @8 Zcurrent_time()  
" H7 A! k9 s. c6 e  j7 w/ n" K 以'hh:mm:ss'或hhmmss格式返回当前时间值(根据返回值所处上
" w+ c/ G  ~! I6 p下文是字符串或数字)      4 h5 z; t4 m5 e- w$ w( y! Q
mysql> select curtime();   
3 ~* K  V3 e! _' g  -> '23:50:26'    1 x/ O- T  ^* I# t
mysql> select curtime() + 0;   
9 A; ?0 C4 q1 `  -> 235026    6 b- J5 j- z# b3 Y$ a/ E8 s

  n7 K; C" z% [* C. z9 anow()    
; B0 V0 q" b- }  Fsysdate()    3 w  q8 H( L: G* w! {: e
current_timestamp()  
' M& C, a4 c1 |& |: T1 t5 y; G 以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回当前日期
1 |* X1 ]7 Z, s/ p1 |时间(根据返回值所处上下文是字符串或数字)     : ?- V  b' Y; Y# ?
mysql> select now();   
  _6 o- t" R3 [, X* X6 M6 _3 [  -> '1997-12-15 23:50:26'    / f( c1 T) ?! {* E% ]8 \  E
mysql> select now() + 0;    $ c6 x& E; b( Q
  -> 19971215235026    1 N: j& K% X$ z- D9 K  \

; E9 i, \  H* z+ C* \unix_timestamp()    , E. W: s  j$ E
unix_timestamp(date)   
$ ]. P9 v6 ~# ~% p: z返回一个unix时间戳(从'1970-01-01 00:00:00'gmt开始的秒% U: P" ]  t5 d" k/ f( J5 p* x  g
数,date默认值为当前时间)  
. l0 q& O  ~6 i8 m; G! s& V7 [2 Cmysql> select unix_timestamp();    ( O/ m1 j9 A  ^% e- O0 u1 c# @' g
  -> 882226357    + [+ ?7 F! G7 v/ x. }2 k( o+ M) l
mysql> select unix_timestamp('1997-10-04 22:23:00');    ; m  B4 e! _. `/ E) a& H
  -> 875996580   
6 P0 [: r) }3 N& c; s: ~ : }  O2 c  f  r+ S, W
from_unixtime(unix_timestamp)   
* ^/ v, ]7 z. R7 X& U8 g以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回时间戳的# q! p" f+ b2 j+ h
值(根据返回值所处上下文是字符串或数字)     
3 F5 \( v( S* }( F' p9 Rmysql> select from_unixtime(875996580);    1 R4 _8 `# A9 n' ~8 ^5 z
  -> '1997-10-04 22:23:00'   
$ {! U: T: |, L" Lmysql> select from_unixtime(875996580) + 0;    1 Y; C6 `/ Y9 ^9 Q7 b% q1 u
  -> 19971004222300   
4 w' p1 K2 g5 Y6 ^
8 V- [1 _; a$ w4 S" Dfrom_unixtime(unix_timestamp,format)    
) E- c7 m1 N# Z以format字符串格式返回时间戳的值  
; f* [, e( ^( X( U  F7 ?; ]mysql> select from_unixtime(unix_timestamp(),'%y %d %m %5 Z8 C; N3 i& o3 n  y
h:%i:%s %x');    0 Y7 @* a- }# b. K5 q
  -> '1997 23rd december 03:43:30 x'   
% d. G5 `4 Q# h- Z8 Q / I8 `# P2 k3 v0 e6 Z2 c
sec_to_time(seconds)   
/ b+ K* Y( E) r; u! b! B* _以'hh:mm:ss'或hhmmss格式返回秒数转成的time值(根据返回值所处上下文是字符串或数字)     $ E* v( e3 N2 ?4 m# x
mysql> select sec_to_time(2378);   
( y4 C  i( a, F) f7 _  -> '00:39:38'   
8 x2 V/ q, T7 \9 S3 Z. y& `mysql> select sec_to_time(2378) + 0;   
2 c* l) J6 c- s, }  -> 3938    ; }6 _8 a! u1 H% v, S# L

/ k1 Z) \. b+ N$ L" N. jtime_to_sec(time)    $ e- x+ X5 @9 E
返回time值有多少秒   
; v& ~* C  ~# Mmysql> select time_to_sec('22:23:00');   
0 [$ K# y! ~4 c. f# a  -> 80580    : C# \  b/ x2 C' }+ m
mysql> select time_to_sec('00:39:38');    ! y3 k4 P- F$ [0 N% f4 s. w2 d
  -> 2378
; z* v7 g7 G& Q0 D ' u1 Q# O. @0 a7 [6 J
转换函数
" u8 X) G$ w: |2 V# r/ ocast
% P8 g6 K9 M2 t8 ~. r7 J用法:cast(字段 as 数据类型) [当然是否可以成功转换,还要看数据类型强制转化时注意的问题]( @, m% B* i+ h4 J
实例:select cast(a as unsigned) as b from cardserver where order by b desc;9 F& e0 g. I- W- ~# t
convert:* D) y1 D: i" {- M
用法:convert(字段,数据类型)
, E/ J6 Y" h+ `) S- Z实例:select convert(a ,unsigned) as b from cardserver where order by b desc;

5 d, L" q2 x5 W( O: N" |* _- X8 q# X/ K
回复

使用道具 举报

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

本版积分规则

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