双表查询java代码_多表查询(⽰例代码)前期准备⼯作:
两张表:部门表(department),员⼯表(employee)
create table department(
id int,
name varchar(20)
);
create table employee(
id int primary key auto_increment,
name varchar(20),
sex enum(‘male‘,‘female‘) not null default ‘male‘,
age int,
dep_id int
);#插⼊数据
insert into department values
(200,‘技术‘),
(201,‘⼈⼒资源‘),
(202,‘销售‘),
(203,‘运营‘);
insert into employee(name,sex,age,dep_id) values
(‘egon‘,‘male‘,18,200),
(‘alex‘,‘female‘,48,201),
(‘wupeiqi‘,‘male‘,38,201),
(‘yuanhao‘,‘female‘,28,202),
(‘nvshen‘,‘male‘,18,200),
(‘xiaomage‘,‘female‘,18,204)
;#查看表结构和数据
mysql>desc department;+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+rowsin set (0.19sec)
mysql>desc employee;+--------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| sex | enum(‘male‘,‘female‘) | NO | | male | |
| age | int(11) | YES | | NULL | |
| dep_id | int(11) | YES | | NULL | |
+--------+-----------------------+------+-----+---------+----------------+rowsin set (0.01sec)
mysql> select * fromdepartment;+------+--------------+
| id | name |
+------+--------------+
sql left join 多表连接
| 200 | 技术 |
| 201 | ⼈⼒资源 |
| 202 | 销售 |
| 203 | 运营 |
+------+--------------+rowsin set (0.02sec)
mysql> select * fromemployee;+----+----------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+----------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | nvshen | male | 18 | 200 |
| 6 | xiaomage | female | 18 | 204 |
+----+----------+--------+------+--------+rowsin set (0.00 sec)
View Code
ps:观察两张表,发现department表中id=203部门在employee中没有对应的员⼯,发现employee中id=6的员⼯在department表中没有对应关系。
⼀、多表连接查询
两张表的准备⼯作已完成,⽐如现在我要查询的员⼯信息以及该员⼯所在的部门。从该题中,我们看出既要查员⼯⼜要查该员⼯的部门,肯定要将两张表进⾏连接查询,多表连接查询。
重点:外链接语法
语法:
select 字段列表:from 表1 inner|leftight join 表2
on 表1.字段= 表2.字段;
(1)交叉连接:不适⽤任何匹配条件。
select * from employee,department;
mysql> select * fromemployee,department;+----+----------+--------+------+--------+------+--------------+ | id | name | sex | age | dep_id | id | name |
+----+----------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 1 | egon | male | 18 | 200 | 201 | ⼈⼒资源 |
| 1 | egon | male | 18 | 200 | 202 | 销售 |
| 1 | egon | male | 18 | 200 | 203 | 运营 |
| 2 | alex | female | 48 | 201 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | ⼈⼒资源 |
| 2 | alex | female | 48 | 201 | 202 | 销售 |
| 2 | alex | female | 48 | 201 | 203 | 运营 |
| 3 | wupeiqi | male | 38 | 201 | 200 | 技术 |
| 3 | wupeiqi | male | 38 | 201 | 201 | ⼈⼒资源 |
| 3 | wupeiqi | male | 38 | 201 | 202 | 销售 |
| 3 | wupeiqi | male | 38 | 201 | 203 | 运营 |
| 4 | yuanhao | female | 28 | 202 | 200 | 技术 |
| 4 | yuanhao | female | 28 | 202 | 201 | ⼈⼒资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 4 | yuanhao | female | 28 | 202 | 203 | 运营 |
| 5 | nvshen | male | 18 | 200 | 200 | 技术 |
| 5 | nvshen | male | 18 | 200 | 201 | ⼈⼒资源 |
| 5 | nvshen | male | 18 | 200 | 202 | 销售 |
| 5 | nvshen | male | 18 | 200 | 203 | 运营 |
| 6 | xiaomage | female | 18 | 204 | 200 | 技术 |
| 6 | xiaomage | female | 18 | 204 | 201 | ⼈⼒资源 |
| 6 | xiaomage | female | 18 | 204 | 202 | 销售 |
| 6 | xiaomage | female | 18 | 204 | 203 | 运营 |
+----+----------+--------+------+--------+------+--------------+
24 rows in set (0.00 sec)
View Code
(2)内连接:只连接匹配的⾏ inner join
#两张表共有的部分,相当于利⽤条件从笛卡尔积结果中筛选出了匹配的结果#department没有204这个部门,因⽽employee表中关于204这条员⼯信息没有匹配出来
mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee inner join department on employee.dep_id=department.id;+----+---------+------+--------+--------------+
| id | name | age | sex | name |
+----+---------+------+--------+--------------+
| 1 | egon | 18 | male | 技术 |
| 2 | alex | 48 | female | ⼈⼒资源 |
| 3 | wupeiqi | 38 | male | ⼈⼒资源 |
| 4 | yuanhao | 28 | female | 销售 |
| 5 | nvshen | 18 | male | 技术 |
+----+---------+------+--------+--------------+rowsin set (0.00sec)#上述sql等同于
mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee,department where employee.dep_id=department.id;
(3)外链接之左连接:优先显⽰左表全部记录 left join
#以左表为准,即出所有员⼯信息,当然包括没有部门的员⼯#本质就是:在内连接的基础上增加左边有,右边没有的结果
mysql> select employee.id,employee.name,department.name as depart_name from employee left join department on employee.dep_id=department.id;+----+----------+--------------+
| id | name | depart_name |
+----+----------+--------------+
| 1 | egon | 技术 |
| 5 | nvshen | 技术 |
| 2 | alex | ⼈⼒资源 |
| 3 | wupeiqi | ⼈⼒资源 |
| 4 | yuanhao | 销售 |
| 6 | xiaomage | NULL |
+----+----------+--------------+rowsin set (0.00 sec)
(4) 外链接之右连接:优先显⽰右表全部记录
#以右表为准,即出所有部门信息,包括没有员⼯的部门#本质就是:在内连接的基础上增加右边有,左边没有的结果
mysql> select employee.id,employee.name,department.name as depart_name from employee right join department on employee.dep_id=department.id;+------+---------+--------------+
| id | name | depart_name |
+------+---------+--------------+
| 1 | egon | 技术 |
| 2 | alex | ⼈⼒资源 |
| 3 | wupeiqi | ⼈⼒资源 |
| 4 | yuanhao | 销售 |
| 5 | nvshen | 技术 |
| NULL | NULL | 运营 |
+------+---------+--------------+rowsin set (0.00 sec)
(5) 全外连接:显⽰左右两个表全部记录(了解)
#外连接:在内连接的基础上增加左边有右边没有的和右边有左边没有的结果#注意:mysql不⽀持全外连接 full JOIN#强调:mysql可以使⽤此种⽅式间接实现全外连接
语法:select * from employee left join department on employee.dep_id =department.id
union all
select* from employee right join department on employee.dep_id =department.id;
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
;+------+----------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+------+----------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 5 | nvshen | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | ⼈⼒资源 |
| 3 | wupeiqi | male | 38 | 201 | 201 | ⼈⼒资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 6 | xiaomage | female | 18 | 204 | NULL | NULL |
| NULL | NULL | NULL | NULL | NULL | 203 | 运营 |
+------+----------+--------+------+--------+------+--------------+rowsin set (0.01sec)#注意 union与union all的区别:union会去掉相同的纪录
⼆、符合条件连接查询
⽰例1:以内连接的⽅式查询employee和department表,并且employee表中的age字段值必须⼤于25,即出年龄⼤于25岁的员⼯以及员⼯所在的部门
select employee.name,department.name fromemployee inner join department
on employee.dep_id=department.id
where age> 25;
⽰例2:以内连接的⽅式查询employee和department表,并且以age字段的升序⽅式显⽰。
select employee.id,employee.name,employee.age,department.name fromemployee,department
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论