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