|
1、字符串函数
- d9 b) w# ]: P4 K' E+ gascii(str)
6 H6 @6 V) F1 K0 [返回字符串str的第一个字符的ascii值(str是空串时返回0) 3 j- J- E0 A7 B3 d
mysql> select ascii('2'); 8 Z0 F6 T* e5 L4 \$ z1 R. U$ [
-> 50 4 S! I$ \! x9 Q
mysql> select ascii(2);
* W% h$ |( Y9 Z6 n -> 50 4 v: ^. m+ I5 o$ T0 ^
mysql> select ascii('dete');
9 \' j4 Q9 {! V7 K -> 100 % q/ {. q3 z8 [6 h" S: F/ u# P& f
ord(str) . I( V9 M5 H# {7 [7 O2 U
如果字符串str句首是单字节返回与ascii()函数返回的相同值。
" C( X: u" \& O( H$ W# e9 h 7 S9 V! T( \# i
如果是一个多字节字符,以格式返回((first byte ascii code)*256+(second byte ascii code))[*256+third byte asciicode...] . F( M T, W! N; N, F3 e
mysql> select ord('2'); * o% A6 z8 H# l) t3 ~) U0 I
-> 50
3 _9 ]( Q( |. d* ]; ^( Q+ ?
- P% T% V/ z9 j% U& e% R7 [conv(n,from_base,to_base)
( X5 D8 Y9 ?$ |6 H0 k% @1 n, `4 A对数字n进制转换,并转换为字串返回(任何参数为null时返回null,进制范围为2-36进制,当to_base是负数时n作为有符号数否则作无符号数,conv以64位点精度工作)
( t% T }. a8 k' S1 B* ? C6 @mysql> select conv("a",16,2); 7 {$ ^& e5 t. ?% n" \
-> '1010'
! t8 w# j& C) k* c7 e2 `/ Amysql> select conv("6e",18,8);
: m% ^& O. A2 m- o' o! H( B) ^ -> '172'
4 E4 s" R& h5 @- u B' amysql> select conv(-17,10,-18); + O9 ]) k/ a1 m4 f2 J, x. g
-> '-h'
9 l4 g; J$ [# z) @# {) f1 imysql> select conv(10+"10"+'10'+0xa,10,10);
O1 C$ y" g4 A1 Q -> '40'
+ V b3 H: k1 \ Z, B8 Y6 z" c
" n f* ^9 v! _bin(n) $ m$ n' L) t2 a7 s; D
把n转为二进制值并以字串返回(n是bigint数字,等价于conv(n,10,2)) : _5 u% M4 g5 o7 f4 |. j ~
mysql> select bin(12); , {4 o$ U1 T& |8 j0 K
-> '1100'
7 l2 m, R5 g" q# p/ _2 S% w! h4 \; ~
5 @7 p" q! J& `' j# noct(n) : F2 D+ _ y! j
把n转为八进制值并以字串返回(n是bigint数字,等价于conv(n,10,8))
# k( d8 u: u. }, I* Hmysql> select oct(12);
u0 n j8 n2 W4 ^/ \/ ^ -> '14'
5 }6 D/ H9 `' l
# f3 z9 `! C P- lhex(n) ' F. S9 s8 t- L+ l) D6 T
把n转为十六进制并以字串返回(n是bigint数字,等价于conv(n,10,16)) $ |/ F+ f1 o6 w) ?2 v1 G
mysql> select hex(255);
+ d0 |9 Q1 g- T! S4 m U; @3 K -> 'ff' / H) ~& N6 i' P8 p
( r# X1 {, o6 v+ t1 K9 x6 C# jchar(n,...)
! P/ T9 P) F. ?. }4 _ C$ E! J+ S返回由参数n,...对应的ascii代码字符组成的一个字串(参数是n,...是数字序列,null值被跳过) , H" b6 j$ g6 H. E; h
mysql> select char(77,121,83,81,'76'); ) I/ d" o8 S& b; h, a
-> 'mysql' " P8 J- h+ l% D- B
mysql> select char(77,77.3,'77.3'); + M# o9 }' Z) y. {- f& c1 p
-> 'mmm'
& K1 j' |: M) h: Y: h) m( b 4 K: B& i) u8 S' i3 S
concat(str1,str2,...) 0 [ ~% m4 E" k* v
把参数连成一个长字符串并返回(任何参数是null时返回null) 7 Z( y0 P3 _/ {) O) k! T
mysql> select concat('my', 's', 'ql'); 7 i, k6 L9 h* s" W) B
-> 'mysql' / }; i$ ~, I6 E; X
mysql> select concat('my', null, 'ql'); 2 d0 z5 a* `/ h$ X
-> null
8 R9 c6 ?" a1 L. m: o6 x# zmysql> select concat(14.3); ' l, P+ L* K9 k+ o/ W" |
-> '14.3'
' K6 }( q( W( D% i$ a& k
# T( {+ f9 @9 Q# [) Wlength(str)
; `1 P' c! A6 c# o( ?octet_length(str)
8 Q4 c: W0 s8 L- s* z# m. Cchar_length(str)
2 E/ m$ b+ d/ S7 i( Jcharacter_length(str) ; y) c* n6 F9 S' d/ N9 s6 n5 U) u7 O5 k
返回字符串str的长度(对于多字节字符char_length仅计算一次)/ Y, r" ~) }" X+ m. H
mysql> select length('text');
0 [1 F) U; I1 V% C7 @ y -> 4 % o# _0 x3 m6 n
mysql> select octet_length('text'); [; W0 S- n, b- m3 I
-> 4
l9 G) U+ I) x! X1 I! q# S( s
7 b: h% [+ t- F1 [& V/ h+ _' \locate(substr,str)
2 n, A* H& n4 F( W) p1 y5 o* t$ j2 _, Xposition(substr in str)
% L7 O. I" B9 h+ m% V返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)
; _- G$ s3 u3 c0 gmysql> select locate('bar', 'foobarbar'); ( m! V" g! e2 `( f) c% f
-> 4 X2 V# Z# y1 H( P9 I
mysql> select locate('xbar', 'foobar');
7 u. T0 E, d$ Z2 S$ _ -> 0
4 \) o4 n( M( X- n# B
+ L) t" G; R3 B; [; b8 ylocate(substr,str,pos) 8 F+ F d6 ~# B0 b0 R3 x
返回字符串substr在字符串str的第pos个位置起第一次出现的位置(str不包含substr时返回0)
; [) I8 ` y H: k5 k0 u* umysql> select locate('bar', 'foobarbar',5); ' M$ T1 a: }7 c+ I# u
-> 7
( l4 ]% e6 w/ H# I7 R& d; Z6 Y : Q& a' L+ m' p* @# A7 }5 ?# J% P
instr(str,substr)
% @+ J7 Z M2 M Z% U0 z, [返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0) # ~! c2 |& M. ]0 ]6 t% b1 ~+ ^
mysql> select instr('foobarbar', 'bar');
1 [* J- \, `4 z2 y, B6 u* P -> 4
/ w* r; P1 O* ?2 Mmysql> select instr('xbar', 'foobar'); 0 Q9 I- l$ w" l& j' F: V: K) m
-> 0 9 e7 W5 {1 Y& L' p+ w" _9 y, {6 h7 u
; N/ j, K: `3 h& ?7 @! d! j; ]- n8 y
lpad(str,len,padstr)
9 g- ?$ j) |* |; p8 i用字符串padstr填补str左端直到字串长度为len并返回 0 \, ^0 U* q' ?
mysql> select lpad('hi',4,'??'); + X ?7 `1 l+ f: D9 g' I
-> '??hi'
2 N6 F3 r2 n! Y 4 l! K' k% `6 C3 m" V
rpad(str,len,padstr)
5 k) |" ~) T1 y$ a& k; ~' l用字符串padstr填补str右端直到字串长度为len并返回 , N9 }. s3 a. M8 @2 E1 M9 b; ~
mysql> select rpad('hi',5,'?');
0 o; z7 y5 o8 Z5 r -> 'hi???' : }! ]& k4 h8 N* s
- ~% b! [7 s" B; Wleft(str,len) $ p* s; r- {1 d8 L
返回字符串str的左端len个字符
5 F% p: A4 a; L2 I, ?& Wmysql> select left('foobarbar', 5);
# d# V x* g% w/ p e -> 'fooba' % X, A6 R3 {- R/ z8 J
, E: e0 B/ P$ qright(str,len) # s. p/ L8 i) i U; L2 d0 W' u! ~
返回字符串str的右端len个字符 1 p8 k. X! W* h/ i. @6 {
mysql> select right('foobarbar', 4);
X `& |8 i+ L; v' b -> 'rbar'
/ f; M- {: B) Y. j9 o $ ]! m) z8 I! t. O$ x% \; W
substring(str,pos,len) ( S0 P" T* h* }7 }7 l
substring(str from pos for len)
/ z4 ^! P& G% }7 _) S" Imid(str,pos,len)
% U* B s' P: v) q b0 \返回字符串str的位置pos起len个字符mysql> select substring('quadratically',5,6); : m1 t0 f, x: y1 d
-> 'ratica' . s& }4 }" N6 `! v S7 |% o) w
0 \7 C) m" ]! Y4 L- ysubstring(str,pos)
: V7 Q3 u: Y9 ~" \3 ^7 Xsubstring(str from pos) k0 ^' g0 J5 P4 Z$ n
返回字符串str的位置pos起的一个子串
5 e2 Z1 K( l U3 [, e7 z7 l6 ^8 emysql> select substring('quadratically',5);
7 y$ v% V$ t- U* J+ f -> 'ratically'
5 g* Y1 l! B. ?3 d' k6 o" W8 {mysql> select substring('foobarbar' from 4); 6 d4 ^( U+ o) X1 G/ ?6 @& A" ~0 p
-> 'barbar' ' a! m0 {0 n2 b$ z9 ^+ {6 L
8 l: E2 R$ L% J2 d. n; vsubstring_index(str,delim,count)
' g( u/ m9 v; m8 V% |返回从字符串str的第count个出现的分隔符delim之后的子串5 K0 \0 {" T+ ^5 W/ T! m" T" }( S
(count为正数时返回左端,否则返回右端子串) $ @7 A0 P9 t% q5 l u
mysql> select substring_index('www.mysql.com', '.', 2); ; L L# w6 s' I
-> 'www.mysql' # e' E' `+ \- H! [0 H. b- D
mysql> select substring_index('www.mysql.com', '.', -2); 7 `: A- J3 }, ? G) X% ~# k, E+ j: C
-> 'mysql.com'
0 k: `1 Y F8 s- s! A0 ~, A+ y
% h6 j" i1 B* h* j p3 _/ vltrim(str) 0 t/ P$ p8 I- X# p* l. Q+ p- ?. R
返回删除了左空格的字符串str
" h5 g% k8 l% L& d( f0 L% Bmysql> select ltrim(' barbar');
3 v9 | S$ O! n -> 'barbar' 9 \! f" U k5 [3 k3 g
; y2 D4 T3 b. ortrim(str)
' `) H2 j7 F6 a2 I# U; S- i返回删除了右空格的字符串str
3 i" B' H8 x' `5 [- Y7 Lmysql> select rtrim('barbar ');
( l& U, A0 }9 b3 L% C -> 'barbar' 6 o8 P) X& K8 T Y; A
" _: C8 T9 [8 ^0 R9 n6 M
trim([[both | leading | trailing] [remstr] from] str)
+ `* z+ k6 Y6 O) b+ t返回前缀或后缀remstr被删除了的字符串str(位置参数默认both,remstr默认值为空格) 0 w- ]$ g$ b7 a4 a6 S% O
mysql> select trim(' bar ');
8 b; [+ S7 ?* f, L -> 'bar'
4 K a8 q7 L8 D w/ E" Imysql> select trim(leading 'x' from 'xxxbarxxx');
. o2 T+ d) x: N/ q4 h7 s -> 'barxxx'
; m: c: u- P; ~5 w& Emysql> select trim(both 'x' from 'xxxbarxxx');
+ ?1 F$ S' q8 Y4 |. x9 n0 [/ M6 Y; B* } -> 'bar' & s \0 J0 X/ u0 D
mysql> select trim(trailing 'xyz' from 'barxxyz'); % y) D; E8 Y" j
-> 'barx'
1 p+ p! G: _" w1 m& D
$ J! K0 \0 t8 w/ j* W# Msoundex(str)
$ z6 C+ T& t, U" \返回str的一个同音字符串(听起来“大致相同”字符串有相同的8 Z, a+ d' B' m( ~( u. u( y
同音字符串,非数字字母字符被忽略,在a-z外的字母被当作元音)
6 U8 ?# x; ~& d* D- C6 Kmysql> select soundex('hello');
4 y9 ], Y- ~) w/ \8 k( o -> 'h400'
# |% J" B( R! v! Z3 jmysql> select soundex('quadratically'); ' Z, y# N7 @( x* @1 e* o
-> 'q36324' 9 X" [* `! b9 r) z+ v7 K" h, E, L2 e3 C
( v7 d# u# P% \" ]% {* j Ispace(n) 9 {. }- G5 P' n7 l( j( h" V4 F
返回由n个空格字符组成的一个字符串
+ I: ~- O. }& a9 a9 [1 gmysql> select space(6); Y/ P4 F# @3 h
-> ' ' ( ^, [4 Z# H" J: p! e
. Y* ?! z* C, V# nreplace(str,from_str,to_str)
i! X) t+ Z# A h; Z, ?, ?! Y用字符串to_str替换字符串str中的子串from_str并返回 - O) D8 M" \2 ~3 o# u/ l: b
mysql> select replace('www.mysql.com', 'w', 'ww');
. a C1 o% e- r: }/ Q+ d* Z6 W8 R -> 'wwwwww.mysql.com' e' M4 W8 Z$ r+ k& ?* i- h; o
! V. C4 N, v0 ]! o# j3 H! @1 Brepeat(str,count)
8 u9 n& O! O# f2 a. o9 l! t返回由count个字符串str连成的一个字符串(任何参数为null时, U1 l; @6 p0 |4 a. r
返回null,count<=0时返回一个空字符串) ! ~+ ^+ V. X/ S& G
mysql> select repeat('mysql', 3);
# }* e+ w/ T& W# w -> 'mysqlmysqlmysql' , G. t* _7 P* | O' W
' h9 n+ k" `# s- u7 q0 ereverse(str) 5 p# [, y: H0 p( O; F3 J+ J3 l# ?
颠倒字符串str的字符顺序并返回
$ e/ K2 ]: s |* Amysql> select reverse('abc');
7 U" B7 R9 r" t% `6 L -> 'cba'
' _! I: N% b! P' V9 k 9 W# D; F2 A3 [# E5 c$ ]+ H
insert(str,pos,len,newstr) ! p/ I9 G6 J$ g# Z" P; W
把字符串str由位置pos起len个字符长的子串替换为字符串 `3 x: k8 X) _* M b5 h# U
newstr并返回 " m) g& l: m, e5 r ^
mysql> select insert('quadratic', 3, 4, 'what'); ' f! p' E. ^# p0 ^7 O; Z( ^
-> 'quwhattic' : B- G3 |8 R8 B/ X. ~! W, j, k6 R) o
2 O7 D$ J: c5 ^( ?9 z( Aelt(n,str1,str2,str3,...)
" j* \5 w$ W% x" _2 | {7 f G! j {1 p返回第n个字符串(n小于1或大于参数个数返回null) ( V# I5 W9 `9 ~( i, ? C
mysql> select elt(1, 'ej', 'heja', 'hej', 'foo');
. t; L) Y% ?. w -> 'ej' $ o. Q5 f( I: |. W& {2 v# q T* d
mysql> select elt(4, 'ej', 'heja', 'hej', 'foo');
% @: D2 q+ g; ?* y -> 'foo' - F3 C6 T1 R+ }; s; X* j
7 i( R. r0 ]: u7 b. g: ?1 F$ A- d Rfield(str,str1,str2,str3,...)
) R9 A. |, H9 m5 S s1 g1 |返回str等于其后的第n个字符串的序号(如果str没找到返回0) : k: c, r/ T. L6 h
mysql> select field('ej', 'hej', 'ej', 'heja', 'hej',! F& F& C# i+ l& Q
'foo'); / O! V+ k9 ^7 T
-> 2
/ n- A6 | X5 p7 z- omysql> select field('fo', 'hej', 'ej', 'heja', 'hej',1 a+ O0 O% h8 l8 _& U5 b
'foo'); 9 |, B9 v9 z& W
-> 0
( S. F' N$ ]) ~0 y% |
' p/ p3 F: D( h! b# l, xfind_in_set(str,strlist) $ Q9 D- `# ~$ n6 Z r
返回str在字符串集strlist中的序号(任何参数是null则返回
8 Q8 m' W. |% N0 x" hnull,如果str没找到返回0,参数1包含","时工作异常)
2 d T6 E; M% e7 N# smysql> select find_in_set('b','a,b,c,d'); : m2 V$ P K' a0 j4 {$ m+ t) W4 n
-> 2
, U. e* x7 J8 N4 J# y , Y9 s# q1 ~7 y
make_set(bits,str1,str2,...)
; ~* j7 ^$ b% M1 y R+ \8 f把参数1的数字转为二进制,假如某个位置的二进制位等于1,对应- I: p* X" w, ^, ^
位置的字串选入字串集并返回(null串不添加到结果中)
. D' X, S. f& Y% }% nmysql> select make_set(1,'a','b','c');
& d4 r ~3 O0 S# z( C4 f# v( J -> 'a' 0 U9 g! o+ n4 Y8 W$ H a4 d& G/ w2 h
mysql> select make_set(1 | 4,'hello','nice','world');
5 W* e( K5 h' G0 P9 B -> 'hello,world'
& [' e; v; b" d# n1 `1 imysql> select make_set(0,'a','b','c'); * V# Z/ [: P& s5 n' p* h
-> '' , H/ V0 S6 H- K4 z
' {/ p3 b9 s# _! Y# [7 ~: }
export_set(bits,on,off,[separator,[number_of_bits]]) ' E2 `7 \$ U F# \! r" }. k1 F5 U
按bits排列字符串集,只有当位等于1时插入字串on,否则插入/ g4 [3 j! f8 q$ n$ q8 V
off(separator默认值",",number_of_bits参数使用时长度不足补0/ A c9 L. k+ x
而过长截断)
7 \$ X- D3 h) I; o. n- z3 C! Umysql> select export_set(5,'y','n',',',4) 4 g% P2 C! a- t
-> y,n,y,n
' f5 {; m, o" G( @ f0 D
- y, ?% S, l, ]lcase(str)
& d( a7 z Y9 g8 o$ C8 klower(str) " q$ o- d+ U1 j
返回小写的字符串str
) m8 ^* N c: [mysql> select lcase('quadratically');
) [- b5 H/ ]8 f% U+ t -> 'quadratically'
5 _9 t6 L: s& @ . w( ?5 ~7 j; e9 q! r/ r+ @5 d
ucase(str) 6 Y# w v& T/ A
upper(str) " Q+ e. K$ F! U+ U" n
返回大写的字符串str
& ?( p7 c4 B; K, V6 e4 _; imysql> select ucase('quadratically'); : Z% H; g8 s& F! [( g
-> 'quadratically' - S" J8 M" h, h1 q0 K
; x! Q0 R2 w+ t; q7 ^load_file(file_name) " p" E% H* ]3 B" u8 F& K' z
读入文件并且作为一个字符串返回文件内容(文件无法找到,路径& e5 i" u. w+ S9 x' m" ]6 J# h
不完整,没有权限,长度大于max_allowed_packet会返回null) + y9 f$ |* ]8 m$ r9 Z* g R
mysql> update table_name set blob_column=load_file' Q: r) e# v* x5 b& m
("/tmp/picture") where id=1;
% ^7 K: ^3 n: i( o . d. n% [, f. F/ t
2、数学函数, b6 [1 ^5 U! @ X: M" K1 s+ C
abs(n)
+ s' ~7 H& i" k7 [% t' \/ E3 y返回n的绝对值
2 L- R( C, y+ O( Z& ymysql> select abs(2);
9 R$ Z! B3 C; Q$ o6 G -> 2
; |2 ^' A+ h" Y; [, Bmysql> select abs(-32);
, ]% d; U0 h' d -> 32
/ d. A: W( Q% G - P' Z! k9 F$ V* H+ O4 F
sign(n)
( G6 ^2 |; |9 g( W返回参数的符号(为-1、0或1) % L$ P7 o! F$ V; J+ i3 m: C
mysql> select sign(-32);
) K5 E3 h6 e h0 Z -> -1
" ^5 e, j" U' v( u+ xmysql> select sign(0);
# p" }: F7 ]( @7 g -> 0
" {4 L3 T- K4 }* I4 Vmysql> select sign(234); 8 V+ l* ^5 E+ P* F2 G: K! O9 ]
-> 1 ; }8 e% F {0 f' C, C6 |* q- `! Y
3 b( q; z# @& y" l [4 I5 C
mod(n,m) 7 Z7 x8 `& w# _! _
取模运算,返回n被m除的余数(同%操作符) $ r9 h7 c2 i9 [- l- |
mysql> select mod(234, 10); , O+ B, f. p4 }8 k1 Q; ~ {
-> 4 / A& P7 M7 I% |
mysql> select 234 % 10; 0 B8 u D; M- ~% U& K
-> 4 " V! w/ c2 @& Y
mysql> select mod(29,9);
0 x7 T5 q4 K3 x5 R5 | -> 2 ) M1 i9 W v0 }7 X! a- t
7 F" c. Q* |2 v) c7 d- B; Jfloor(n) 1 e" C, C/ r C
返回不大于n的最大整数值 , L: f9 Y* t( t# Y: K
mysql> select floor(1.23); + D( R6 _8 I# J( q0 v
-> 1
+ z3 m; r$ ]+ n; F5 w2 X1 q+ jmysql> select floor(-1.23); ( R ^% N5 D' O+ g/ j3 l
-> -2 ! o% e1 m7 r, O8 t- _1 A$ T) b' M
( F6 U9 X2 o9 Y1 E
ceiling(n)
4 }* h9 X) q* m. M, O返回不小于n的最小整数值
8 t5 ~5 b: S6 G6 ]$ S6 M5 n+ dmysql> select ceiling(1.23); 4 J# t! E3 [* c7 O; R) q6 B
-> 2 9 k/ F) m( }, L! e* d+ b
mysql> select ceiling(-1.23); $ p! `* H! K3 J
-> -1 , A. g) O/ o& W, U3 ~
' X$ ]8 _+ e& A
round(n,d) - e- f7 j: A7 S j5 J
返回n的四舍五入值,保留d位小数(d的默认值为0)
5 o: P8 P9 e5 G& a5 z2 g6 xmysql> select round(-1.23);
* i i9 J. F! p6 P1 _ -> -1 # O8 i1 l4 w) y
mysql> select round(-1.58);
2 O+ O& A7 f/ S+ X -> -2
# a0 s7 w' p1 _( r$ J' c! Pmysql> select round(1.58); 0 r u! b: l5 J6 k* I; F
-> 2
6 y8 ?4 T/ ]! j% rmysql> select round(1.298, 1); % d4 {1 Q- k( q T1 C
-> 1.3 l+ q/ ^! i, Q p6 w; S
mysql> select round(1.298, 0);
! Q% `1 p8 m+ \& y# [$ h2 e -> 1
$ E- o+ Y4 ^% T+ I& a. p4 X, Q + T1 j3 Z% d8 f: K, j. n0 n @
exp(n) 6 U' |, a& F: a! f1 y1 ` [7 D8 U
返回值e的n次方(自然对数的底) ) b6 z; B2 t, r+ g& J
mysql> select exp(2); 8 Z d5 M. ~* H
-> 7.389056
0 ^" y) S H% p. n3 L1 j% `6 Fmysql> select exp(-2);
# h L9 g! p: R, V5 O: O9 P; | M -> 0.135335
' c( ]0 t6 `/ d$ b* F
/ O8 i' t; m/ Dlog(n)
$ f( O! m9 ~# ]7 J5 B返回n的自然对数
& q* R# i! i$ ? Y' rmysql> select log(2); * W: u" B% i3 S' q- I
-> 0.693147
' q5 w: A6 \# V7 u7 ^+ |mysql> select log(-2);
4 S; W6 I2 j) B2 j) d K- S -> null
) _* y$ V! f8 F5 s5 I0 l, i- M* U + M8 e/ G1 R9 W3 C: G E
log10(n)
3 V0 ~7 D+ \& Q4 B" U' O& P! K- V; m返回n以10为底的对数
9 U& J, z7 |4 J% umysql> select log10(2);
# N0 {, g% \- f, e8 r: H+ h -> 0.301030 1 f0 o( ^3 i3 B4 {- z, s
mysql> select log10(100);
3 Q- c; }$ [; _! I. A7 F/ h0 n0 k -> 2.000000
3 F0 C. x0 c+ A5 r& rmysql> select log10(-100); g( k3 z: ~+ }9 L5 r6 g/ R( ] ]
-> null
1 Q* ^/ | ~: `- |; ` . d/ a: o' Z! y1 Q. _% Z% k
pow(x,y) 2 V% W( z$ k* O& M o5 Y% i H
power(x,y)
1 T5 G0 d3 C. f0 I. O, e" Y 返回值x的y次幂
. d4 O" B# j5 ?* zmysql> select pow(2,2); 5 Y* c0 X7 ?3 v0 J6 }
-> 4.000000 % B$ ~0 ?2 F1 V
mysql> select pow(2,-2); / h* a* P2 H3 q& Z0 N
-> 0.250000 $ P* Z) r( f3 b Y2 v
9 K/ h/ W& B7 A
sqrt(n)
( i. }6 e2 b; x( E5 m 返回非负数n的平方根
8 m8 b. z- p2 M) J1 wmysql> select sqrt(4); / W2 N+ ?$ G& z: m8 C' } B! P
-> 2.000000
- u2 F6 w0 ?! G! J* R- g( jmysql> select sqrt(20);
$ v( A1 P( `" ?8 w: b -> 4.472136 # E% [/ I/ B8 R2 t' A
' [* k$ d! h& [( Q+ e1 |+ i
pi() 3 y8 L! M# v; Y% t
返回圆周率
/ ~( `4 J L H1 D( s3 ]- m% omysql> select pi(); ( i+ J8 r" g1 |4 f* c
-> 3.141593
. O8 s$ B/ N- Z4 H ; c, K% r$ A6 n. i
cos(n)
T# w, \" c8 F 返回n的余弦值 1 h* U6 {1 m M! |) Y2 r! \% o
mysql> select cos(pi());
# |3 V" g% P, ~( _1 }1 m; P -> -1.000000 ; ]5 {" k, j/ x( z2 E
- E1 s' g8 f, w& q- V9 \$ fsin(n) 0 ~4 ~9 A1 B$ ]. e( V: @
返回n的正弦值 0 m; D* U6 Y" v' f; j# P3 R
mysql> select sin(pi()); ) A4 I" j [" ?" T7 R$ u/ e
-> 0.000000 7 g/ Z3 k; J* t4 X' z( o4 r% J
F/ S! x% r8 x1 Z3 a
tan(n) 3 p q) A+ b w# s9 K' @9 r- f; L
返回n的正切值 0 x9 _" {! a7 Q! G/ g- w
mysql> select tan(pi()+1); ) k0 C8 K1 Z% Z/ {* i; \2 }9 L, A1 m
-> 1.557408
* ^8 U' z3 o# w/ L+ M7 T+ Q0 S , s. N( @$ x/ ?- ?6 z( }
acos(n)
6 Y# e' }+ o X, w$ z* B6 p 返回n反余弦(n是余弦值,在-1到1的范围,否则返回null)
3 J W G3 \! {, G0 ]+ hmysql> select acos(1);
' p/ F5 i# V! ~" k3 P -> 0.000000
" n2 d3 l2 x; H6 l+ J/ O, t/ cmysql> select acos(1.0001);
/ p2 ~4 L' W; O1 V4 {* g -> null ' U' e; r- _( e V+ C
mysql> select acos(0);
& P1 M, k# B9 X9 E -> 1.570796 4 n A& n) P1 }5 m
: h1 L' D$ ?3 W, o/ a- e$ o. X( ^' h" Nasin(n)
- V" E- X0 V4 L/ M6 N. ?返回n反正弦值
# V$ Y1 D2 H8 R2 ]0 V' R7 Kmysql> select asin(0.2);
* Z4 |0 b* R' s1 p/ d% u -> 0.201358 2 e0 y2 e! x+ C& i/ |
mysql> select asin('foo');
0 T7 n1 v& g0 D: N( _ -> 0.000000 : T% a6 A. _# J) H/ H: x
w+ r: z) |& L( a! x; E
atan(n)
' x% P/ T/ @. H返回n的反正切值
5 D i. R9 i7 ^( S; u$ F6 Mmysql> select atan(2);
o8 t- Q t. i5 ~# r -> 1.107149 * z7 g: r% H$ b y3 |
mysql> select atan(-2); . p0 O- k+ R" ^( i1 E& Z5 A( ?
-> -1.107149
$ d3 \, R- W. a% N. D$ D: ^ `) Batan2(x,y)
' Q# k ^ k: i( a( @ 返回2个变量x和y的反正切(类似y/x的反正切,符号决定象限)
' D6 V5 l2 ]4 x" D7 ^- m" Hmysql> select atan(-2,2);
: [* D( p# X, B6 h; }6 @ -> -0.785398 & f5 d! j* _: Z8 ^. y
mysql> select atan(pi(),0); 7 ]" i7 ?3 j1 H" |. v# P* X
-> 1.570796 3 [1 Q1 f" G/ F# A# C
+ T+ b9 ? [9 k4 j: q! |" ucot(n) , L, r' Y0 u$ X( @
返回x的余切 9 a0 Y) J5 N" C3 R$ c) v" X
mysql> select cot(12);
4 l( \* |& T6 V! s2 F7 j -> -1.57267341
4 S5 j& ?: O- {$ b( V4 @9 ymysql> select cot(0);
: G% `5 ~+ r) I0 Z/ J( P -> null 9 o4 S' D7 ~ I* f: y2 @. k, I. Z2 K
. d) r4 K) v( S% m( {) D. Z) Arand() % A. N8 k) z3 ~5 v2 \5 n
rand(n)
3 i: @! k# s+ z, M8 a返回在范围0到1.0内的随机浮点值(可以使用数字n作为初始值)
' r) l* k9 d/ p- X) w/ E* C
* z0 { q4 M P C; ?/ wmysql> select rand();
1 ~, l) o7 O/ ?: a% ~ -> 0.5925
) u/ l1 a8 D- J+ v- q( h& imysql> select rand(20); 2 G: P5 H) O2 ?" b4 o1 F
-> 0.1811
, i: z+ E2 F) X# q, {( K. |: {mysql> select rand(20);
7 E; H2 e) x+ p3 X1 t -> 0.1811 E8 I4 ^$ w! L4 R0 U8 L0 c
mysql> select rand();
. v0 L& |% b1 y% M. \ -> 0.2079 ; o1 [. g' X% p. _2 q
mysql> select rand(); $ A( @0 i* Q. \# ]6 h. [: s
-> 0.7888
- C: X# U) O6 ?4 @. A
, z5 ~( q) ^: z8 t1 n6 cdegrees(n)
9 t: Z3 J0 p' ~6 h) |7 _2 B! p- G把n从弧度变换为角度并返回
! T' _& _' R X8 A7 {2 u) x! tmysql> select degrees(pi());
! W7 i$ v3 j, `$ l1 d% J7 a1 Q -> 180.000000
5 i# r7 ?( t6 \# T: q. a2 V
2 Z5 j8 y; V0 H/ K$ b+ Mradians(n) 3 g$ Y" j1 K5 b( M
把n从角度变换为弧度并返回
! }' z7 O7 u, ]. rmysql> select radians(90); 7 s' S5 j: U/ z3 b6 `
-> 1.570796
2 R& f' O" [0 O. A! J+ } T; }- m% J) `% ^
truncate(n,d)
. Q3 x* J3 j, |* r) d* Y保留数字n的d位小数并返回
# a) h ^3 I; S$ P1 Omysql> select truncate(1.223,1);
( [. W3 d5 L4 w6 C -> 1.2
9 J, l1 |0 D0 \9 p; ?mysql> select truncate(1.999,1); - E6 r; Z1 H( _6 u, C. O
-> 1.9
9 [2 l. E$ c6 ]7 |1 T' k. Gmysql> select truncate(1.999,0);
# Z G: g4 F( v: m, u ~ -> 1
- P5 H0 s# E! g. z7 e S4 T% v; p& e9 R' Q6 x" v; L
least(x,y,...) : Q% z: ^+ P# n# c
返回最小值(如果返回值被用在整数(实数或大小敏感字串)上下文或所有参数都是整数(实数或大小敏感字串)则他们作为整数(实数或大小敏感字串)比较,否则按忽略大小写的字符串被比较) & K( Y8 |- H7 Q6 V" g! O/ c# B* y
mysql> select least(2,0); $ L; D) R& F; l. s1 ?
-> 0 0 L) X. u$ ] O' L
mysql> select least(34.0,3.0,5.0,767.0);
4 c5 ]# _* C( `' X9 A -> 3.0
6 o! Y5 z9 Q. o% p$ f3 p5 Cmysql> select least("b","a","c"); / G( A c5 q% w9 E
-> "a"
, o: `% n# @" Q
5 B4 ?+ g0 Y, Z' P% ]+ A1 b" s. s- k( r; hgreatest(x,y,...)
6 S5 X- Y s7 @% j2 H% w返回最大值(其余同least())
. l' |" r( K3 E- d& qmysql> select greatest(2,0); ; X8 p& n3 K+ L8 U! I, u
-> 2 8 U# `- ~$ R& s& q
mysql> select greatest(34.0,3.0,5.0,767.0);
3 f9 @6 U) i; w; n( ~5 x( @/ l -> 767.0
& z0 T/ e4 h2 c# a% t& l3 Emysql> select greatest("b","a","c"); . i4 c" f) Z/ z* Z. d. x
-> "c"
]" H5 O6 n( d* t. q" u% F* r5 Q& G6 M
3、时期时间函数 2 _, G3 H0 e6 o6 }1 @
dayofweek(date) , |1 V* a2 j& q$ I! c+ V% f3 ?
返回日期date是星期几(1=星期天,2=星期一,……7=星期六,odbc标准) # y, @) s' F+ X, W
mysql> select dayofweek('1998-02-03');
0 l/ d& _9 ? z6 y8 J" l4 w9 {5 E2 p -> 3 . \) y4 h: \. v" c
$ Z) [2 r9 K+ a! D* ~; @' ^
weekday(date)
3 E( o { A/ c: @# W" c返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。
( M! W0 p9 t; ~; ~ ) Q2 L* e. g. ?/ [
mysql> select weekday('1997-10-04 22:23:00');
: }" ~6 P* o5 B% f: Z% j0 f4 Z -> 5
9 H% w$ ~9 V: r. e2 g% rmysql> select weekday('1997-11-05'); ) n8 }" I) a: _
-> 2
- O4 |* _6 {. l- w: M& H
' S6 y& o8 `/ H: U! P* [1 D# Kdayofmonth(date) 7 ^6 [8 w' Q2 `2 @/ B$ A! [, l; Y
返回date是一月中的第几日(在1到31范围内)
1 i: R5 S. @- J3 n% x& Jmysql> select dayofmonth('1998-02-03');
' s9 F! B, E( C: p -> 3 ( b) [) @5 [' n# ~+ O, s" W
6 k+ l/ s% u& q/ A2 L2 Y3 a! jdayofyear(date) & j: x) O8 t' f
返回date是一年中的第几日(在1到366范围内)
1 C7 W% ?& [8 u2 s. X' lmysql> select dayofyear('1998-02-03'); 2 {0 m! ? q; d) }
-> 34
# w k8 O/ r; ^' `8 z C! j7 \
+ u! M1 K$ [- D" G, P: ~7 ^month(date) 6 d$ `4 ^! M, i8 ^. z, F& _
返回date中的月份数值 2 K" H4 E& m- m2 t0 L# o
mysql> select month('1998-02-03'); L d5 z/ Y9 ?: |$ K
-> 2
; `- Z7 |, Z2 J i
) H1 y1 _* d, z! k3 J+ [9 Fdayname(date)
; H& j, K) p$ I返回date是星期几(按英文名返回)
: C, l7 b7 r- ]/ n8 \mysql> select dayname("1998-02-05"); 4 n/ Y" H7 _/ J, _4 B# z% l
-> 'thursday'
6 D* ^/ D; d" q# g, x
, H# u/ W1 D) B" d dmonthname(date)
- f' }( n2 G+ Z- R* G返回date是几月(按英文名返回) + l3 C) S+ ? @! \8 [8 e2 ?: `- P
mysql> select monthname("1998-02-05"); " Z' O6 j# Y; g, W
-> 'february' ) G9 i, b) l# m" ]- R
: [! @! J% {; a& p5 Q, Qquarter(date) / ? o& [6 G' f. j# n# n
返回date是一年的第几个季度 & g) U4 ~! W' k y7 m; n. \9 O
mysql> select quarter('98-04-01'); , P. `3 p6 C! P) w& m
-> 2 % {) `* Z' j# b9 ?) \7 F6 P( G4 @4 U
7 K9 o2 i) @; f; x2 r; |0 A( k' h
week(date,first)
/ @$ N7 C& `% H/ ~- _% C+ R返回date是一年的第几周(first默认值0,first取值1表示周一是. v3 C8 P/ M. ]7 e
周的开始,0从周日开始)
$ n9 b1 b6 Y, G$ k" Qmysql> select week('1998-02-20'); * r& p/ ~& `: g- p& v
-> 7
2 I+ H. k B/ G# ?1 M5 Umysql> select week('1998-02-20',0);
. v. z5 h9 Z% o4 n) G; ^* I -> 7 4 Y' ?' ?( M* L
mysql> select week('1998-02-20',1); . F$ P4 x8 _' I: h6 c
-> 8
" d0 j& a4 v) c, g+ I2 ?
% s$ m. f- g/ z5 l k3 d. n+ k, V nyear(date)
* A' f; f w7 c d( r7 h( u返回date的年份(范围在1000到9999) - E% J" X3 @" ^ c" T3 j
mysql> select year('98-02-03');
* g" P, T) w7 n3 |+ | -> 1998
! X% t+ ^ r U/ m1 F
6 u6 ^) h) s( ?6 Bhour(time)
k5 k9 Z# M0 s: ^返回time的小时数(范围是0到23)
( E6 d2 w& |" k) q2 hmysql> select hour('10:05:03'); 0 Q. N) w; S% Z
-> 10 - ?/ H2 A5 s r/ F; X6 w
/ y; @, V* \8 z& y2 R
minute(time) Q% I6 y- J e
返回time的分钟数(范围是0到59) 0 T0 N- d1 O, [! u
mysql> select minute('98-02-03 10:05:03'); / [& a) G. R& E
-> 5
: W! g: h& M& ~) d. \ + N( B8 e% }& ^( ?
second(time) ! q4 E! P# b5 |( u( `6 o" V( ^/ e
返回time的秒数(范围是0到59) 8 G$ x t: E$ `3 U' v4 y* O
mysql> select second('10:05:03');
H( n* `* w# @" i9 [ -> 3 : d7 W8 O* S# O* }# L* s
/ p, e' t9 ~5 w( W: x r9 }4 O
period_add(p,n)
0 e, [0 P& {% D! _增加n个月到时期p并返回(p的格式yymm或yyyymm)
3 K) g5 z4 G6 o1 m! U7 pmysql> select period_add(9801,2);
8 U( U) S3 o2 P -> 199803
8 x" b2 G& [" n- Y( c * e- b5 P8 z' s6 q4 f" P
period_diff(p1,p2)
6 |: z8 n' D: N. \- F+ S) c返回在时期p1和p2之间月数(p1和p2的格式yymm或yyyymm) 3 Q. w% t/ r% F
mysql> select period_diff(9802,199703); ) `: k1 J/ E& _7 t9 ]. [
-> 11 % P7 }+ Q: h) J0 D/ p& I) W. s3 W
, z' @0 N' A1 |" c+ hdate_add(date,interval expr type) - x" ~7 f6 O/ d0 N1 H# [1 Z; m8 a
date_sub(date,interval expr type)
E9 I, S) m4 T% u- v5 A6 Ladddate(date,interval expr type) & Y% n/ o6 ?; I0 I$ F8 C
subdate(date,interval expr type)
2 h0 y) ~$ Q2 ?" F/ L# o$ O对日期时间进行加减法运算 , g* Z; [) Q _2 h
(adddate()和subdate()是date_add()和date_sub()的同义词,也
. j& N8 M+ n% E( Y可以用运算符+和-而不是函数
' [0 v" x* P( |" G( Kdate是一个datetime或date值,expr对date进行加减法的一个表
& S. ^) J; Z+ s1 h. \: V6 _达式字符串type指明表达式expr应该如何被解释 - T, m: D8 P/ O+ r [1 L
[type值 含义 期望的expr格式]: ! D6 s; x8 N3 Z0 X
second 秒 seconds
@! L' R$ J- L, \& r l6 f minute 分钟 minutes 7 o4 Q) y6 }, \, D3 M- O1 X% l
hour 时间 hours
* n5 P6 J2 h0 Y+ S2 ^) C. V day 天 days 8 u9 O8 ~! h8 R9 |
month 月 months 4 p' R! x; ^" M& g+ \! n u: J
year 年 years
1 ?, C( [: h- R* O3 _; T9 a4 t minute_second 分钟和秒 "minutes:seconds"
) Z z! l `5 j( j: a hour_minute 小时和分钟 "hours:minutes" $ L! z9 S' g! F: E
day_hour 天和小时 "days hours"
4 O+ K& b+ X0 q5 ?* G year_month 年和月 "years-months" 0 q3 d [8 B9 ]1 x/ Q' ~3 j
hour_second 小时, 分钟, "hours:minutes:seconds" 9 W. L- g2 |, F' ~
day_minute 天, 小时, 分钟 "days hours:minutes" " C" Q, N* L! b7 f
day_second 天, 小时, 分钟, 秒 "days* Z6 U" w% p5 g L+ L& A
hours:minutes:seconds"
4 t! [# p; E, p# P3 F1 f1 g expr中允许任何标点做分隔符,如果所有是date值时结果是一个/ _7 e' z0 `6 d5 Q% f/ M. g. }
date值,否则结果是一个datetime值)
% ~9 U" A5 s8 k4 X, g 如果type关键词不完整,则mysql从右端取值,day_second因为缺2 X5 p4 z3 z# i8 m r
少小时分钟等于minute_second) ; w0 }$ ^7 T6 S" |8 L7 t
如果增加month、year_month或year,天数大于结果月份的最大天
' [9 o5 }3 K. U" U7 B数则使用最大天数)
$ ^, k/ z- @8 A8 u; u4 v$ ~mysql> select "1997-12-31 23:59:59" + interval 1 second; ( E7 L7 }, a2 k1 w
9 ?( P1 \( ^+ O3 ?4 A' j, G; ?
-> 1998-01-01 00:00:00
2 ]2 m% G2 y0 _$ {( cmysql> select interval 1 day + "1997-12-31";
' T0 U* h6 L8 p -> 1998-01-01
g: N& N. T s$ Q$ Wmysql> select "1998-01-01" - interval 1 second; 2 l, A Z& g, N3 h- o. v
-> 1997-12-31 23:59:59
$ U! J9 s6 a7 }6 gmysql> select date_add("1997-12-31 23:59:59",interval 1) ~% m. r, j0 ?! k; H* j
second); + z+ q' Q* I# H; [5 ~
-> 1998-01-01 00:00:00
8 m b) n* ?) ^4 {6 J: ^0 Bmysql> select date_add("1997-12-31 23:59:59",interval 19 p2 z) p5 P0 k: X3 ]
day); * H) c% X6 d4 _1 x2 p
-> 1998-01-01 23:59:59 " X9 _2 M" u: I( I6 u2 n ?
mysql> select date_add("1997-12-31 23:59:59",interval+ o" j# H+ [3 f6 M0 O0 e0 ]; o+ F
"1:1" minute_second); / p+ |. I. x) ?. L1 }! p- a3 N
-> 1998-01-01 00:01:00
. b1 k, i& ~% F$ U* U: E8 Fmysql> select date_sub("1998-01-01 00:00:00",interval "1# d9 }" F. `2 M6 S6 _0 z
1:1:1" day_second); . a+ t8 B3 a+ Y& U: y5 J
-> 1997-12-30 22:58:59
' x% U, R% }' `3 x6 Y) [mysql> select date_add("1998-01-01 00:00:00", interval "-1
- [* J' h/ ~1 f& x) }10" day_hour);
6 i0 _# ~ _8 M* k- ] A& L -> 1997-12-30 14:00:00
u9 r% j! Q& i7 l! X! C3 [mysql> select date_sub("1998-01-02", interval 31 day); + Z0 G" N4 ~) P) g
-> 1997-12-02 3 N) d" C4 y4 Q; j, O' e7 R2 R
mysql> select extract(year from "1999-07-02");
, a" d+ k% [# G8 k4 r! o+ a5 h -> 1999
6 Y t0 {: B$ a. ]" S, ?3 |% Wmysql> select extract(year_month from "1999-07-02& T; Z. P% ]' s$ S4 l
01:02:03");
1 x$ T6 j7 x" F6 [ -> 199907
4 n; L9 ~$ k9 D4 w8 Pmysql> select extract(day_minute from "1999-07-02/ L' q8 s$ f) G7 v
01:02:03");
9 J z" x& D1 k+ q' C" f -> 20102
5 L6 |9 ~3 d7 m5 Q
- Q' Z6 X. i" Y- u, a9 M4 @to_days(date)
5 ?4 U/ w3 m/ Z% j$ y2 X返回日期date是西元0年至今多少天(不计算1582年以前)
1 S8 }' R& G% V6 g2 t' `mysql> select to_days(950501);
& P- G; ?; D; X( z1 G' Y! F -> 728779
) Z" w* Y. P1 M0 w, O F. ?4 F6 gmysql> select to_days('1997-10-07'); * E5 a, N: k* h% W' t
-> 729669 " U! r; Z9 r5 ^' j& k5 u- j- r. }
& P0 @: x, i' {" M; |from_days(n) ' {& Y8 q h+ S$ [' {( K4 }
给出西元0年至今多少天返回date值(不计算1582年以前)
2 u Q' Z/ K0 u3 p9 F1 _( `0 F! Q. x Vmysql> select from_days(729669); * c S6 R {# g; B$ v0 q# S
-> '1997-10-07' 9 E9 z0 p. H; Y' L
) ?: a* w( F- E; x7 Zdate_format(date,format)
X+ p8 O7 ^. ]. s; B 根据format字符串格式化date值 - n' j7 T4 S# b+ N/ U) {% K
(在format字符串中可用标志符: 3 G s8 G$ U7 A: o% z2 I
%m 月名字(january……december) $ }9 d) E. \, K- g! x& S( W
%w 星期名字(sunday……saturday)
6 L2 t" Q# w* ~% V6 s. } %d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。) - x y& t8 F4 R* `0 o- z/ Y" m
%y 年, 数字, 4 位
1 P ^ M: V) w% G %y 年, 数字, 2 位 , q$ X" C# @! \3 z) h7 a4 D
%a 缩写的星期名字(sun……sat) 9 B3 e. O) b" _. r, `8 I5 I
%d 月份中的天数, 数字(00……31)
* W% L$ x0 N; x/ w/ ~ %e 月份中的天数, 数字(0……31) ( B- ~3 q1 t. }6 P$ U
%m 月, 数字(01……12)
Y5 N% B1 @/ _/ I) A %c 月, 数字(1……12) 1 z A& A( o8 ?" q* w# ^# g
%b 缩写的月份名字(jan……dec) : |) v- ~9 \- F! c3 N) q
%j 一年中的天数(001……366)
% S6 M* N- G0 C* L; T %h 小时(00……23)
" q1 s% ~0 r4 y3 ^$ F %k 小时(0……23) 5 ~! X0 r9 ^5 ]* `$ i. Q
%h 小时(01……12)
$ ~: u8 G: B+ [2 c. e %i 小时(01……12)
6 g$ Y2 E5 N- R %l 小时(1……12) 5 M* E& m9 T3 b% n4 J
%i 分钟, 数字(00……59)
& x2 f# T( ]( ~. B5 `: M %r 时间,12 小时(hh:mm:ss [ap]m)
3 H; @. [. a4 j$ x/ K %t 时间,24 小时(hh:mm:ss) 2 b$ l) x/ ?* t$ J0 v4 f0 L
%s 秒(00……59) ( F8 `& v" p2 q! [
%s 秒(00……59) 3 Y: C- m, S5 Y6 G8 ]
%p am或pm
3 s0 R2 z3 {# Z( B %w 一个星期中的天数(0=sunday ……6=saturday )
& R' \& Y( f4 ]( K, U2 p- l %u 星期(0……52), 这里星期天是星期的第一天 / x1 w5 Q6 J% ~, [ e; w- D( p) q
%u 星期(0……52), 这里星期一是星期的第一天
; d3 d( r; }2 U6 X2 c" R, E1 W8 P %% 字符% )
1 i/ ^. E" I C; l" ]. |# O; u0 e; Dmysql> select date_format('1997-10-04 22:23:00','%w %m %: Z' ?+ e3 _) k5 {, R: V; L- Y3 \
y');
- z+ P* F) j+ n* U- ? -> 'saturday october 1997' 0 ~7 `7 b/ {5 r5 o
mysql> select date_format('1997-10-04 22:23:00','%h:%i:%
' _, q0 }- \7 _( H+ }+ D8 |s'); # @! {* I2 ^" ?2 y6 }$ z E* r
-> '22:23:00' ' ?! q8 o9 e7 q4 z; `
mysql> select date_format('1997-10-04 22:23:00','%d %y %a
D0 V3 S2 U) B4 Z%d %m %b %j'); 2 l3 r" E% J0 ~4 g% [& ]
-> '4th 97 sat 04 10 oct 277'
" \' G+ @4 ]# B F2 q- jmysql> select date_format('1997-10-04 22:23:00','%h %k %i
( i0 w# j/ _. N" a%r %t %s %w'); 8 { `8 c! V- \+ t
-> '22 22 10 10:23:00 pm 22:23:00 00 6' * t4 y, c! g3 I* ~: {
9 ~: Z3 y( G' {. A1 @+ A" c5 m. _time_format(time,format) 1 o% y D' l. C b
和date_format()类似,但time_format只处理小时、分钟和秒(其& \. Y2 O) L2 r! o) Y. v
余符号产生一个null值或0)
* S7 M( @. H% n% _ y! R5 B " j8 w1 @& d. S7 L7 c, e% }
curdate() ) N( \5 R) j# t1 O! ]2 K
current_date() : ?+ k$ S8 O3 G% b) N) c
以'yyyy-mm-dd'或yyyymmdd格式返回当前日期值(根据返回值所3 F. V2 J6 n: J- l
处上下文是字符串或数字) 3 S7 ?5 K6 |+ J, c J0 F B
mysql> select curdate(); & I, x# o( D k! `# ?
-> '1997-12-15' 5 ^ \/ ?) l f9 B. n
mysql> select curdate() + 0; 5 ^. s4 F- s; \2 |$ `7 \$ b
-> 19971215 8 w9 g; ], U/ N' J
9 b- M: @1 g. tcurtime()
3 M' Q7 K5 j" ~7 m( O1 bcurrent_time()
0 d+ t. j/ T0 r& H 以'hh:mm:ss'或hhmmss格式返回当前时间值(根据返回值所处上: w6 L$ {% h3 P/ b, U$ E( F% ~% z
下文是字符串或数字)
) D4 ~9 B9 j/ ^8 J( \* omysql> select curtime(); 6 t9 F& Y# j" m! m
-> '23:50:26' . M( n5 S* ? H3 ~" z# P* g* b
mysql> select curtime() + 0; : Z: g1 {" u5 W( V# o0 m4 i! l0 V
-> 235026 8 q" X+ c# e' v
3 ~" Q) S0 f& |; Know()
* k' }/ u/ G! w: c* Jsysdate() 4 a# c2 w f0 ?0 Q. ?
current_timestamp() 1 o9 ?3 W& C" |# V* |6 U
以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回当前日期- K Q2 u2 y" l) U$ W6 N
时间(根据返回值所处上下文是字符串或数字)
2 C$ X1 x8 e" _# ~+ Y/ H& `mysql> select now(); : ^; K+ l. s0 k A- g
-> '1997-12-15 23:50:26'
6 W o1 P5 {1 N' o% Xmysql> select now() + 0; 2 A9 E, N7 Q% C" p' G- E
-> 19971215235026 5 Z v C/ H. m: c# o
3 I* r; m* K2 c2 n- ~7 g
unix_timestamp() + ^) C( S ^- |) w5 x
unix_timestamp(date)
- E* K) u k& h返回一个unix时间戳(从'1970-01-01 00:00:00'gmt开始的秒: i+ K0 |7 \2 |# P
数,date默认值为当前时间)
% a5 W# S! A6 G7 y) @, }0 P7 Zmysql> select unix_timestamp();
4 g" M) ^" \$ J- ]2 ^) T -> 882226357
9 J& w2 e; ?. o5 c( c7 jmysql> select unix_timestamp('1997-10-04 22:23:00');
2 B6 E- m+ g/ q& L- g* Q. X8 x4 e -> 875996580 ! {, k3 h! Q; W, k
& u+ X8 `" S; [" p# D
from_unixtime(unix_timestamp) ; s7 B3 @5 K! M" w m
以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回时间戳的 ~; f1 y# V) @! n' M6 |
值(根据返回值所处上下文是字符串或数字)
! c- e, L0 C2 v: imysql> select from_unixtime(875996580); + [2 D; B$ `4 b) ^
-> '1997-10-04 22:23:00' # [/ F# Q; O; Y+ f; ]) O
mysql> select from_unixtime(875996580) + 0; |) w# u' L9 f! ^8 j2 \& z+ I- \! W
-> 19971004222300 4 |/ K# Y5 z9 S) N
- p1 d ]! w; {. y# b% y, j) H3 c
from_unixtime(unix_timestamp,format) 8 v8 U& p5 j- {* Z
以format字符串格式返回时间戳的值 / g7 o, A+ z( ^" S
mysql> select from_unixtime(unix_timestamp(),'%y %d %m %2 q2 Y% l3 D* l1 [, N
h:%i:%s %x');
6 Z, M- c( o3 B- s' x -> '1997 23rd december 03:43:30 x' ?* C5 c* A; K. w2 K
) `8 V1 ]5 V% A- W/ ?6 {, N
sec_to_time(seconds) K% a- @4 N/ z
以'hh:mm:ss'或hhmmss格式返回秒数转成的time值(根据返回值所处上下文是字符串或数字)
! H7 ^0 ?* [# H) P: w5 Zmysql> select sec_to_time(2378); ) F1 w; C* P6 K! V, m
-> '00:39:38'
$ s; h2 ^- \. R: S6 i; tmysql> select sec_to_time(2378) + 0;
" k+ k5 g+ s( q% T8 X5 k* y0 \0 _3 M -> 3938
: _5 S9 N) p- Y- t* k
# S2 i5 W6 @4 \) R0 L6 k3 K W3 w0 qtime_to_sec(time) 8 u8 [% X: K2 c7 R7 D [
返回time值有多少秒 2 G6 J9 j5 X- B* n, W
mysql> select time_to_sec('22:23:00');
+ V0 i8 I- l! Y6 J -> 80580
& y( d5 [' B6 h. }' nmysql> select time_to_sec('00:39:38');
6 y6 Q8 b% m9 `8 X1 E! k4 C -> 2378
+ }3 j2 D( h2 x- L! u8 r1 q- P % t; f* q1 _6 x0 K3 E
转换函数$ M" ~' a2 A- ]0 f9 U2 ?. L: _: f
cast% C2 `- e/ j2 b8 I9 y, f
用法:cast(字段 as 数据类型) [当然是否可以成功转换,还要看数据类型强制转化时注意的问题]
' J4 H2 m( l+ a( J实例:select cast(a as unsigned) as b from cardserver where order by b desc;: s* j9 n, h+ z
convert:- Z9 `2 z3 h6 W* J" e9 R, v$ F6 T2 E9 u
用法:convert(字段,数据类型)
( O# n' A% r4 N5 R* _' t3 x实例:select convert(a ,unsigned) as b from cardserver where order by b desc;
! G _) ~% K& I5 D5 X7 e |