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