SQL查询之联结查询和子查询

联结查询和子查询

  通常,我们会将要存储的数据分解,存放在不同的表中,即数据库的表的设计应该满足第三范式,这样做的目的是避免存取重复的数据,保证数据的单一性。
  但这会不利于我们进行查询,使用联结查询或者子查询可以弥补这个缺点。

本次使用的ER图
这里写图片描述

子查询

把一个查询的结果作为另一个查询的数据集,这种查询的方式成为子查询
目的: 进行跨表查询。
注意: 使用子查询会严重的影响效率,尽量使用联结查询。

使用子查询进行跨表查询

 select cust_id from orders where order_num in 
(select order_num from orderitems where prod_id = 'RGAN01');
 --  结果
 -- '1000000004'
 -- '1000000005'

子查询的处理顺序是从内向外进行的,可以锲套多个子查询。

联结查询

联结查询可以替代子查询进行跨表查询,效率会有明显提高。下面介绍联结查询的几种方式:

使用where子句进行联结查询

使用where 子句进行联结查询时,列名该使用完全限定名。

select vend_name,prod_name, prod_price
from vendors,products
-- 联结条件
where vendors.vend_id = products.vend_id;

/*
    Bears R Us  8 inch teddy bear   5.99
    Bears R Us  12 inch teddy bear  8.99
    Bears R Us  18 inch teddy bear  11.99
    Doll House Inc. Fish bean bag toy   3.49
    Doll House Inc. Bird bean bag toy   3.49
    Doll House Inc. Rabbit bean bag toy 3.49
    Doll House Inc. Raggedy Ann 4.99
    Fun and Games   King doll   9.49
    Fun and Games   Queen doll  9.49
*/

跨表查询不加where进行限定条件时,此时的查询为交叉查询,即返回两个表各字段的笛卡尔积。

-- 此时会返回笛卡尔积
select vend_name,prod_name, prod_price
from vendors,products

使用内联结实现联结查询

内联结查询是比使用where查询更好的解决方案

此时返回和上面一样的结果

select vend_name ,prod_name,prod_price from  vendors
inner join products
on vendors.vend_id = products.vend_id;
/*
    Bears R Us  8 inch teddy bear   5.99
    Bears R Us  12 inch teddy bear  8.99
    Bears R Us  18 inch teddy bear  11.99
    Doll House Inc. Fish bean bag toy   3.49
    Doll House Inc. Bird bean bag toy   3.49
    Doll House Inc. Rabbit bean bag toy 3.49
    Doll House Inc. Raggedy Ann 4.99
    Fun and Games   King doll   9.49
    Fun and Games   Queen doll  9.49
*/
使用inner join 实现多表联结查询

inner join 锲套形式

select * from raw_table
– 此处的意思是使用inner join选出所有满足条件的Table1
inner join (table1 inner join table2 on table2.column2 = raw_table.column2) on raw_table.column1 = table1.column1;

select prod_name,vend_name,prod_price,quantity
from orderitems
-- 此时的inner join必须为锲套的形式
inner join (products inner join vendors on products.vend_id = vendors.vend_id)
on orderitems.prod_id = products.prod_id
where order_num = 20007;
/*
    18 inch teddy bear  Bears R Us  11.99   50
    Fish bean bag toy   Doll House Inc. 3.49    100
    Bird bean bag toy   Doll House Inc. 3.49    100
    Rabbit bean bag toy Doll House Inc. 3.49    100
    Raggedy Ann Doll House Inc. 4.99    50
*/
使用where实现多表联结查询

使用where进行多表联结查询就比较好理解了

在where子句中逐一进行匹配即可,不多说了,结果和上面一样。

select prod_name ,vend_name, prod_price,quantity
from orderitems,products,vendors
where products.prod_id = orderitems.prod_id
and vendors.vend_id = products.vend_id
and order_num = 20007;

/*
    18 inch teddy bear  Bears R Us  11.99   50
    Fish bean bag toy   Doll House Inc. 3.49    100
    Bird bean bag toy   Doll House Inc. 3.49    100
    Rabbit bean bag toy Doll House Inc. 3.49    100
    Raggedy Ann Doll House Inc. 4.99    50
*/

随后会介绍其他的联结方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值