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