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