mysql--多表连接查询
一.多表连接查询
SELECT 字段列表
FROM 表1 INNER|LEFT|RIGHT JOIN 表2
ON 表1.字段 = 表2.字段;
1.交叉连接:不适用任何匹配条件.生成笛卡尔积
select * from department,employee; #表用逗号分隔,看我查询时表的顺序,先department后employee,所以你看结果表的这些字段,是不是就是我们两个表字段并且哪个表在前面,哪个表的字段就在前面
2.内连接:只连接匹配的行
select * from employee,department where employee.dep_id=department.id;
3.外连接: 左连接 优先显示左表全部记录
#本质就是:在内连接的基础上增加左边有右边没有的结果 #注意语法: select employee.id,employee.name,department.name as depart_name from employee left join department on employee.dep_id=department.id;
4.外连接: 右连接 有限显示右表全部记录
本质就是:在内连接的基础上增加右边有左边没有的结果
select employee.id,employee.name,department.name as depart_name from employee right join department on employee.dep_id=department.id;
5.全外连接:显示左右两个表全部记录
全外连接:在内连接的基础上增加左边有右边没有的和右边有左边没有的结果
#注意:mysql不支持全外连接 full JOIN
#强调:mysql可以使用此种方式间接实现全外连接
select * from employee left join department on employee.dep_id = department.id
union
select * from employee right join department on employee.dep_id = department.id
;
二.符合条件连接查询
select employee.name,department.name from employee inner join department
on employee.dep_id = department.id
where age > 25;
三.子查询
子查询其实就是将你的一个查询结果用括号括起来,这个结果也是一张表,就可以将它交给另外一个sql语句,作为它的一个查询依据来进行操作。
1、带IN关键字的子查询
#查看不足1人的部门名(子查询得到的是有人的部门id)
select name from department where id not in (select distinct dep_id from employee);
2、带比较运算符的子查询
#比较运算符:=、!=、>、>=、<、<=、<>
在用where ,having条件时可以使用
3.带EXISTS关键字的子查询
EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录。而是返回一个真假值。True或False
当返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询。还可以写not exists,
和exists的效果就是反的
连表
练习:通过连表的方式来查询每个部门最新入职的那位员工 复制代码
company.employee
员工id id int
姓名 emp_name varchar
性别 sex enum
年龄 age int
入职日期 hire_date date
岗位 post varchar
职位描述 post_comment varchar
薪水 salary double
办公室 office int
部门编号 depart_id int #创建表,只需要创建这一张表
create table employee(
id int not null unique auto_increment,
name varchar(20) not null,
sex enum('male','female') not null default 'male', #大部分是男的
age int(3) unsigned not null default 28,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int, #一个部门一个屋子
depart_id int
); #查看表结构
mysql> desc employee;
+--------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(3) unsigned | NO | | 28 | |
| hire_date | date | NO | | NULL | |
| post | varchar(50) | YES | | NULL | |
| post_comment | varchar(100) | YES | | NULL | |
| salary | double(15,2) | YES | | NULL | |
| office | int(11) | YES | | NULL | |
| depart_id | int(11) | YES | | NULL | |
+--------------+-----------------------+------+-----+---------+----------------+ #插入记录
#三个部门:教学,销售,运营
insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
('egon','male',18,'','老男孩驻沙河办事处外交大使',7300.33,401,1), #以下是教学部
('alex','male',78,'','teacher',1000000.31,401,1),
('wupeiqi','male',81,'','teacher',8300,401,1),
('yuanhao','male',73,'','teacher',3500,401,1),
('liwenzhou','male',28,'','teacher',2100,401,1),
('jingliyang','female',18,'','teacher',9000,401,1),
('jinxin','male',18,'','teacher',30000,401,1),
('成龙','male',48,'','teacher',10000,401,1), ('歪歪','female',48,'','sale',3000.13,402,2),#以下是销售部门
('丫丫','female',38,'','sale',2000.35,402,2),
('丁丁','female',18,'','sale',1000.37,402,2),
('星星','female',18,'','sale',3000.29,402,2),
('格格','female',28,'','sale',4000.33,402,2), ('张野','male',28,'','operation',10000.13,403,3), #以下是运营部门
('程咬金','male',18,'','operation',20000,403,3),
('程咬银','female',18,'','operation',19000,403,3),
('程咬铜','male',18,'','operation',18000,403,3),
('程咬铁','female',18,'','operation',17000,403,3)
; #ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk
连表示例
mysql--多表连接查询的更多相关文章
- 【连接查询】mySql多表连接查询与union与union all用法
1.准备两个表 表a: 结构: mysql> desc a; +-------+-------------+------+-----+---------+-------+ | Field | T ...
- mysql多表连接查询
新建两张表: 表1:student 截图如下: 表2:course 截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际开发中我们不会这样建表,实际开发中这两个表会有自己不同的主键.) ...
- Mysql 多表连接查询 inner join 和 outer join 的使用
JOIN的含义就如英文单词“join”一样,连接两张表,大致分为内连接,外连接,右连接,左连接,自然连接.这里描述先甩出一张用烂了的图,然后插入测试数据. 首先先列举本篇用到的分类(内连接,外连接,交 ...
- [原创]普通的MySQL多表连接查询
- mysql(4)—— 表连接查询与where后使用子查询的性能分析。
子查询就是在一条查询语句中还有其它的查询语句,主查询得到的结果依赖于子查询的结果. 子查询的子语句可以在一条sql语句的FROM,JOIN,和WHERE后面,本文主要针对在WHERE后面使用子查询与表 ...
- MySQL之多表查询一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习
MySQL之多表查询 阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都会建 ...
- Mysql表连接查询
原文地址: https://www.cnblogs.com/qiuqiuqiu/p/6442791.html 1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等 ...
- MySQL数据库:多表连接查询
多表连接查询 注意:使用连接技术建议将表经行重命名! # explain 检索连接是否达标 # 内连接 # 语法1 from 表1 inner join 表2 on 主键字段=外键字段 [where ...
- mysql自关联和多表连接查询
自关联操作 多表连接查询 inner join 内查询 left join 左查询 right join 右查询 ...
- MySQL多表连接
主要分3种:内连接,外连接,交叉连接 其 他:联合连接,自然连接 1.内联接 典型的联接运算,使用像 = 或 <> 之类的比较运算).包括相等联接和自然联接. 内联接使用比 ...
随机推荐
- 【CentOS 6.5】【转】新版本linux生成xorg.conf
新版本的linux如何生成xorg.conf 较新版本的linux系统都已经没有xorg.conf文件,但是有时候为了对显示做微调或为了支持多屏显示等原因,还需要手工生成一个xorg.conf,然后根 ...
- echart改变legend样式及分页
legend: { type: 'scroll', orient: 'horizontal', bottom:0, left:'center', itemGap: 0, itemWidth: 10, ...
- CSS 第1练 搜索
1.搜索 效果: <!DOCTYPE HTML> <html> <head> <meta charset="gbk" /> < ...
- Spring MVC 学习 之 - 拦截器
public class GlobalInterceptor implements HandlerInterceptor { public boolean preHandle(HttpServletR ...
- leetcode654
class Solution { public: TreeNode* constructMaximumBinaryTree(vector<int>& nums) { ) retur ...
- Win10 安装 及应用遇到的问题
IOS https://www.microsoft.com/zh-cn/software-download/techbench setup win10安装必须用administrator账号安装 在w ...
- VB.NET条码机打印设置纸张大小的方法
Imports System.Drawing.PrintingImports System.Runtime.InteropServices Public Class Page <Runti ...
- rdd去重
a=[[1,2,3,2,3,4],[3,4,5,6,7,5,3,2]]b=sc.parallelize(a) d=b.flatMap(lambda x:x) #铺平 ,形成一个rdd e=d.dis ...
- java中将数字的字符串表示转化为数字
int a = new Integer("1234").intValue() 或 int b = Integer.parseInt("1234") System ...
- ShaderForge打造自定义光照模型
[ShaderForge打造自定义光照模型] 1.Lambert逻辑图. 2.Blinn-Phong逻辑图. 参考:https://www.youtube.com/watch?v=EjCXwV0YYd ...