SQL 多表连接查询

news/2024/7/24 11:23:21 标签: sql, 数据库

• First attempt: List the property numbers viewed by client number ‘CR56’:

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

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

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

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

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

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:自然连接输出属性上取值相同的元组对即有同一列名上值相等该行的数据

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

Theta Joins

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

• 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)

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

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

右连接 全连接 输出左右两边关系如果有两边条件值相同的属性则输出 如果不同输出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..


http://www.niftyadmin.cn/n/5425154.html

相关文章

前端框架发展史

前端开发自互联网诞生之初就开始了它的演变历程,从最早的静态页面到今天的高度动态和交互式的应用程序,前端框架在这个进程中起到了核心的作用。这些框架不仅极大地推进了网页技术的边界,还改善了开发者的工作流程和最终用户的体验。让我们深…

【C++】—— 代理模式

目录 (一)什么是代理模式 (二)为什么使用代理模式 (三)代理模式实现步奏 (四)代码示例 (五)代理模式优缺点 (一)什么是代理模式 …

按键顺序读写yaml文件

按键顺序读写yaml文件 依赖两个接口: ​ collections.OrderedDict ​ pip install yamlordereddictloader 1. 按照yaml文件的顺序读取到orderdict中 import yamlordereddictloader from collections import OrderedDictwith open(test.yaml, r, encodingutf-8)…

【AcWing】蓝桥杯集训每日一题Day3|差分|4262.空调(C++)

空调 4262. 空调 - AcWing题库难度:简单时/空限制:1s / 64MB总通过数:4559总尝试数:7483来源:USACO 2021 December Contest Bronze算法标签 差分贪心 题目内容 Farmer John 的 N 头奶牛对他们牛棚的室温非常挑剔。 有…

windows下mysql8.3解压安装后临时密码报1045错误解决

过程: 1.登录MySQL官网www.mysql.com,进入产品下载页面。 2.选择MySQL Community(GPL)Downloads链接跳转下载页。 3.选择MySQL Community Server。 4.选择安装方式。通常有解压包安装和步骤安装两种方式,这里选择了解…

SQL: 触发器/存储过程/游标的操作

目录 触发器存储过程创建存储过程修改存储过程删除存储过程执行存储过程 游标待续、更新中 触发器 待更新存储过程 定义 是一组TSQL语句的预编译集合,能实现特定的功能 是一种独立的数据库对象,在服务器上创建和运行 类似于编程语言中的过程或函数分类…

前端npm和yarn更换国内淘宝镜像

NPM 查询当前镜像 npm get registry 设置为淘宝镜像 npm config set registry https://registry.npm.taobao.org/ (旧地址) npm config set registry https://registry.npmmirror.com/ (最新地址) 设置为官方镜像 npm config set registry https://registry.n…

Rust 的 into_owned() 方法

into_owned 是 Rust 语言中 std::borrow::Cow 枚举的一个方法。Cow 是一个“克隆在写时”(Copy on Write)的智能指针,它可以包含对数据的引用或数据的实际所有权。这种设计模式在需要避免不必要的数据复制时特别有用,尤其是当数据…