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