(七)MySQL数据操作DQL:多表查询2
(1)准备环境
1)创建员工表
mysql> create table company.employee6(
-> emp_id int auto_increment primary key not null,
-> emp_name varchar(10),
-> age int,
-> dept_id int);
mysql> insert into employee6(emp_name,age,dept_id) values
-> ('tom',19,200),
-> ('jack',30,201),
-> ('alice',24,202),
-> ('robin',40,200),
-> ('natasha',28,204);
2)创建部门表
mysql> create table company.department(
-> dept_id int,
-> dept_name varchar(100));
mysql> insert into department values (200,'hr'), (201,'it'), (202,'sale'), (203,'fd');
(2)交叉连接
生成笛卡尔积,不使用任何匹配条件
语法:select 表1.字段1,表1.字段2,表2.字段1 from 表1,表2;
mysql> select employee6.emp_name,employee6.age,employee6.dept_id,department.dept_name from employee6,department;
+----------+------+---------+-----------+
| emp_name | age | dept_id | dept_name |
+----------+------+---------+-----------+
| tom | 19 | 200 | hr |
| tom | 19 | 200 | it |
| tom | 19 | 200 | sale |
| tom | 19 | 200 | fd |
| jack | 30 | 201 | hr |
| jack | 30 | 201 | it |
| jack | 30 | 201 | sale |
| jack | 30 | 201 | fd |
| alice | 24 | 202 | hr |
| alice | 24 | 202 | it |
| alice | 24 | 202 | sale |
| alice | 24 | 202 | fd |
| robin | 40 | 200 | hr |
| robin | 40 | 200 | it |
| robin | 40 | 200 | sale |
| robin | 40 | 200 | fd |
| natasha | 28 | 204 | hr |
| natasha | 28 | 204 | it |
| natasha | 28 | 204 | sale |
| natasha | 28 | 204 | fd |
+----------+------+---------+-----------+
(3)内连接:根据两张表的相同字段只连接匹配的行
语法:select 表1.字段n,表2.字段n from 表1,表2 表1.字段 = 表2.字段
根据员工表的dept_id 和部门表的dept_id进行连接,只匹配dept_id相同的行
mysql> select employee6.dept_id,employee6.emp_name,employee6.age,department.dept_name from employee6,department where employee6.dept_id = department.dept_id;
+---------+----------+------+-----------+
| dept_id | emp_name | age | dept_name |
+---------+----------+------+-----------+
| 200 | tom | 19 | hr |
| 201 | jack | 30 | it |
| 202 | alice | 24 | sale |
| 200 | robin | 40 | hr |
+---------+----------+------+-----------+
(4)外连接
语法:select 字段列表 from 表1 left|right join 表2 on 表1.字段 = 表2.字段
1)外连接之左连接:会显示左边表内所有的值,不论在右边表内匹不匹配
mysql> select emp_id,emp_name,age,dept_name from employee6 left join department on employee6.dept_id = department.dept_id;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 1 | tom | 19 | hr |
| 4 | robin | 40 | hr |
| 2 | jack | 30 | it |
| 3 | alice | 24 | sale |
| 5 | natasha | 28 | NULL |
+--------+----------+------+-----------+
2)外连接之右连接:会显示右边表内所有的值,不论在左边表内匹不匹配
mysql> select emp_id,emp_name,age,dept_name from employee6 right join department on employee6.dept_id = department.dept_id;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 1 | tom | 19 | hr |
| 2 | jack | 30 | it |
| 3 | alice | 24 | sale |
| 4 | robin | 40 | hr |
| NULL | NULL | NULL | fd |
+--------+----------+------+-----------+
(5)复合条件连接查询
以内连接的方式查询 employee6 和 department 表,并且 employee6 表中的 age 字段值必须大于 25,排序
mysql> select emp_id,emp_name,age,dept_name from employee6,department where employee6.dept_id = department.dept_id and age >25;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 4 | robin | 40 | hr |
| 2 | jack | 30 | it |
+--------+----------+------+-----------+
2 rows in set (0.00 sec)
mysql> select emp_id,emp_name,age,dept_name from employee6,department where employee6.dept_id = department.dept_id and age >25 order by age;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 2 | jack | 30 | it |
| 4 | robin | 40 | hr |
+--------+----------+------+-----------+
2 rows in set (0.00 sec)
mysql> select emp_id,emp_name,age,dept_name from employee6,department where employee6.dept_id = department.dept_id and age >25 order by age desc;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 4 | robin | 40 | hr |
| 2 | jack | 30 | it |
+--------+----------+------+-----------+
(6)子查询
子查询是将一个查询语句嵌套在另一个查询语句中。内层查询语句的查询结果,可以为外层查询语句提供查询条件。
1)带in的子查询
mysql> select * from employee6 where dept_id in (select dept_id from department);
+--------+----------+------+---------+
| emp_id | emp_name | age | dept_id |
+--------+----------+------+---------+
| 1 | tom | 19 | 200 |
| 2 | jack | 30 | 201 |
| 3 | alice | 24 | 202 |
| 4 | robin | 40 | 200 |
+--------+----------+------+---------+
4 rows in set (0.00 sec)
2)带比较运算符的子查询
mysql> select dept_name from department where dept_id in (select dept_id from employee6 where age >25);
+-----------+
| dept_name |
+-----------+
| it |
| hr |
+-----------+
2 rows in set (0.00 sec)
(七)MySQL数据操作DQL:多表查询2的更多相关文章
- (七)MySQL数据操作DQL:单表查询1
(1)单表查询 1)环境准备 mysql> CREATE TABLE company.employee5( id int primary key AUTO_INCREMENT not null, ...
- mysql第四篇:数据操作之多表查询
mysql第四篇:数据操作之多表查询 一.多表联合查询 #创建部门 CREATE TABLE IF NOT EXISTS dept ( did int not null auto_increment ...
- mysql第四篇:数据操作之单表查询
单表查询 一.简单查询 -- 创建表 DROP TABLE IF EXISTS `person`; CREATE TABLE `person` ( `id` ) NOT NULL AUTO_INCRE ...
- 07-查询操作(DQL)-多表查询
一. 综述 查询操作主要从两个方面来说:单表查询和多表查询. 多表查询包括:笛卡尔积.外键约束.内连接查询.外链接查询.自连接查询. 二 . 案例设计 1. 设计产品表(product). ...
- 06-查询操作(DQL)-单表查询
一. 综述 查询操作主要从两个方面来说:单表查询和多表查询. 单表查询包括:简单查询.过滤查询.结果排序.分页查询.聚集函数. 二 . 案例设计 1. 设计产品表(product).包括:主键 ...
- mysql 数据操作 单表查询 目录
mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...
- mysql 数据操作 多表查询 目录
mysql 数据操作 多表查询 准备 多表连接查询介绍 mysql 数据操作 多表查询 多表连接查询 笛卡尔积 mysql 数据操作 多表查询 多表连接查询 内连接 mysql 数据操作 多表查询 多 ...
- mysql 数据操作 单表查询 where 约束 目录
mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...
- mysql 数据操作 单表查询 group by 分组 目录
mysql 数据操作 单表查询 group by 介绍 mysql 数据操作 单表查询 group by 聚合函数 mysql 数据操作 单表查询 group by 聚合函数 没有group by情况 ...
随机推荐
- 【bzoj3052】[wc2013]糖果公园 带修改树上莫队
题目描述 给出一棵n个点的树,每个点有一个点权,点权范围为1~m.支持两种操作:(1)修改一个点的点权 (2)对于一条路径,求$\sum\limits_{i=1}^m\sum\limits_{j=1} ...
- line-height用法总结
Line-height是前端用语,经常被前端开发人员经常使用. line-height设置1.5和150%有什么区别?这是一个比较常见的前端面试题. 定义: line-height指的是文本行基线间的 ...
- Java操作Redis存储对象类型数据
背景描述 关于JAVA去操作Redis时,如何存储一个对象的数据,大家是非常关心的问题,虽然官方提供了存储String,List,Set等等类型,但并不满足我们现在实际应用.存储一个对象是是 ...
- MySQL 创建一个简单的成绩管理系统
操作过程使用实验楼. 首先是创建一个数据库studentsystem,使用语句是: CREATE DATABASE studentsystem; 查看创建好的数据库的命令还是SHOW DATABAS ...
- 如何用Ajax传一个数组数据
PHP接收多个同名复选框信息不像ASP那样自动转换成为数组,这给使用带来了一定不便.但是还是有解决办法的,就是利用javascript做一下预处 理.多个同名复选框在javascript中还是以数组的 ...
- Spring表达式语言之SpEL
•Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. •语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL ...
- Things To Do Before NOI2017
TC div1 10套 数据结构 25题 网络流 10题 字符串 20题 数学 15题 图论 15题 计算几何 5题 提交答案 5题 嗯...先这些吧... 以上所有题目,博客都会有更新--- NOI ...
- python 读 excel 模块: xlrd
主要来自:[ python中使用xlrd.xlwt操作excel表格详解 ] 为了方便阅读, 我将原文两个模块拆分为两篇博文: [ python 读 excel 模块: xlrd ] [ python ...
- bzoj 2435 BFS
我们可以先将无根树变成有根树,随便选一个点当根就行了,我们选1,bfs求出来每个点 的size值,代表以它为根节点的子树中有多少个节点,(dfs可能会爆栈),然后再对于每一条 边算就好了 我tle了, ...
- 鼠标DPI和液晶显示器分辨率的关系
鼠标DPI和液晶显示器分辨率的关系 说起鼠标,有两个性能指标是我们不能忽略的.一是游戏玩家相当熟悉的扫描率(单位:Frames Per Second),二是我们今天要和大家探讨的鼠标的分辨率(单位:D ...