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