SQL 多表连接查询

6048fefde5a342cfb4a7e5c75047ffb6.png• First attempt: List the property numbers viewed by client number ‘CR56’:c29f81bb05574de3b508ecfdb6ffa1fa.png49769b6b588b4cf8bea5302bdb75c7dd.png

• But we’d like to see the client name & property details • So we’ll need to access Client and PropertyForRent for names etc...

243583cbbfe8498ca7e4619001c8106d.png

• We now have two table names in the FROM clause • Note use of Table.ColumnName to avoid ambiguity in column names 使用Table.ColumnName来避免重名e3f934698e3d47f8bd40eba48a715560.png

• The two AND clauses are called join criteria 两个AND子句成为连接条件

• Users shouldn’t have to know about internal keys...f253bb5b1b1946efba9d76e615d80331.png

Using Table Aliases • Table aliases can help reduce amount of typing • The following is identical to the previous query:可以使用表的简称来减少字符数387c1aee066844e4985a32f12145bcac.png

FROM 表名 表简称

Natural Joins • Natural joins implement relationships in the Relational model • The DBMS will know which columns are key columns • The following is equivalent to the previous query:a9e0feae632245e78e0efa0ad60ae4fb.png自然连接输出属性上取值相同的元组对即有同一列名上值相等该行的数据5dc290eadef440229d664ff90a2313eb.png

Cross Joins (Cartesian Products)交叉连接 • Cartesian Product: a join with no WHERE clause SELECT * FROM Left, Right;6abafb16ac3a435688efeca5641c9ee3.png8adeda6346594c38ae361557686b1776.png• Useful for: — query optimisation — data mining 优化查询和数据挖掘 将左右两边数据依次关联 左边第一行数据对应右边第一直到第n行数据 左边第二行直到第n行数据依次类推

 

Theta Joinsf8d4d3aa52c14b97b589773604223e15.png

• For all clients, list the properties that each client can afford to rent:703b60da70c240748c773298434aaad3.png

• The DBMS could implement theta joins by: — First forming a cross join to give... — An intermediate (Cartesian product) table... — Then applying WHERE clause to find matching rows

Self-Joins • Sometimes it is useful to join a table to itself (must use aliases)221ad02a917c47108ee87263fdccfdde.png

在使用自连接时,必须将使用表的两个不同的简称

Outer Joins – Selecting Unmatched Rows • Example: List the branch offices and properties in the same city, along with any unmatched branches:c005ed9bd86a49b29e4710310be6c366.png外连接的左连接 以左边为主 输出左边关系以及右边与左边条件值相同的属性 如果不同则输出NULL

33bfb25ca156446ba89a3f0f2952ff71.png

右连接 全连接 输出左右两边关系如果有两边条件值相同的属性则输出 如果不同输出NULL

Why So Many Types of Join? • Theta join – a join using a simple WHERE predicate • Equi join – a special case of theta join (= predicate) • Natural join – special case of equi join (match keys) • Semi join – theta join that outputs from just one table • Self join – joining a table to itself • Outer join – a join that may include unmatched rows • Cross join – Cartesian products (no predicates) • Question: Why do we need to distinguish so many different types of join? • Answer: Queries with different joins are often optimised differently..

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值