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