mysql数据库div函数_MySQL数据库——内置函数

MySQL数据库——内置函数

建表并插入数据create table student(id char(36) primary key,name varchar(8) not null,age int(3) default 0,mobile char(11),address varchar(150))

插入数据insert into student values ('9b4435ec-372c-456a-b287-e3c5aa23dff4','张三',24,'12345678901','北京海淀');insert into student values ('a273ea66-0a42-48d2-a17b-388a2feea244','李%四',10,'98765432130',null);insert into student values ('eb0a220a-60ae-47b6-9e6d-a901da9fe355','张李三',11,'18338945560','安徽六安');insert into student values ('6ab71673-9502-44ba-8db0-7f625f17a67d','王_五',28,'98765432130','北京朝阳区');insert into student values ('0055d61c-eb51-4696-b2da-506e81c3f566','王_五%%',11,'13856901237','吉林省长春市宽平区');

6477d8602e8e04488fdbf539445f85e8.png

内置函数

在MySQL中已经定义过的函数,具有特定的功能和作用。

将SQL函数分为单行函数和多行函数:单行函数:单行函数仅对单条数据中的列进行操作并且返回一个结果

多行函数:多行函数可以操作成组的多条数据,每组返回一个结果,所以多行函数又称之为组函数

单行函数

字符串函数

length(column_name|str)返回字符串存储长度select name, length(name) from student;

mysql数据库div函数_MySQL数据库——内置函数_第1张图片

注意:一个中文字符占三个长度

char_length(column_name|str)返回字符串中字符个数select name,char_length(name) from student;

mysql数据库div函数_MySQL数据库——内置函数_第2张图片

这里需要注意和length函数的区别

concat(column_name1|str1, column_name2|str2,…)将多个字符串首尾相连后返回select concat(id,':',name) "个人信息" from student;

2ee397809c04daba3e3ca4a6628fc601.png

concat_ws(separator,column_name1|str1, column_name2|str2,…)将多个字符串按照执行separator进行首尾相连;select concat_ws(':',id,name,age,address) from student;

d0c9a0fe5701c960aee82c98a43e74e0.png

trim(str)返回去掉str源字符串两端、前缀或后缀字符串select trim(name) from student;

mysql数据库div函数_MySQL数据库——内置函数_第3张图片

因为这里插入的数据没有全面面是空格的数据,所以效果不太明显。

substr(str,pos[,len])从源字符串str中的指定位置pos开始取一个字串并返回select substr('abcdefg',2) from dual;

864b61bc6005a52e19474cf831e97d79.pngselect substr('abcdefg',2,4) from dual;

fc9dbbe45116c2081ed21e643c4e464d.png

replace(str, from_str, to_str)将源字符串str中所有子串form_str(大小写敏感替代成字符串to_str并返回替换后的字符串select replace(name,'%','@') from student;

mysql数据库div函数_MySQL数据库——内置函数_第4张图片

这里可看出所有的%符号都被替换成了@符号

reverse(str)返回字符串str反转结果select reverse(name) from student;

mysql数据库div函数_MySQL数据库——内置函数_第5张图片

strcmp(expr1,expr2)两个字符串相同则返回0;第一个小于第二个返回-1,否则返回1select name,strcmp(name,'王五') from student;

mysql数据库div函数_MySQL数据库——内置函数_第6张图片select name,strcmp(name,'张三') from student;

mysql数据库div函数_MySQL数据库——内置函数_第7张图片

注意:当插入的数据前面相同,后面有空格的时候也是返回0的

比如这里的数据

数值函数

mod(x,y)取x与y的余数

57a4dab8c1350710ed8d142a3e0f5309.png

这里的张李三后面有空格select name,strcmp(name,'张李三') from student;

mysql数据库div函数_MySQL数据库——内置函数_第8张图片

可看到张李三对应的返回值为0

round(x[,y])返回参数x的四舍五入值,该值有y位小数;不指定第二个参数,则默认为0select round(1.234);

1844ec7bdaaaa8e0a6a84d1a99fc2e64.pngselect round(1.534);

2bb202547774a61aaf8e21a1a6b821ee.pngselect round(1.234,1);

660df1fd0fc84b71a41a174eab1ed951.pngselect round(1.255,2);

de6d4ccef6fe03d74e59a50906446c3c.png

truncate(x,y)返回数字x截断后的结果,该值有y位小数select truncate(1.9999,2) from dual;

fab5d2c08672b590ac2ffcf7fe345422.pngselect truncate(1.9999,0) from dual;

1a4e7b08cccd6b61734fc0e40582e945.png

日期函数

now()获得当前日期 时间select now();

54d15edb5c91125396d756ded0aad560.png

date_format(date,format)获取指定格式的日期select date_format(now(),'%Y-%m-%d %H:%i:%s');

2aeb5917a962aeea8ac1095bfedfd830.png

datediff(date1,date2)返回(date1-date2)天select datediff(now(),20190501);

42aae536af912f79c8d785740388d729.png

timediff(time1,time2)返回time1-time2select timediff(now(),20190501000000);

a2b8cc5ec801459e7cb2d3bf0a5985d3.png注意:函数的两个参数类型必须相同

转换函数

convert(value,type)将value转换为type类型,type可以是char(字符型)、date(日期型)、time(时间型)、datetime(日期时间型)、 signed(整型) 和decimal(浮点型)类型中的一个select convert('111',signed);

40e8aab49b1b31949d09b7e377ddd7ee.png

其他函数

if(expr1,expr2,expr3)expr1为TRUE,返回expr2,否则返回expr3select if(address is not null,mobile,"未知") from student;

mysql数据库div函数_MySQL数据库——内置函数_第9张图片

ifnull(expr1,expr2)expr1不是NULL,返回expr1,否则返回expr2select ifnull(address,"未知") from student;

mysql数据库div函数_MySQL数据库——内置函数_第10张图片

多行函数

多行函数又称组函数,这类函数用于对多行数据进行操作,在使用时需要注意一下几点:组函数忽略空值——可以通过ifnull函数或if(expr1, expr1, expr1)用一个值代替空值;

组函数默认考虑重复值——可以通过distinct关键字使组函数不考虑重复值;

常用 组函数(多行函数)

avg(input)求平均值,例如:select avg(age) from student;#计算学生平均年龄,包括重复的年龄select avg(age) from student;

1db2384cddd693422c7fe81b6188d73f.png

max(input)求最大值,例如:select max(age) from student——获取学生表中最大年龄select max(age) from student;

029b09c2205408f49f7bc24d025094b9.png

min(input)求最小值,例如:select min(age) from student——获取学生表中最小年龄select min(age) from student;

97995a9a6d0aaae42839f6d2f8be1159.png

sum(input)求和,例如:select sum(age) from student——计算学生表中年龄之和select sum(age) from student;

d497321ad32144d0aeb01f8be4ea7ed3.png

count(*|input)求行数,如果使用*则不会忽略空值的行,select count(address) from student;

f79a17abcffa9f8f7f22a2a211f1f1a6.png

使用*就不会忽略nullselect count(*) from student;

bbad94686bde1563c3e3bcb4dd779df6.png来源:http://www.icode9.com/content-2-186151.html

你可能感兴趣的:(mysql数据库div函数)