1.字符串函数
//字符串连接
select concat('aaa','bbb','ccc'),concat('aaa',null);
//insert(str,x,y,instr):将字符串从第x位置开始,y个字符长的子串替换为字符串instr
select insert('beijing2008you',12,3,'me');
lower(str):将字符串转换为小写
upper(str):将字符串转换为大写
left(str,x):返回字符串最左边的x个字符
right(str,x):返回字符串最右边的x个字符
lpad(str,n,pad)和rpad(str,n,pad):用字符串pad对str最左边和最右边进行填充,直到长度为n个字符长度。
select left('beijing2008',7),left('beijing',null),right('beijing2008',7);
select lower('BEIjing2008'),upper('BEIjing2008');
select lpad('2008',20,'beijing'),rpad('beijing',20,'2008');
ltrim(str)和rtrim(str):去掉字符串str左侧和右侧空格
repeat(str,x):返回str重复x次的结果
replace(str,a,b):用字符串b代替字符串str中出现的字符串a
select ltrim(' |beijing'),rtrim('beijing| ');
select repeat('mysql',3);
select replace('beijing_2010','2010','2008');
**strcmp(s1,s2):比较s1和s2的ASCII码值的大小
s1
select strcmp('a','b'),strcmp('b','b'),strcmp('bc','b');
select trim(' $ beijing $ ');
select substring('beijing2008',4),substring('beijing',1,7);
2.数值函数
- abs(x):return |x|;
- ceil(x):return min integer >x;
- floor(x):return max integer
-
select abs(-0.8),abs(0.8); select ceil(-0.8),ceil(0.8); select floor(-0.8),floor(0.8);
select mod(15,10),mod(1,11),mod(null,10); select rand(),rand(); select ceil(100*rand()),ceil(100*rand()); select round(1.1),round(1.1,2),round(1,2); select round(1.235,2),truncate(1.235,2);
3.日期和时间函数
curdate():返回当前日期 curtime():返回当前时间 now():返回当前日期和时间 unix_timestamp(date):返回日期date的unix时间戳 from_unixtime:返回unix时间戳的日期值 week(date):返回date为一年中的第几周 year(date):返回date的年份 month(date):返回月份 monthname(date):返回月份名 hour(time):返回date的小时 minute(time):返回time的分钟值 date_format(date,fmt):按字符串fmt格式化日期date值 date_add(date,interval expr type): 返回一个日期或时间值加上一个时间间隔的时间值 datediff(expr,expr2):返回起始时间expr和结束时间expr2之间的天数
select curdate(); select curtime(); select now(); select unix_timestamp(now()); select from_unixtime(1184134516); select week(now()),year(now()); select hour(curtime()),minute(curtime()); select month(now()),monthname(now()); select date_format(now(),'%M,%D,%Y'); select now() current,date_add(now(),interval 31 day) after31days,date_add(now(),interval '1_2' year_month after_oneyear_twomonth; select now() current,date_add(now(),interval -31 day) before31days; select datediff('2008-08-08',now());
4.流程函数
- if(value t f) value为真,返回t,否则返回f
- ifnull(value1,value2) value1不为空,返回value1,否则返回value2
- case when[value1] then []result1]…else[default] end value1为真:返回result1,否则返回default
- case [expr] when [value1] then [result1]…else[default] end 如果expr等于value1,返回result1,否则返回default
-
create table salary(userid int,salary decimal(9,2)); insert into salary(1,1000),(2,2000),(3,3000),(4,4000),(5,5000),(1,null); select * from salary; select if(salary>2000,'high','low') from salary;
select ifnull(salary,0) from salary; select case when salary<=2000 then 'low' else 'high' end from salary; select salary when '1000' then 'low' when '2000' then 'mid' else 'high' end from salary;
5.其他函数
- database() 返回当前数据库名
- version() 返回当前数据库版本
- user() 返回当前登陆用户名
- inet_aton(IP) f返回ip地址的数字表示
- inet_ntoa(num) 返回数字代表的IP地址
- password(str) 返回字符串str的加密版本
- MD5(str)返回字符串str的MD5值