无忧站长 >

mysql 常用命令之函数

2019-11-01 14:45:28 
函数如下:
left,right 字符串截取
from_unixtime 格式化unix时间戳
concat 字符串连接函数
max 取某列最大值
min  取某列最小值
sum  计算某列的和
count 统计条数
md5  返回md5加密码的串
format  格式化数字为xx,xxx,xxx.xxxx格式 比如1,1000.123
length 计算某个字符串长度
distinct 去重复
replace 替换字符串
in  指定查询某个值的记录
like 模糊查询
is null 查询某个条件为空(null),注:null不等于""
is not null 查询某个条件不为为空(null)
MATCH ... AGAINST ... mysql的全文索引查询
mysql left,right函数
left和right是一对截取字符串前几位可后几位的函数,left是从左向右开始计算,right相反是从右向左计算
例:
select left(name,10) as name from user; 显示用户名的前10位
select right(name,10) as name from user; 显示用户名的后10位
select * from user where left(datetime,10)="2011-12-02" 取出2011-12-02日注册的用户
select * from user where left(datetime,7)="2011-12" 取出2011-12月注册的用户
left,right不仅仅可以用于截取字符串,还可以用在where条件上。特别是用在查询条件上他的作用非常大。
mysql from_unixtime函数
from_unixtime函数用来对unix时间戳进行格式化,格式化成我们易读的日期时间格式。
例:
select from_unixtime(time, "%Y-%m-%d %H:%i:%s" ) as datetime from table; 把time字段格式化成易读的日期时间显示(time为unix时间戳)
select * from table where left(from_unixtime(time, "%Y-%m-%d" ))=’2011-12-02’ 取出2011-12-02日的记录
mysql concat 函数
concat函数 可以用来把某二个字符连接在一起查询或显示,也可以把字段和字符串进行连接。
例:
select concat(year,"-",month,"-",day) as datetime from table; 把表中year,month,day字段连接起来显示
select concat("My name is:",name) as name from table; 把字符串和字段连接起来显示
update software set icon=concat("http://iteye.com",icon); 把数据库中icon批量更新并在原有的icon前增加域名 iteye.com
mysql max,min函数
顾名思义max函数用于查询某个字段中的最大值
例:
select max(age) from user; 返回最大的年龄
select min(age) from user; 返回最小的年龄

mysql sum函数
sum函数 可对某个字符(int型)进行求和
例:
select sum(money) from user 计算出所有人的金钱总数
select sum(money),area from user group by area 计算出各地区人员的金钱总数

mysql count函数
统计聚合函数,可对sql查询的结果进行统计
例:
select count(*) as total from user 计算出总会员 iteye.com
mysql md5函数
同php中的md5一样,对某个字符串进行加密
例:
select md5(password) as password from user 把明码的密码进行md5加密显示
insert into user(name,password) values("abc",md5("abc")); 写入user表把密码进行md5加密后存储

mysql format函数
用于格式化数字为xx,xxx.xxx格式的数字
例:
select format(downloads) as download from software; 把下载量格式化为xx,xxx格式如:12,000
select format(money,2) as money from user; 把用户金钱格式化为xx,xxx.xx格式,参数2为精确的小数点位数如:12,000.05

mysql length函数
计算某个字段值的长度
例:
select length(name) as length from user; 显示出用户名的长度
select * from table where length(aa) > 10 ; 查询某字段长度大于10的记录 php程序员站
mysql distinct函数
对某个字段去重复,(在某些时候group by也可以做到)
例:
select distinct(area) from user; 对地区进行去重复
select area,count(*) from user group by area; 对地区进行聚合并统计出数量

mysql replace函数
查找某个字符串并进行替换
例:
select replace(icon,"www.iteye.com","img.iteye.com") from software; 把icon中的www.iteye.com替换成替换成img.iteye.com显示
update software set icon=replace(icon,"www.iteye.com","img.iteye.com") ; 把数据库中icon的域名批量进行查找替换 www.iteye.com
mysql in函数
可批量指定几个值作为查询条件
例:
select * from user where user_id in(1,2,3,4,5,100,200,333)
select * from user where user_name in("a","b","d")
mysql like函数
可对某个字段进行模糊查询,"%"号用于匹配任意字符
例:
select * from user where name like "%王%"; 查询所有用户名中带"王"字符的用户
select * from user where name like "%王"; 查询所有用户名第一个字符为"王"字的用户

mysql is null函数
匹配某个字符为null值的记录,注:null不代表空符串""
例:
select * from user where a is null ; 查询a字段为null的用户
select a.* from user as a left join add_user as b on a.user_id=b.user_id where b.user_id is null; 连表查询附加表add_user中没有附加用户信息数据的用户

mysql is not null函数
和is null用法一样,匹配某个字符不为空的记录

mysql MATCH ... AGAINST 全文匹配函数
mysql的全文匹配函数,要使用此函数查询的字符必须增加了全文索引,另外mysql不支持中文全文索引,所以国人在开发中估计很少用到此函数。
match中包含要进行全文匹配的字段,多个字段用","号分割 against为匹配的字符串
例:
select * from software where match(title,body) against("php"); 全文匹配title和body字段中包含"php"的记录
select * from software where match(title) against("php mysql"); 全文匹配title字段中包含"php mysql"的记录。