(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的更多相关文章

  1. (七)MySQL数据操作DQL:单表查询1

    (1)单表查询 1)环境准备 mysql> CREATE TABLE company.employee5( id int primary key AUTO_INCREMENT not null, ...

  2. mysql第四篇:数据操作之多表查询

    mysql第四篇:数据操作之多表查询 一.多表联合查询 #创建部门 CREATE TABLE IF NOT EXISTS dept ( did int not null auto_increment ...

  3. mysql第四篇:数据操作之单表查询

    单表查询 一.简单查询 -- 创建表 DROP TABLE IF EXISTS `person`; CREATE TABLE `person` ( `id` ) NOT NULL AUTO_INCRE ...

  4. 07-查询操作(DQL)-多表查询

    一. 综述    查询操作主要从两个方面来说:单表查询和多表查询. 多表查询包括:笛卡尔积.外键约束.内连接查询.外链接查询.自连接查询. 二 . 案例设计   1.  设计产品表(product). ...

  5. 06-查询操作(DQL)-单表查询

    一. 综述   查询操作主要从两个方面来说:单表查询和多表查询. 单表查询包括:简单查询.过滤查询.结果排序.分页查询.聚集函数. 二 . 案例设计   1. 设计产品表(product).包括:主键 ...

  6. mysql 数据操作 单表查询 目录

    mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...

  7. mysql 数据操作 多表查询 目录

    mysql 数据操作 多表查询 准备 多表连接查询介绍 mysql 数据操作 多表查询 多表连接查询 笛卡尔积 mysql 数据操作 多表查询 多表连接查询 内连接 mysql 数据操作 多表查询 多 ...

  8. mysql 数据操作 单表查询 where 约束 目录

    mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...

  9. mysql 数据操作 单表查询 group by 分组 目录

    mysql 数据操作 单表查询 group by 介绍 mysql 数据操作 单表查询 group by 聚合函数 mysql 数据操作 单表查询 group by 聚合函数 没有group by情况 ...

随机推荐

  1. Redis能做什么?不能做什么?

    https://blog.csdn.net/u014229282/article/details/81174202 最近阅读了<redis设计与实现>,这是一本比较枯燥的书,毕竟涉及到re ...

  2. 在es中用scroll查询与completableFuture

    一般而言,es返回数据的上限是10000条,如果超过这个数量,就必须使用scroll查询. 所谓scroll查询就类似DBMS中的游标,或者快照吧,利用查询条件,在第一次查询时,在所有的结果上形成了一 ...

  3. kibana和ElasticSearch的信息查询检索

    使用kibana来进行ElasticSearch的信息查询检索 大家经常会听到使用ELK搭建日志管理平台.完成日志聚合检索的功能,那么这个平台到底是个什么概念,怎么搭建,怎么使用呢? ELK包括Ela ...

  4. BZOJ4690 Never Wait for Weights(并查集)

    带权并查集按秩合并即可维护. #include<iostream> #include<cstdio> #include<cmath> #include<cst ...

  5. BZOJ4668 冷战(并查集)

    显然可以用LCT维护kruskal重构树.或者启发式合并维护kruskal重构树的倍增数组虽然多了个log也不一定比LCT慢吧. 当然这里的kruskal重构树几乎只是把树上的边权换成了点权,并不重要 ...

  6. SRM707 div1 MultiplyAddPuzzle

    题目大意:给定4个数,s,t,a,b 每次可以将s加a或者乘b,问最少多少次可以得到t 做法:考虑最后的形式,肯定是s*b^n + a*f(b),f(b)是关于b的多项式 那么b乘多少次实际上是可以知 ...

  7. 理解NLP中的卷积神经网络(CNN)

    此篇文章是Denny Britz关于CNN在NLP中应用的理解,他本人也曾在Google Brain项目中参与多项关于NLP的项目. · 翻译不周到的地方请大家见谅. 阅读完本文大概需要7分钟左右的时 ...

  8. Linux总结(二)

      1. 虚拟机安装 a)双系统(不建议初学者一开始去装) b)般建议使用虚拟机来操作试验环境 c)好处:可以模拟真实的环境进行各种的试验和操作 d)在启动之后,在操作的时候会占用一部分的系统资源 1 ...

  9. POJ2155 Matrix 【二维线段树】

    题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...

  10. boost 文件操作

    void testFileSystem() { boost::filesystem::path path("/test/test1"); //初始化 boost::filesyst ...