MySQL(六)运算符与函数

1.运算符
+加法、-减法、*乘法、/除法、%取余、
在这里插入图片描述
比较运算符
在这里插入图片描述
查看年龄在19到21之间的
在这里插入图片描述
查看以姓张开头名字是两个字的信息
在这里插入图片描述
查看电话以135开头的信息
在这里插入图片描述
逻辑运算符
and与、or或、not非
查询地址在北京、并且年龄大于18的信息
在这里插入图片描述
2.数值函数
2.1
ceil(x):获取返回大于x的最小整数值。
列:select ceil(28.5);
结果:29
2.2
floor(x):返回小于x的最大整数值。
列:select floor(28.5);
结果:28
2.3
round(x):返回最接近于x的整数,对x进行四舍五入。
列:select round(28.55);
结果:29
round(x,y):返回接近于x的参数,其值保留到小数点后的y位,若y位负值,则保留到小数点左边y位。
列:select round(28.55,1),round(28.55,0),round(28.55,-1);
结果:28.6 、29、30
2.4截断函数
truncate(x,y):返回被舍去至小数点后y位的数字x。若y值为0,则结果为整数、若y的值为负数,则截去x小数点左起第y位开始后面所有地位的值。
列:select truncate(28.55,1),truncate(28.55,0),truncate(28.55,-1);
结果:28.5、28、20
2.5
mod(x.y):返回x被y除后的余数
列:select mod(11,2);
结果:1
3.字符函数
3.1
concat(s1,s2…):返回结果为连接参数产生的字符串,如果任何一个参数为null,则返回结果null;
列:select concat(‘hello’,‘world’);
结果:helloworld
concat_ws(x,s1,s2…):第一个参数x是其他参数的分隔符,如果分隔符为null,则结果为null。
列:select concat_ws(’-’,‘hello’,‘world’);
结果:hello-world
3.2字母转换大小写
lower(str):将str中的字母字符全部转换成小写字母。
列:select lower(‘Hello World’);
结果:hello world
upper(str):将str中的字母字符全部转换成大写字母。
列:select upper(‘Hello World’);
结果:HELLO WORLD
3.3求字符串长度
length(str):返回值为字符串的字节长度。
列:select length(‘hello’);
结果:5
3.4删除空格
ltrim(str):删除字符左侧空格、返回字符串str。
rtrim(str):删除字符右侧空格、返回字符串str。
trim(str):删除字符两侧空格、返回字符串str。
3.6截取字符串
substring(str,n,len):str需要截取的字符串,n起始位置,len截取的长度。n如果是负数,则子字符串位置起始于字符串结尾的n个字符。
列:select substring(‘hello world’,1,5);
结果:hello
列:select substring(‘hello word’,-3,2);
结果:rl
3.7获取指定长度字符串
left(s,n):返回字符串s开始的最左边n个字符。
列:select left(‘hello world’,5);
结果:hello
right(s,n):返回字符串中最右边n个字符。
列:select right(‘hello world’,5);
结果:world
3.8替换函数
replace(str,from_str,to_str):str原字符串from_str要被替换的字符串to_str被替换的字符串。
列:select replace(‘hello world’,‘world’,‘mysql’);
结果:hello mysql
3.9格式化函数
format(x,n):将数字x格式化,并以四舍五入的方式保留小数点后n位,结果以字符串的形式返回。若n为0,则返回结果不含小数部分。
列:select format(1234.5678,2),format(1234.5,2),format(1234.5678,0);
结果:1234.57,1234.50,1235

猜你喜欢

转载自blog.csdn.net/qq_38904347/article/details/83099278
今日推荐