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