SQL高级语句(包含子查询,连接查询,视图联集等)
Posted 老张学coding
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL高级语句(包含子查询,连接查询,视图联集等)相关的知识,希望对你有一定的参考价值。
文章目录
三、SQL高级语句
3.1、gruop by
作用:对group by 后面的栏位的查询结果进行汇总分组,通常结合聚合函数一起使用
注意:group by有一个原则,就是select后面的所有列中,没有使用聚合函数的列,必须出现在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 desc ;
3.2、having
作用:having用来过滤由group by 语句返回的记录集,通常与group by 连用
注意:having 语句的存在弥补了where 关键字不能与聚合函数联合使用的不足。如果被select 的只有函数栏,那么就不需要group by子句
语法说明:
select '栏位1',sum('栏位2') from '表格名' group by '栏位1' having (函数条件);
案例:
select Store_Name,sum(Sales) from Store_Info group by Store_Name having sum(Sales) >1500;
3.3、别名(栏位别名,表格别名)
语法说明:
select 表格别名.栏位1 [as] 栏位别名 from 表格名 [as] 表格别名;
案例:
select A.Store_Name store,sum(A.Sales) 'Total Sales' from Store_Info A group by A.Store_Name;
3.4、子查询
作用:连接表格,在where 或having 子句中插入另一个SQL语句
语法说明:
select "栏位1" from "表格1" where "栏位2" [比较运算符] #外查询
(select "栏位1" from "表格2" where "条件");#内查询
#可以是符号的运算符,例如:=,>,<,>=,<= ;也可以是文字运算符,例如:like in between
案例1:
select sum(Sales) from Store_Info where Store_Name in (select Store_Name from location where Region='West');
案例2:
select sum(A.Sales) from Store_Info A where A.Store_Name in (select Store_Name from location B where B.Store_Name =A.Store_Name);
3.5、exists
作用:用来测试内查询有没有产生任何结果,类似布尔值是否为真,如果有的话,系统就会执行外查询中的SQL语句,如果没有的话,那么整个SQL语句就不会产生任何结果
语法说明:
select "栏位1" from "表格1" where exists (select * from '表格2' where '条件');
select sun(Sales) from Store_Info where exists (select * from location where Region='West');
防止空跑SQL语言,减少占用空间
案例:
select sum(Sales) from Store_Info where exists (select * from location where Region='West');
3.6、连接查询
update Store_Info set Store_Name ='Washingtion' where sales=300;
inner join(内连接):只返回两个表中联结字段相等的记录
left join(左连接):返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右连接):返回包括右表中的所有记录和左表中联结字段相等的记录
案例1:
select * from location A,Store_Info B where A.Store_Name=B.Store_Name;
#和内连接查询结果相同
案例2:
select A.Region REGION,sum(B.Sales) SALES from location A,Store_Info B where A.Store_Name=B.Store_Name group by REGION;
① inner join (内连接)
select * from location A inner join Store_Info B on A.Store_Name=B.Store_Name;
②left join (左连接)
select * from location A left join Store_Info B on A.Store_Name=B.Store_Name;
③right join (右连接)
select * from location A right join Store_Info B on A.Store_Name=B.Store_Name;
3.7、视图(view)
作用:可以被当作是虚拟表或存储查询
视图与表格以及临时表比较的特点:
#视图和表格的不同是,表格中有实际存储资料,而视图时建立在表格之上的一个架构,它本身并不实际存储资料
#临时表在用户退出或同数据库的链接断开后就自动消失,,而视图不会消失
#视图不含有数据,值存储它的定义,他的用途一般可以简化负载的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写SQL语句会很麻烦,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便
注意:查出来的不能有重复值,不能用’*’
语法说明:
create view '视图表名' as 'select语句';
案例:
create view V_REGION_SALES as select A.Region REGION,sum(B.Sales) SALES from location A
inner join Store_Info B on A.Store_Name=B.Store_Name group by REGION;
select * from V_REGION_SALES;
drop view V_REGION_SALES;
注意:
-
视图表不用于存储数据,而是用来查询
-
可以用show tables查看表
3.8、union联集
① union
作用:
-
将两个SQL语句的结果合并起来,两个SQL语句所产生的栏位需要是同样的资料种类
-
生成结果的资料值将没有重复,且按照字段的顺序进行排序
语法说明:
[ select 语句1 ] union [select 语句2 ];
案例:
select Store_Name from location union select Store_Name from Store_Info;
② union all
作用:将生成的结果的资料值都列出来,无论有无重复
语法说明:
[ select 语句1 ] union all [select 语句2 ];
案例:
select Store_Name from location union all select Store_Name from Store_Info;
3.9、交集值,取两个SQL语句结果的交集
案例1:
select A.Store_Name from location A inner join Store_Info B on A.Store_Name=B.Store_Name ;
select A.Store_Name from location A inner join Store_Info B using(Store_Name);
案例2:
#两表其中的一个表没有指定的行,而另一个表这个行有重复不适用,要求两个确实有交集的时候用
select A.Store_Name from (select Store_Name from location union all select Store_Name from Store_Info ) A
group by A.Store_Name having count(*) > 1;
注意:这种方式的前提条件是两表其中的一个表没有指定的行,而另一个表这个行有重复不适用,要求两个表确实有交集的时候用
案例3:取两个SQL语句结果的交集,且没有重复
#取两个SQL语句结果的交集,且没有重复
select A.Store_Name from (select B.Store_Name from location B inner join Store_Info C on B.Store_Name=C.Store_Name ) A group by A.Store_Name ;
select distinct A.Store_Name from location A inner join Store_Info B using(Store_Name);
select distinct Store_Name from location where (Store_Name) in (select Store_Name from Store_Info);
select distinct A.Store_Name from location A left join Store_Info B using(Store_Name) where B.Store_Name is not null;
3.10、无交集值,显示第一个SQL语句的结果,且与第二个SQL语句没有交集的结果,且没有重复
①用distinct
案例:
select distinct Store_Name from location where (Store_Name) not in (select Store_Name from Store_Info);
②用左连接查询
案例:
select A.Store_Name from location A left join Store_Info B using(Store_Name) where B.Store_Name is null;
③用联集union all
案例:
select A.Store_Name from (select distinct Store_Name from location union all select distinct Store_Name from Store_Info) A group by A.Store_Name having count(*)=1;
3.11、case
作用:是SQL用来作为 if,then,else之类逻辑的关键字
语法说明:
select case ('栏位名')
where '条件1' then '结果1'
where '条件2' then '结果2'
where '条件3' then '结果3'
........
[else '结果N']
end
from '表名';
#条件可以是一个数值或者公式。else子句不是必须的
案例:
select Store_Name ,case Store_Name
when 'Los Angeles' then Sales * 2
when 'Boston' then Sales * 1.5
else Sales
end
'New Sales' ,Date
from Store_Info;
#'New Sales' 是用于case那个栏位的栏位名
以上是关于SQL高级语句(包含子查询,连接查询,视图联集等)的主要内容,如果未能解决你的问题,请参考以下文章