本文整理自网络,侵删。
目录
- 一、MySQL进阶查询
- 二、MySQL数据库函数
- 三、MySQL存储过程
- 总结
一、MySQL进阶查询
首先先创建两张表
mysql -u root -pXXX #登陆数据库,XXX为密码 create database jiangsu; #新建一个名为jiangsu的数据库 use jiangsu; #使用该数据库 create table location(Region char(20),Store_name char(20)); #创建location表,字段1为Region,数据类型为char,数据长度为20;字段2为Store_name,数据类型为char,长度为20. insert into location values('North','Xuzhou'); #插入4条数据 insert into location values('North','Suqian'); insert into location values('South','Nanjing'); insert into location values('South','Suzhou'); create table store_info(Store_name char(20),Sales int(10),Date char(10)); #再创建一张store_info表 insert into store_info values('Xuzhou',300,'2020-12-08'); #插入4条数据 insert into store_info values('Suqian',249,'2020-12-07'); insert into store_info values('Nanjing',1500,'2020-12-05'); insert into store_info values('Suzhou',700,'2020-12-08');
----------------select-------------------
select用于查询表格中的一个或多个字段的数据记录
语法格式:select 字段1,字段2,... from 表名;
select * from location; #查询location表中的所有字段,*表示所有,如果不嫌麻烦,当然也可以将所有字段都输进去
select Region from location; #查询location表中的Region的数据
select sales,Date from store_info; #查询store_info表中的sales,Date字段的数据----------------DISTINCT-------------------
DISTINCT用于将重复的数据压缩为一个
语法格式:select distinct 字段名 from 表名;
select distinct Store_name from store_info; #查询dtore_info表中的Store_name字段的数据,如有重复,则只显示一个----------------where-------------------
where用于带条件查询
语法格式:select 字段名 from 表名 where 条件语句;
select Store_name from store_info where sales=300; #查询store_info表中sales字段的值等于300的Store_name的数据
select Store_name from store_info where sales>500; #查询store_info表中sales字段的值大于500的Store_name的数据----------------and or-------------------
and,且,用于查询一个数据范围;or,或,用于查询包含条件语句的所有数据
语法格式:select 字段名 from 表名 where 条件1 and/or 条件2;
select Store_name from store_info where sales>250 and sales<1000; #查询store_info表中sales字段的值大于250,且小于1000的Store_name的数据
select Store_name from store_info where sales<250 or sales>1000; #查询store_info表中sales字段的值小于250,或者 大于1000的Store_name的数据
select Store_name from store_info where sales>1000 or (sales >200 and sales < 500); #括号的优先级高,所以先根据括号里的and条件语句进行筛选,然后再根据or进行筛选,最后查询最终筛选出来的数据;该语句先筛选出sales大于200且小于500的值,再使用or进行或的删选,最终筛选出来的结果应该是在200到500之间的值或者大于1000的值。
select Store_name from store_info where sales>1000 or sales >200 and sales < 500; #如果不加括号,and的优先级是比or要高的,也就是说,当一条条件语句中同时存在and和or(没有括号),会先执行and条件。----------------in-------------------
in用来显示已知值的数据,简单来说,in后面跟的是一个数据集合,查询语句会根据数据集合中的值进行筛选查询。not in 就是取数据集合中的反,不在数据集合中的数据。
语法格式:select 字段名1 from 表名 where 字段名2 in ('字段名2的值1','字段名2的值2,......');
select * from store_info where Store_name in ('Nanjing','Xuzhou'); #将Nanjing和Xuzhou的所有信息都查询出来。
注:in可以用or代替
上述语句等于:select * from store_info where Store_name='Nanjing' or Store_name='Xuzhou';----------------between...and-------------------
between 值1 and 值2 ,在值1与值2之间(值2 > 值1),该语句查询的是一个范围,包含值1和值2。其作用相在某一方面当于大于等于 ... and 小于等于 ... 。
语法格式:select 字段名 from 表名 where 字段名 between 值1 and 值2;
select * from store_info where Date between '2020-12-07' and '2020-12-10'; #查询store_info表中的Data的值在12-06与12-10之间的所有数据----------------通配符-------------------
通配符一般情况下和like一起使用进行模糊查询,模糊查询的概念就是将所有符合条件的数据全部查询出来,而等于号是精确查询,会直接将具体的某一数据查询出来
模糊查询的字符如下:
%:百分号表示0个,1个或多个字符
_:下划线表示单个字符
语法格式:select 字段 from 表名 where 字段 like '通配符';
select * from store_info where Date like '2020%'; #将Date的值为2020,后面随便(2020后有没有都行)的值全部查询出来
select * from store_info where Date like '2020-12-0_'; #将2020-12-0,后面只能匹配一个字符(必须存在且只能有一个)的所有数据查询出来----------------like-------------------
like,模糊查询,用于查询符合条件的所有数据,通常和通配符一起使用,语法和通配符一样的,因为是结合使用。
create database name;
use name;
create table stu_name(sname char(10));
insert into stu_name values('张');
insert into stu_name values('张三');
insert into stu_name values('张四');
insert into stu_name values('张无忌');
insert into stu_name values('一张纸');
insert into stu_name values('弓长张');
select * from stu_name where sname like '张%'; #查询所有张姓的名字,只要姓张就行
select * from stu_name where sname like '%张'; #查询所有最后一个字是张的姓名,前面无所谓
select * from stu_name where sname like '%张%'; #查询所有包含张的姓名,张字在姓在名都行
select * from stu_name where sname like '张_'; #查询所有张姓且只有两个字的名字
select * from stu_name where sname like '张__';(两条下划线) #查询所有张姓,且必须为三个字的名字
select * from stu_name where sname like '_张%'; #查询所有第二个字为张的名字
select * from stu_name where sname like '张_%'; #查询所有张姓,名字至少包含两个字的名字
select * from stu_name where sname like '张%_'; #查询所有张姓,名字至少包含两个字的名字,该语句和上面的查询结果一样,但理解是不同的。----------------order by-------------------
order by 用于关键字的排序
语法格式:select 字段 from 表名 [where 条件语句] order by 字段 asc/desc;
asc:按字段升序,默认为asc
desc:按字段降序
select Store_name,Date,sales from store_info order by sales; #按照sales升序排列后,查询name、date和sales
select Store_name,Date,sales from store_info order by sales desc; #按照sales降序排列后,查询name、date和sales
二、MySQL数据库函数
-------------数学函数-------------------
abs(x) #返回x的绝对值
rand() #返回0到1之间的随机数
mod(x,y) #返回x除以y的余数
power(x,y) #返回x的y次方
round(x) #返回离x最近的整数
round(x,y) #保留x的y位小数四舍五入之后的值
sqrt(x) #返回x的平方根
truncate(x,y) #返回x截断为y位小数的值
ceil(x) #返回大于或等于x的最小整数
floor(x) #返回小于或等于x的最大整数
greatest(x,y,z,...) #返回集合中最大的值
least(x,y,z,...) #返回集合中最小的值
---------------------------------------------
select abs(-1),rand(),mod(5,2),power(2,3),round(1.75);
select round(3.1415926,5),sqrt(2),truncate(3.141592653,4),ceil(5.2),floor(3.2),greatest(1.61,2.54,0.87),least(5.23,8.71,4.13);
--------------聚合函数--------------------
avg() #返回指定列的平均值
count() #返回指定列中非空值的个数
min() #返回指定列的最小值
max() #返回指定列的最大值
sum(x) #返回指定列的所有值的和
------------------------------------------
select avg(sales) from store_info; #查询sales的平均值
平均值的另一种方法:
select sum(sales)/(select count(sales) from store_info) from store_info;select count(Date) from store_info; #统计Date的数据个数,包括重复的值,但不包括空值
select count(distinct Date) from store_info; #统计Date的数据个数,重复的数据只统计一次,不包括空值
select count(*) from store_info; #全部统计,包括空值,count(*)扫描全表
select min(sales) from store_info; #查询sales的最小值
最小值的另一种方法:
select sales from store_info order by sales limit 1;select max(sales) from store_info; #查询sales的最大值
最大值的另一种方法:
select sales from store_info order by sales desc limit 1;select sum(sales) from store_info; #查询sales的和
-----------------字符串函数--------------------
trim() #返回去除指定格式的值
concat(x,y) #将提供的参数x和y拼接成一个字符串
substr(x,y) #获取字符串x中第y个位置开始的字符串,跟substring()函数作用相同
substr(x,y,z) #获取字符串x中第y个位置开始,长度为z的字符串
length(x) #返回字符串x的长度
replace(x,y,z) #将字符串z替代字符串x中的字符串y
upper(x) #将字符串x中的所有字母变成大写
lower(x) #将字符串x中的所有字母变成小写
left(x,y) #返回字符串x中的前y个字符
right(x,y) #返回字符串x中的后y个字符
repeat(x,y) #将字符串x重复y次
space(x) #返回x个空格
strcmp(x,y) #比较x和y,返回的值可以为-1,0,1
reverse(x) #将字符串x反转select concat(Region,Store_name) from location where Store_name='Xuzhou'; #将location表中Store_name='Xuzhou'的Region,Store_name的值拼接在一起
select Region || ' ' || Store_name from location where Store_name='Xuzhou'; #在my.cnf中开启了PIPES_AS_CONCAT模式后,可以使用 || 代替concat函数,将多个字符串拼接在一起
select substr(Store_name,3) from store_info where Store_name='Suqian'; #将Suqian的第3个字符往后的所有字符截取出来
select substr((select Region || ' ' || Store_name from location where Store_name='Xuzhou'),1,5); #将上一条拼接的字段的第1到5个字符截取出来
select trim([[位置] [要除移的字符串] from] 字符串);
#位置:leading(开头),tariling(结尾),both(开头及结尾)
#要除移的字符串:从字符串的开头、结尾,或开头及结尾除移的字符串,缺省时为空格
select trim(leading 'Xu' from 'Xuzhou'); #将Xuzhou开头的Xu去掉select Region,length(Store_name) from location; #查询location表中的Region字段的值和Store_name的值的长度
select replace(Region,'th','thern') from location; #将location表中的Region字段的值包含th的替换为thern,然后返回。
------------------group by-------------------
group by用于对查询结果进行汇总分组,通常是结合聚合函数
一起使用,group by有一个原则,凡是在group by后面出现的
字段,必须在select后面出现。凡是在select后面出现、且未在
group by后面出现的字段,必须出现在group by后面。
语法格式:select 字段1,sum(字段2) from 表名 group by 字段1;select Store_name,sum(sales) from store_info group by Store_name order by sales;
------------------having----------------------
having用来过滤由group by语句返回的记录值,通常与group by语句联合使用。having语句的存在弥补了where关键字不能与聚合函数联合使用的不足。
语法格式:select 字段1,sum(字段2) from 表名 group by 字段1 having (函数条件);
select Store_name,sum(sales) from store_info group by Store_name having sum(sales) >1000;相关阅读 >>
更多相关阅读请进入《sql》频道 >>
![]()
书籍 数据库系统概念 第6版
机械工业出版社
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
转载请注明出处:木庄网络博客 » MySQL一些常用高级SQL语句详解
标签:sql相关推荐
评论
管理员已关闭评论功能...