MySQL에서 다중 테이블 쿼리를 구현하는 방법은 무엇입니까? MySQL 다중 테이블 쿼리 문

青灯夜游
풀어 주다: 2018-10-27 16:57:21
앞으로
7537명이 탐색했습니다.

이 기사에서는 MySQL이 다중 테이블 쿼리를 구현하는 방법을 소개합니다. MySQL 다중 테이블 쿼리 문. 도움이 필요한 친구들이 참고할 수 있기를 바랍니다.

Create Table

# 创建表
create table department(id int,name varchar(20));
create table employee1(
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 employee1(name,sex,age,dep_id) values('egon','male',18,200),('alex','female',48,201),('tom','male',38,201),('yuanhao','female',28,202),('lidawei','male',18,200),('jinkezhou','female',18,204);

# 查看表
mysql> select * from employee1;
+----+-----------+--------+------+--------+
| id | name      | sex    | age  | dep_id |
+----+-----------+--------+------+--------+
|  1 | egon      | male   |   18 |    200 |
|  2 | alex      | female |   48 |    201 |
|  3 | tom       | male   |   38 |    201 |
|  4 | yuanhao   | female |   28 |    202 |
|  5 | lidawei   | male   |   18 |    200 |
|  6 | jinkezhou | female |   18 |    204 |
+----+-----------+--------+------+--------+
6 rows in set (0.00 sec)
mysql> select * from department;
+------+--------------+
| id   | name         |
+------+--------------+
|  200 | 技术       |
|  201 | 人力资源   |
|  202 | 销售       |
|  203 | 运营       |
+------+--------------+
4 rows in set (0.00 sec)
로그인 후 복사

Multiple Table Join Query

Cross Join

Cross Join: 일치 기준이 적용되지 않습니다. 데카르트 곱 생성

mysql> select * from employee1 ,department;
로그인 후 복사

내부 조인

내부 조인: 두 테이블의 공통 부분을 찾습니다. 이는 조건을 사용하여 데카르트 곱 결과에서 올바른 결과를 필터링하는 것과 같습니다. (일치하는 행만 연결)

# 找两张表共有的部分,相当于利用条件从笛卡尔积结果中筛选出了正确的结果
#department没有204这个部门,因而employee表中关于204这条员工信息没有匹配出来
mysql> select * from employee1,department where employee1.dep_id=department.id;

#上面用where表示的可以用下面的内连接表示,建议使用下面的那种方法
mysql> select * from employee1 inner join department on employee1.dep_id=department.id;

# 也可以这样表示哈
mysql> select employee1.id,employee1.name,employee1.age,employee1.sex,department.name from employee1,department where employee1.dep_id=department.id;
로그인 후 복사

Left Join left

왼쪽 테이블의 모든 레코드를 우선적으로 표시합니다.

#左链接:在按照on的条件取到两张表共同部分的基础上,保留左表的记录
mysql> select * from employee1 left join department on department.id=employee1.dep_id;

mysql> select * from department left join  employee1 on department.id=employee1.dep_id;
로그인 후 복사

오른쪽 연결 오른쪽

오른쪽 테이블의 모든 레코드를 먼저 표시합니다.

#右链接:在按照on的条件取到两张表共同部分的基础上,保留右表的记录
mysql> select * from employee1 right join department on department.id=employee1.dep_id;
mysql> select * from department right join employee1 on department.id=employee1.dep_id;
로그인 후 복사

All Join Join

mysql> select * from department full join employee1;
로그인 후 복사

조건을 충족하는 다중 테이블 쿼리

예제 1: 내부 조인을 사용하여 직원 및 부서 테이블을 쿼리하고 직원 테이블의 연령 필드 값은 다음과 같아야 합니다. 25보다 크다,
즉, 회사 전 부서에서 25세 이상인 사원을 찾아낸다

mysql> select * from employee1 inner join department on employee1.dep_id=department.id and age>25;
로그인 후 복사

예 2: 내부 조인을 이용하여 사원 테이블과 부서 테이블을 쿼리하고, 연령 필드의 오름차순으로 표시한다.

mysql> select * from employee1 inner join department on employee1.dep_id=department.id and age>25 and age>25 order by age asc;
로그인 후 복사

Subquery

#1:子查询是将一个查询语句嵌套在另一个查询语句中。
#2:内层查询语句的查询结果,可以为外层查询语句提供查询条件。
#3:子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
#4:还可以包含比较运算符:= 、 !=、> 、<等
로그인 후 복사

예 :

# 查询平均年龄在25岁以上的部门名
mysql> select name from department where id in ( select dep_id from employee1 group by dep_id having avg(age) > 25 );

# 查看技术部员工姓名
mysql> select name from employee1 where dep_id = (select id from department where name='技术');

# 查看小于2人的部门名
mysql> select name from department where id in (select dep_id from employee1 group by dep_id having count(id) < 2) union select name from department where id not in (select distinct dep_id from employee1);
# 提取空部门                              
#有人的部门
mysql> select * from department where id not in (select distinct dep_id from employee1);
로그인 후 복사

위 내용은 MySQL에서 다중 테이블 쿼리를 구현하는 방법은 무엇입니까? MySQL 다중 테이블 쿼리 문의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:cnblogs.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!