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