当前位置:网站首页>Mysql_12 多表查询
Mysql_12 多表查询
2022-08-05 00:01:00 【萨科塔资深干员】
多表查询
一对一,一对多,多对多(两个一对多)
一对一:其中一方设置unique的外键。
一对多:多的一方设置外键。
多对多:建立中间表,设置两个外键。
直接查询:select * from A, B;笛卡尔积,结果是AB两表所有记录的排列组合。后面再加where可以筛选查询条件。
内连接
只连接两个表共有部分。
select * from A, B where A.id=B.id;-- 隐式内连接
select * from A [inner] join B on 条件;
外连接
完全包含左/右表中的数据。另一个表没有对应数据则补null。
select * from A left [outer] join B on 条件;-- 左外连接
select * from A right [outer] join B on 条件;-- 右外连接
自连接
select * from A AS1 inner join A AS2 where 条件;-- 自连接可以是内或外连接。必须起别名!!!
联合 Union
Union 会把查询结果联合起来。
union all:结果可能重复。
union:重复的结果合并。
想做union操作,两个要联合的表必须字段数相同,字段类型匹配。
子查询
上一次查询的结果作为下一次查询的条件。
select * from (select * from b);
标量子查询:结果为一个。可以做> < =操作。
列子查询:结果为一列。可以做 IN NOT IN SOME ALL ANY 操作。(ANY SOME 等价)
行子查询:结果为一行。可以做 = <> IN NOT IN 操作。
表子查询:结果为多行多列。最常用 IN NOT IN 操作。
边栏推荐
- The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
- golang 协程的实现原理
- 注解@EnableAutoConfiguration的作用以及如何使用
- 什么是次世代建模(附学习资料)
- 《MySQL入门很轻松》第2章:MySQL管理工具介绍
- Mysql based
- Xiaohei leetcode surfing: 94. Inorder traversal of binary tree
- 怎样进行在不改变主线程执行的时候,进行日志的记录
- jenkins发送邮件系统配置
- Basic web in PLSQL
猜你喜欢
![[Happy Qixi Festival] How does Nacos realize the service registration function?](/img/df/5793145da45bc80d227b0babfac914.png)
[Happy Qixi Festival] How does Nacos realize the service registration function?

What is next-generation modeling (with learning materials)

三、实战---爬取百度指定词条所对应的结果页面(一个简单的页面采集器)

学会反射后,我被录取了(干货)

MongoDB permission verification is turned on and mongoose database configuration
手写分布式配置中心(1)

标识符、关键字、常量 和变量(C语言)

性能测试如何准备测试数据

Getting started with 3D modeling for games, what modeling software can I choose?

找不到DiscoveryClient类型的Bean
随机推荐
KT148A语音芯片怎么烧录语音进入芯片里面通过串口和电脑端的工具
Mysql based
Handwritten Distributed Configuration Center (1)
一、爬虫基本概念
mysql基础
线程三连鞭之“线程的状态”
在线中文姓名生成工具推荐
Develop a SpaceX website based on the Appian low-code platform
Uniapp dynamic sliding navigation effect demo (finishing)
Senior game modelers tell newbies, what are the necessary software for game scene modelers?
【SSR服务端渲染+CSR客户端渲染+post请求+get请求+总结】
OpenCV:10特征检测
The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
Modelers experience sharing: model study method
【LeetCode】矩阵模拟相关题目汇总
看图识字,DELL SC4020 / SCv2000 控制器更换过程
VMware NSX 4.0 -- 网络安全虚拟化平台
性能测试如何准备测试数据
npm基本操作及命令详解