多表查询 —— 内连接&外连接&子查询
连接查询

内连接
1、查询语法
-- 隐式内连接
select 字段列表 from 表1, 表2... where 条件; -- 显式内连接
select 字段列表 from 表1 [INNER] join 表2 on 条件;
(1)隐式内连接
-- 隐式内连接
select * from emp, dept where emp.dep_id = dept.did;
(2)显式内连接
-- 显式外连接
select * from emp inner join dept on emp.dep_id = dept.did;

多表查询中要查询单独字段时 —— 使用 emp.id emp.name 这种格式 若表名称比较长,需要起别名
-- 查询 name, gender, salary, dname
select emp.name, emp.gender, emp.salary, dept.dname from emp, dept where emp.dep_id = dept.did; -- 给表起别名
select t1.name, t1.gender, t1.salary, t2.dname from emp t1, dept t2 where t1.dep_id = t2.did;

外连接
1、 外连接查询语法
-- 左外连接
select 字段列表 from 表1 left [outer] join 表2 on 条件 -- 右外连接
select 字段列表 from 表1 right [outer] join 表2 on 条件
左外连接:
-- 左外连接
-- 查询emp表所有数据和对应的部门信息
select * from emp left outer join dept on emp.dep_id = dept.did;

右外连接:
-- 右外连接
-- 查询dept表所有数据和对应的员工信息
Select * from dept right outer join emp on dept.did = emp.dep_id;

子查询
1、 概念: 查询中嵌套查询,称嵌套查询为子查询
2、 子查询根据查询结果不同,作用不同
单行单列:作为条件值,使用 = 、!= 、>、< 等进行条件判断
select 字段列表 from 表 where 字段名 = ();
例:
-- 查询工资高于 赵六 的员工信息
select * from emp where salary > (select salary from emp where name = '赵六');
多行单列:作为条件值,使用 in 等关键字进行条件判断
select 字段列表 from 表 where 字段名 in (子查询);
例:
-- 查询‘财务部‘和‘运营部‘所有的员工信息
select * from emp where dep_id in(select did from dept where dname = '财务部' or dname = '运营部');
多行多列:作为虚拟表
select 字段列表 from (子查询) where 条件;
例:
-- 查询入职日期是‘2015-11-11‘之后的员工信息和部门信息
select * from emp where join_date > '2015-11-11'; -- ① select * from emp LEFT OUTER JOIN dept on emp.dep_id = dept.did; -- 左外连接 ②
select * from emp, dept where emp.dep_id = dept.did; -- 隐式内连接③
-- 合二为一 ① ② 或① ③ 替换掉 emp
select * from (select * from emp where join_date > '2015-11-11') t1 LEFT OUTER JOIN dept on t1.dep_id = dept.did; -- (左外连接)
select * from (select * from emp where join_date > '2015-11-11') t1, dept where t1.dep_id = dept.did; -- (隐式内连接)
多表查询 —— 内连接&外连接&子查询的更多相关文章
- mysql数据库中的多表查询(内连接,外连接,子查询)
用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...
- mysql(4)—— 表连接查询与where后使用子查询的性能分析。
子查询就是在一条查询语句中还有其它的查询语句,主查询得到的结果依赖于子查询的结果. 子查询的子语句可以在一条sql语句的FROM,JOIN,和WHERE后面,本文主要针对在WHERE后面使用子查询与表 ...
- 知识点:Oracle+表连接方式(内连接-外连接-自连接)+详解 来自百度文库
Oracle 表之间的连接分为三种: 1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右 ...
- sql内连接外连接自然连接
为什么我们要使用内连接和外连接呢?可以从两张或者多张表中找出,我们需要的属性. 这个比较好:http://www.cnblogs.com/youzhangjin/archive/2009/05/22/ ...
- MySQL 查询语句--------------进阶7:子查询
#进阶7:子查询 /* 含义: 出现在其他语句中的select语句,称为子查询或者内查询 外部的查询语句,称为主查询或外查询 分类: 按照子查询出现的位置: select后面:只支持标量子查询 fro ...
- DQL---连接查询(内连接、外连接)、子查询、分页查询
一.连接查询 1.连接查询建立在有相互关系的两个表间,进行两个及两个以上的表或视图的查询. 2.对n张表进行查询,至少需要n-1个连接表的条件. 二.笛卡尔积(容易造成数据库宕机) 1.指表中每行元素 ...
- 【cl】多表查询(内、外连接)
交叉连接(cross join):该连接产生的结果集笛卡尔积 a有7行,b有8行 a的第一行与b的每一行进行连接,就有8条a得第一行 7*8=56条 select a.real_name,s.u ...
- oracle 内连接 外连接 查询 笔记
elect ename,job,sal from emp where deptno>10 order by sal desc; 联合查询,PK dept.deptno FK emp.deptno ...
- 【SQL】多表查询中的 外连接 ,on,where
先简单粗暴给个结论,多表连结查询中,on比where更早起作用,系统首先根据各个表之间的联接条件,把多个表合成一个临时表后,再由where进行匹配过滤,where后语句为真,则能查询出来,而通过外连接 ...
- mysql8必知必会7 连接 内连接 外连接 交叉连接
/* 2 需求:建立产品和分类表 1.查询每种分类的产品数量,没有产品的分类也要统计.(cname,quantity) 2.根据分类名称查询分类中的所有产品 */ -- --------------- ...
随机推荐
- Django 自带忘记密码,密码重置功能
registration/password_reset_form.html: 重置密码表单模板 registration/password_reset_email.html: 发送重置密码邮件模板 r ...
- [oeasy]python0131_[趣味拓展]各种符号_汉语拼音符号_中文全角英文字母_中文全角标点
各种符号 回忆上次内容 中文字符可以有各种分类方法 声母 拼音检字法 韵母 合辙押韵的分类 偏旁部首 实际上unicode的排序方法 添加图片注释,不超过 140 字(可选) ...
- [oeasy]python0007-Guido的简历
执行 esc 退回到正常模式 编辑 esc退出插入模式 准备底行命令模式运行当前py文件 保存执行 :w|!python3 % 保存并用 python3 解释当前程序(%) 编辑 可以 ...
- 软件设计 软件设计模式之SOLID原则
软件设计模式之SOLID原则 By:授客 QQ:1033553122 #单一职责原则(SRP) 定义:任何一个软件模块都只对某一类行为者负责 说明:这里"软件模块",在大部分情况下 ...
- java8interface的新特性:default,static,funcation
default:默认方法 在类接口中可以直接定义的方法,实现接口的类可以直接使用 使用案例: public interface MyInterface { default void display() ...
- python Selenium 不要混合隐式和显式等待
警告:不要混合隐式和显式等待.这样做可能会导致不可预测的等待时间.例如,设置10秒的隐式等待和15秒的显式等待可能会导致20秒后发生超时 Warning: Do not mix implicit an ...
- ansible 001 ansible介绍 原理 主机清单 定义变量 提权与连接
ansible 自动化运维 ansible 部署应用程序 (在操作系统层面之上) 系统初始化过程 主机名,yun源,网络,服务,时间同步,内核参数 (可以在pxe这里完成) ansible可以方便10 ...
- 【Forza Horizon 5】频繁断网解决办法
参考自文章: https://www.acfun.cn/a/ac32056183_2 简而言之就是玩地平线5的时候不要挂着腾讯的QQ.TIM.微信
- 【Vue】Re14 Router 第一部分(入门案例)
一.箭头函数(Lambda匿名函数) <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- Efficientvit: Enhanced linear attention for high-resolution low-computation visual recognition
GitHub地址: https://github.com/mit-han-lab/efficientvit#usage 相关: https://www.jetson-ai-lab.com/vit/tu ...