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