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