MySql-Task03

一级目录

二级目录

三级目录

集和运算指:
在这里插入图片描述

表的加减

以下操作会导致记录行数的增减。

union-集和加法(并集)

做加法时会去除重复的记录。

select product_id,product_name from productions1
union 
select product_id,product_name from productions2

在这里插入图片描述

  • 使用 UNION 对两个查询结果取并集,和在一个查询中使用 WHERE 子句, 然后使用 OR 谓词连接两个查询条件,
    能够得到相同的结果.
  • union合并两个数据集的时候会直接去重,如果不想去重进而对数据集的每种记录计数的话,可以使用union
    all(在原来union的地方写成union all)即可。

intersect-交集

目前MySql不支持,可以在where后用and来表示交集
(两个条件都要满足,相当于交集)

except-差集补集,集和的减法运算

目前MySql不支持直接使用except表示差集和补集,但是可以用not in 来实现集和的减法
例如:找出只存在于 product 表但不存在于 product2 表的商品.
假设可以使用except:

select * from product except product2;

等效于

select * from product where product_id not in (
select product_id from product2);

连结join

如果想要从多个表中获取数据,在一张表上增加列数(如果不使用case等表达式的话),就可以使用连结 join

内连结

from tb_1 inner join tb_2 on conditions

需要找到两张表的公共列作为连接的桥梁,on语句后面就跟着公共列

select <列名1>,<列名2>,<列名3>...
from tb_1 as <别名1> 
inner join tb_2 as <别名2>  
on conditions;

KP:
1.连结的时候要使用多张表
2.on后面要有连结的条件
3.select中的列最好按照<表名.列名>来使用,避免遇到重名的时候会起冲突。
4.where子句如果要删选某些行的话,where要放在on子句的后面

  • 当有内连结的时候,where的使用方法:

可以先将内连结的表作为一种子查询,再从子查询的结果中再次查询
eg

select * from
(select <>,<>,<> from <> inner join <> on conditions)
where conditions;

而事实上,where子句会在from子句之后执行,也就是说在做完inner join… on 得到一个新表之后,才会执行where子句,那么就得到标准的写法。

select <>,<>,<> from <> inner join <> on conditions
where conditions;

(不建议使用)
还可以在连结条件on后面直接跟着需要的筛选条件,就是说把原本应该放在where后面的条件直接加到on后面,不过需要将这些连结条件和筛选条件括起来。

如果需要将多个表连接起来的话,或者筛选条件比较复杂的话,可以将任务分割,分成两个子查询,再将子查询连结。

select <>,<>,<> from (子查询1) inner join (子查询2);

这里的子查询又可以用select语句了。

  • 当有内连结的时候,group by的使用方法:
    如果分组列在同一个表上,在内连结之前就可以是使用group by;
    如果分组列不在同一个表上,只能先进行内连结在去做分组 group by;

可以用内连结来解决关联子查询问题。
(以下代码用关联子查询解决:求出高于类均值的产品)

select P1.product_id,
	   P1.product_name,
       P1.product_type,
       P1.sale_price,
       P2.avg_price
from productions as P1
inner join(select product_type,avg(sale_price) as avg_price
from productions
group by product_type) as P2
on P1.product_type = P2.product_type
where P1.sale_price > P2.avg_price;

(以下代码用内连结查询解决同样的问题)

select 	P1.product_id,
		P1.product_name,
        P1.product_type,
        P1.sale_price,
        P2.avg_price
from productions as P1
inner join 
(select product_name,
		product_type,
		sale_price,
        product_id,
        avg(sale_price) as avg_price
from productions 
group by product_type) as P2
on P1.product_id =P2.product_id
where P2.avg_price <= P1.sale_price;

on语句后面接着 内外关联的语句即可;

自然连结(内连结的一种)

当两个表进行自然连结是,会按照两个表都包含的列名来进行等值内连结,无需用on来指定连接条件。

外连结

KP:
①选取单张表中的全部信息。

-- 左连结 
select * from <tb_1> left outer join <tb_2> on <conditions>
-- 右连结 
select * from <tb_1> right outer join <tb_2> on <conditions>
-- 全外连结 
select * from <tb_1> full outer join <tb_2> on <conditions>

左连结那就是把左边表的所有信息都取出来
②使用left right来指定主表,使用左还是右没有区别,都可以,大不了换一下表的位置。

全外连结还不能做到,可以用左外连结 union 右外连结
可以同时进行多个表的外连结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值