0
点赞
收藏
分享

微信扫一扫

数据库-多表查询-连接查询

13. 数据库-多表查询-连接查询

同时查询多张表获取到需要的数据 比如:我们想查询到开发部有多少人,需要将部门表和员工表同时进行查询数据库-多表查询-连接查询_mysql

多表查询的分类:数据库-多表查询-连接查询_列表_02

准备数据

-- 创建部门表
create table dept(
id int primary key auto_increment,
name varchar(20)
);

insert into dept (name) values ('开发部'),('市场部'),('财务部');

-- 创建员工表
create table emp (
id int primary key auto_increment,
name varchar(10),
gender char(1), -- 性别
salary double, -- 工资
join_date date, -- 入职日期
dept_id int,
foreign key (dept_id) references dept(id) -- 外键,关联部门表(部门表的主键)
);

insert into emp(name,gender,salary,join_date,dept_id) values('孙悟空','男',7200,'2013-02-24',1);
insert into emp(name,gender,salary,join_date,dept_id) values('猪八戒','男',3600,'2010-12-02',2);
insert into emp(name,gender,salary,join_date,dept_id) values('唐僧','男',9000,'2008-08-08',2);
insert into emp(name,gender,salary,join_date,dept_id) values('白骨精','女',5000,'2015-10-07',3);
insert into emp(name,gender,salary,join_date,dept_id) values('蜘蛛精','女',4500,'2011-03-14',1);

查询某员工在哪个部门?

-- 只查询一张表不能查询出员工名字和部门名字,需要使用多表操作
select * from emp, dept;

完成多表操作的两种方式:

  1. 表连接
  2. 子查询

1. 笛卡尔积

概念

左表的每条数据和右表的每条数据组合成新的数据

如:查询员工表和部门表,查询的数据如下,结果就是笛卡尔积的数据

select * from emp,dept;


数据库-多表查询-连接查询_sql_03

1566353721827

查询某员工所在的部门

部门是左表,员工是右表。

数据库-多表查询-连接查询_数据库_04


1565859274486

我们发现不是所有的数据组合都是有用的,只有 员工表.dept_id = 部门表.id 的数据才是有用的。所以需要通过条件过滤掉没用的数据

# 需求: 查询孙悟空在哪个部门名字
-- 1. 查询每张表: 如果单次只查询一张表无法同时得出某个员工对应的部门
select * from emp;
select * from dept;

-- 2. 查询所有的员工和所有的部门
-- 查询2张表结果是2张表记录的乘积,称为笛卡尔积
select * from emp,dept;

-- 3. 查询员工和对应的部门
-- 消除笛卡尔积:条件是从表.外键=主表.主键
select * from emp,dept where emp.dept_id = dept.id;
-- 这就是隐式内连接,使用where,没有用到join...on

-- 给表起别名
select * from emp e ,dept d where e.dept_id = d.id;

-- 4. 查询员工孙悟空和对应的部门名字
select * from emp e ,dept d where e.dept_id = d.id and e.name = '孙悟空';

-- 只查询孙悟空的员工名字和部门名字,并指定别名:员工名、部门名
select e.name 员工名,d.name 部门名 from emp e ,dept d where e.dept_id = d.id and e.name = '孙悟空';

执行如下:

-- 1. 查询每张表: 如果单次只查询一张表无法同时得出某个员工对应的部门
mysql> select * from emp;
+----+-----------+--------+--------+------------+---------+
| id | name | gender | salary | join_date | dept_id |
+----+-----------+--------+--------+------------+---------+
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 |
| 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 |
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 |
| 4 | 白骨精 | | 5000 | 2015-10-07 | 3 |
| 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 |
+----+-----------+--------+--------+------------+---------+
5 rows in set (0.00 sec)

mysql> select * from dept;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 开发部 |
| 2 | 市场部 |
| 3 | 财务部 |
+----+-----------+
3 rows in set (0.00 sec)

-- 2. 查询所有的员工和所有的部门
-- 查询2张表结果是2张表记录的乘积,称为笛卡尔积
mysql> select * from emp,dept;
+----+-----------+--------+--------+------------+---------+----+-----------+
| id | name | gender | salary | join_date | dept_id | id | name |
+----+-----------+--------+--------+------------+---------+----+-----------+
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 | 1 | 开发部 |
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 | 2 | 市场部 |
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 | 3 | 财务部 |
| 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 | 1 | 开发部 |
| 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 | 2 | 市场部 |
| 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 | 3 | 财务部 |
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 | 1 | 开发部 |
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 | 2 | 市场部 |
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 | 3 | 财务部 |
| 4 | 白骨精 | | 5000 | 2015-10-07 | 3 | 1 | 开发部 |
| 4 | 白骨精 | | 5000 | 2015-10-07 | 3 | 2 | 市场部 |
| 4 | 白骨精 | | 5000 | 2015-10-07 | 3 | 3 | 财务部 |
| 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 | 1 | 开发部 |
| 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 | 2 | 市场部 |
| 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 | 3 | 财务部 |
+----+-----------+--------+--------+------------+---------+----+-----------+
15 rows in set (0.00 sec)

-- 3. 查询员工和对应的部门
-- 消除笛卡尔积:条件是从表.外键=主表.主键
-- 这就是隐式内连接,使用where,没有用到join...on
mysql> select * from emp,dept where emp.dept_id = dept.id;
+----+-----------+--------+--------+------------+---------+----+-----------+
| id | name | gender | salary | join_date | dept_id | id | name |
+----+-----------+--------+--------+------------+---------+----+-----------+
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 | 1 | 开发部 |
| 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 | 1 | 开发部 |
| 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 | 2 | 市场部 |
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 | 2 | 市场部 |
| 4 | 白骨精 | | 5000 | 2015-10-07 | 3 | 3 | 财务部 |
+----+-----------+--------+--------+------------+---------+----+-----------+
5 rows in set (0.00 sec)

-- 给表起别名
mysql> select * from emp e ,dept d where e.dept_id = d.id;
+----+-----------+--------+--------+------------+---------+----+-----------+
| id | name | gender | salary | join_date | dept_id | id | name |
+----+-----------+--------+--------+------------+---------+----+-----------+
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 | 1 | 开发部 |
| 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 | 1 | 开发部 |
| 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 | 2 | 市场部 |
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 | 2 | 市场部 |
| 4 | 白骨精 | | 5000 | 2015-10-07 | 3 | 3 | 财务部 |
+----+-----------+--------+--------+------------+---------+----+-----------+
5 rows in set (0.00 sec)

-- 4. 查询员工孙悟空和对应的部门名字
mysql> select * from emp e ,dept d where e.dept_id = d.id and e.name = '孙悟空';
+----+-----------+--------+--------+------------+---------+----+-----------+
| id | name | gender | salary | join_date | dept_id | id | name |
+----+-----------+--------+--------+------------+---------+----+-----------+
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 | 1 | 开发部 |
+----+-----------+--------+--------+------------+---------+----+-----------+
1 row in set (0.00 sec)

-- 只查询孙悟空的员工名字和部门名字,并指定别名:员工名、部门名
mysql> select e.name 员工名,d.name 部门名 from emp e ,dept d where e.dept_id = d.id and e.name = '孙悟空';
+-----------+-----------+
| 员工名 | 部门名 |
+-----------+-----------+
| 孙悟空 | 开发部 |
+-----------+-----------+
1 row in set (0.00 sec)

mysql>

2. 内连接

内连接分类

  1. 隐式内连接
  2. 显示内连接

数据库-多表查询-连接查询_数据库_05


1567928226926

语法

-- 隐式内连接语法
select 列名 from 左表,右表 where 从表.外键=主表.主键

-- 显示内连接, on后面就是表连接的条件
select 列名 from 左表 inner join 右表 on 从表.外键=主表.主键

应用

查询唐僧的信息,显示员工id,姓名,性别,工资和所在的部门名称

  1. 确定查询哪些表
  2. 确定表连接条件,员工表.dept_id = 部门表.id 的数据才是有效的
  3. 确定查询条件,我们查询的是唐僧的信息,员工表.name='唐僧'
  4. 确定查询字段,查询唐僧的信息,显示员工id,姓名,性别,工资和所在的部门名称
  5. 我们发现写表名有点长,可以给表取别名,显示的字段名也使用别名
-- 查询唐僧的信息,显示员工id,姓名,性别,工资和所在的部门名称
-- 1. 确定查询哪些表
select * from emp e inner join dept d;

-- 2. 确定表连接的条件
select * from emp e inner join dept d on e.dept_id = d.id;

-- 3. 如果有其它的查询条件,添加where语句
select * from emp e inner join dept d on e.dept_id = d.id where e.name='唐僧';

-- 4. 确定查询哪些列
select e.id 编号, e.name 姓名, e.gender 性别, e.salary 工资, d.name 部门名
from emp e inner join dept d on e.dept_id = d.id where e.name='唐僧';

执行如下:

-- 1. 确定查询哪些表(使用显示内连接)
mysql> select * from emp e inner join dept d;
+----+-----------+--------+--------+------------+---------+----+-----------+
| id | name | gender | salary | join_date | dept_id | id | name |
+----+-----------+--------+--------+------------+---------+----+-----------+
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 | 1 | 开发部 |
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 | 2 | 市场部 |
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 | 3 | 财务部 |
| 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 | 1 | 开发部 |
| 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 | 2 | 市场部 |
| 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 | 3 | 财务部 |
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 | 1 | 开发部 |
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 | 2 | 市场部 |
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 | 3 | 财务部 |
| 4 | 白骨精 | | 5000 | 2015-10-07 | 3 | 1 | 开发部 |
| 4 | 白骨精 | | 5000 | 2015-10-07 | 3 | 2 | 市场部 |
| 4 | 白骨精 | | 5000 | 2015-10-07 | 3 | 3 | 财务部 |
| 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 | 1 | 开发部 |
| 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 | 2 | 市场部 |
| 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 | 3 | 财务部 |
+----+-----------+--------+--------+------------+---------+----+-----------+
15 rows in set (0.00 sec)

-- 2. 确定表连接的条件
mysql> select * from emp e inner join dept d on e.dept_id = d.id;
+----+-----------+--------+--------+------------+---------+----+-----------+
| id | name | gender | salary | join_date | dept_id | id | name |
+----+-----------+--------+--------+------------+---------+----+-----------+
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 | 1 | 开发部 |
| 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 | 1 | 开发部 |
| 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 | 2 | 市场部 |
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 | 2 | 市场部 |
| 4 | 白骨精 | | 5000 | 2015-10-07 | 3 | 3 | 财务部 |
+----+-----------+--------+--------+------------+---------+----+-----------+
5 rows in set (0.00 sec)

-- 3. 如果有其它的查询条件,添加where语句
mysql> select * from emp e inner join dept d on e.dept_id = d.id where e.name='唐僧';
+----+--------+--------+--------+------------+---------+----+-----------+
| id | name | gender | salary | join_date | dept_id | id | name |
+----+--------+--------+--------+------------+---------+----+-----------+
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 | 2 | 市场部 |
+----+--------+--------+--------+------------+---------+----+-----------+
1 row in set (0.01 sec)

-- 4. 确定查询哪些列
mysql> select e.id 编号, e.name 姓名, e.gender 性别, e.salary 工资, d.name 部门名 from emp e inner join dept d on e.dept_id = d.id where e.name='唐僧';
+--------+--------+--------+--------+-----------+
| 编号 | 姓名 | 性别 | 工资 | 部门名 |
+--------+--------+--------+--------+-----------+
| 3 | 唐僧 | | 9000 | 市场部 |
+--------+--------+--------+--------+-----------+
1 row in set (0.00 sec)

mysql>

3. 外连接(Mysql支持:左外连接 、右外连接)

外连接分类

  1. 左外连接
    左表中所有的记录都出现在结果中,并上右表与之对应的部分, 如果右表没有匹配的记录,使用NULL填充

数据库-多表查询-连接查询_sql_06


1567928263577

  1. 右外连接
    右表中所有的记录都出现在结果中,并上左表与之对应的部分, 如果左表没有对应的记录,使用NULL填充

数据库-多表查询-连接查询_数据库_07


1567928322473

语法

-- 左外连接
select 列名 from 左表 left join 右表 on 从表.外键=主表.主键

-- 右外连接
select 列名 from 左表 right join 右表 on 从表.外键=主表.主键

应用

左外连接

需求:查询所有的部门,以及该部门下面的员工

-- 添加一个销售部,暂时还没有员工
insert into dept (name) values ('销售部');

-- 使用内连接查询,缺少销售部
select * from dept d inner join emp e on d.id = e.dept_id;

-- 使用左外连接查询
select * from dept d left join emp e on d.id = e.dept_id;

执行如下:

-- 1. 添加一个销售部,暂时还没有员工
mysql> select * from dept;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 开发部 |
| 2 | 市场部 |
| 3 | 财务部 |
+----+-----------+
3 rows in set (0.00 sec)

mysql> insert into dept (name) values ('销售部');
Query OK, 1 row affected (0.00 sec)

mysql> select * from dept;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 开发部 |
| 2 | 市场部 |
| 3 | 财务部 |
| 4 | 销售部 |
+----+-----------+
4 rows in set (0.00 sec)

-- 2. 使用内连接查询,缺少销售部
mysql> select * from dept d inner join emp e on d.id = e.dept_id;
+----+-----------+----+-----------+--------+--------+------------+---------+
| id | name | id | name | gender | salary | join_date | dept_id |
+----+-----------+----+-----------+--------+--------+------------+---------+
| 1 | 开发部 | 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 |
| 1 | 开发部 | 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 |
| 2 | 市场部 | 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 |
| 2 | 市场部 | 3 | 唐僧 | | 9000 | 2008-08-08 | 2 |
| 3 | 财务部 | 4 | 白骨精 | | 5000 | 2015-10-07 | 3 |
+----+-----------+----+-----------+--------+--------+------------+---------+
5 rows in set (0.00 sec)

-- 3. 使用左外连接查询: 可以看到能够查询出 【销售部】
-- 左表中所有的记录都出现在结果中,并上右表与之对应的部分, 如果右表没有匹配的记录,使用NULL填充
mysql> select * from dept d left join emp e on d.id = e.dept_id;
+----+-----------+------+-----------+--------+--------+------------+---------+
| id | name | id | name | gender | salary | join_date | dept_id |
+----+-----------+------+-----------+--------+--------+------------+---------+
| 1 | 开发部 | 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 |
| 1 | 开发部 | 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 |
| 2 | 市场部 | 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 |
| 2 | 市场部 | 3 | 唐僧 | | 9000 | 2008-08-08 | 2 |
| 3 | 财务部 | 4 | 白骨精 | | 5000 | 2015-10-07 | 3 |
| 4 | 销售部 | NULL | NULL | NULL | NULL | NULL | NULL |
+----+-----------+------+-----------+--------+--------+------------+---------+
6 rows in set (0.01 sec)

mysql>

右外连接

需求:查询所有员工,以及员工所属的部门

-- 在员工表中增加一个员工:'沙僧','男',6666,'2013-02-24',null
insert into emp values(null, '沙僧','男',6666,'2013-02-24',null);
select * from emp;

-- 使用内连接查询
select * from dept d inner join emp e on d.id = e.dept_id;

-- 使用右外连接查询
select * from dept d right join emp e on d.id = e.dept_id;

执行如下:

-- 1. 在员工表中增加一个员工:'沙僧','男',6666,'2013-02-24',null
mysql> insert into emp values(null, '沙僧','男',6666,'2013-02-24',null);
Query OK, 1 row affected (0.00 sec)

mysql> select * from emp;
+----+-----------+--------+--------+------------+---------+
| id | name | gender | salary | join_date | dept_id |
+----+-----------+--------+--------+------------+---------+
| 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 |
| 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 |
| 3 | 唐僧 | | 9000 | 2008-08-08 | 2 |
| 4 | 白骨精 | | 5000 | 2015-10-07 | 3 |
| 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 |
| 6 | 沙僧 | | 6666 | 2013-02-24 | NULL | -- 可以看到该条数据没有 dept_id 的值
+----+-----------+--------+--------+------------+---------+
6 rows in set (0.00 sec)

-- 2. 使用内连接查询: 无法查询出 dept_id null 沙僧 数据,此时就要以 右表 emp 为准,使用右连接查询所有数据
mysql> select * from dept d inner join emp e on d.id = e.dept_id;
+----+-----------+----+-----------+--------+--------+------------+---------+
| id | name | id | name | gender | salary | join_date | dept_id |
+----+-----------+----+-----------+--------+--------+------------+---------+
| 1 | 开发部 | 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 |
| 1 | 开发部 | 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 |
| 2 | 市场部 | 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 |
| 2 | 市场部 | 3 | 唐僧 | | 9000 | 2008-08-08 | 2 |
| 3 | 财务部 | 4 | 白骨精 | | 5000 | 2015-10-07 | 3 |
+----+-----------+----+-----------+--------+--------+------------+---------+
5 rows in set (0.00 sec)

-- 3. 使用右外连接查询: 基于右连接查询,不管 emp 的数据有没有关联 dept_id, 也可以查询出右表中的所有数据
mysql> select * from dept d right join emp e on d.id = e.dept_id;
+------+-----------+----+-----------+--------+--------+------------+---------+
| id | name | id | name | gender | salary | join_date | dept_id |
+------+-----------+----+-----------+--------+--------+------------+---------+
| 1 | 开发部 | 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 |
| 1 | 开发部 | 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 |
| 2 | 市场部 | 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 |
| 2 | 市场部 | 3 | 唐僧 | | 9000 | 2008-08-08 | 2 |
| 3 | 财务部 | 4 | 白骨精 | | 5000 | 2015-10-07 | 3 |
| NULL | NULL | 6 | 沙僧 | | 6666 | 2013-02-24 | NULL |
+------+-----------+----+-----------+--------+--------+------------+---------+
6 rows in set (0.01 sec)

mysql>

4. 全外连接查询 full(Mysql不支持,Oracle支持,了解就可以)

在上面的操作中,我们已经执行了 左外连接 和 右外连接。那么如果我们想要两张表的所有数据同时查询出来呢?

这时候就要使用 全外连接查询 了。

语法格式:

select * from dept d full join emp e on d.id = e.dept_id;

但是在 mysql 执行中是不支持的,执行则会报错如下:

mysql> select * from dept d full join emp e on d.id = e.dept_id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'full join emp e on d.id = e.dept_id' at line 1
mysql>

对于这种情况,mysql 提供了 union 联合查询来解决。

5. 联合查询 Union (Mysql 支持)

Mysql 为了查询所有表的关联数据,可以将左右连接的查询 联合一起来执行。

语法格式:

-- 使用union联合合并左右外连接的查询结果,就是相当于全外连接查询了。

-- 1.使用 union 将会重复的交集进行去重
-- 左外连接
select 列名 from 左表 left join 右表 on 从表.外键=主表.主键
union
-- 右外连接
select 列名 from 左表 right join 右表 on 从表.外键=主表.主键

-- 2.使用 union all 将展示重复的交集内容
-- 左外连接
select 列名 from 左表 left join 右表 on 从表.外键=主表.主键
union
-- 右外连接
select 列名 from 左表 right join 右表 on 从表.外键=主表.主键

执行如下:

-- 1.使用 union 将会重复的交集进行去重
mysql> select * from dept d left join emp e on d.id = e.dept_id
-> union
-> select * from dept d right join emp e on d.id = e.dept_id;
+------+-----------+------+-----------+--------+--------+------------+---------+
| id | name | id | name | gender | salary | join_date | dept_id |
+------+-----------+------+-----------+--------+--------+------------+---------+
| 1 | 开发部 | 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 |
| 2 | 市场部 | 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 |
| 2 | 市场部 | 3 | 唐僧 | | 9000 | 2008-08-08 | 2 |
| 3 | 财务部 | 4 | 白骨精 | | 5000 | 2015-10-07 | 3 |
| 1 | 开发部 | 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 |
| 4 | 销售部 | NULL | NULL | NULL | NULL | NULL | NULL |
| NULL | NULL | 6 | 沙僧 | | 6666 | 2013-02-24 | NULL |
+------+-----------+------+-----------+--------+--------+------------+---------+
7 rows in set (0.00 sec)

mysql>

-- 2.使用 union all 将展示重复的交集内容
mysql> select * from dept d left join emp e on d.id = e.dept_id
-> union all
-> select * from dept d right join emp e on d.id = e.dept_id;
+------+-----------+------+-----------+--------+--------+------------+---------+
| id | name | id | name | gender | salary | join_date | dept_id |
+------+-----------+------+-----------+--------+--------+------------+---------+
| 1 | 开发部 | 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 |
| 2 | 市场部 | 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 |
| 2 | 市场部 | 3 | 唐僧 | | 9000 | 2008-08-08 | 2 |
| 3 | 财务部 | 4 | 白骨精 | | 5000 | 2015-10-07 | 3 |
| 1 | 开发部 | 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 |
| 4 | 销售部 | NULL | NULL | NULL | NULL | NULL | NULL |
| 1 | 开发部 | 1 | 孙悟空 | | 7200 | 2013-02-24 | 1 | -- 交集内容重复出现
| 1 | 开发部 | 5 | 蜘蛛精 | | 4500 | 2011-03-14 | 1 |
| 2 | 市场部 | 2 | 猪八戒 | | 3600 | 2010-12-02 | 2 |
| 2 | 市场部 | 3 | 唐僧 | | 9000 | 2008-08-08 | 2 |
| 3 | 财务部 | 4 | 白骨精 | | 5000 | 2015-10-07 | 3 |
| NULL | NULL | 6 | 沙僧 | | 6666 | 2013-02-24 | NULL |
+------+-----------+------+-----------+--------+--------+------------+---------+
12 rows in set (0.00 sec)

mysql>

6. select 查询顺序

select 查询列表    
from 表1 别名
连接类型 join 表2
on 连接条件
where 筛选
group by 分组列表
having 筛选
order by排序列表
limit 起始条目索引,条目数;

---- 基本顺序如下:
from 表1 别名
连接类型 join 表2
on 连接条件
where 筛选
group by 分组列表
having 筛选
select 查询列表
order by 排序列表
limit 起始条目索引,条目数


举报

相关推荐

0 条评论