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