本文已参加「新人创造礼」活动,一起开启创造之路

Mysql数据库根底系列

软件下载地址 提取码:7v7u 数据下载地址 提取码:e6p9

一、基本语法

又称多表查询,当查询的字段分别来自于不同表时,就会用到衔接查询。衔接查询有sql92和sql99语法,sql92规范仅仅支撑内衔接,因而本文首要介绍sql99语法。

1.1sql99语法首要如下:

select coloun
from table1 衔接类型
join table 2
on 衔接条件

1.2首要分类:

内衔接:inner
外衔接:
	左外:left outer
	右外:right outer
	全外(用的较少)
交叉衔接:cross(用的较少)相当于做了一个笛卡尔乘积

Mysql数据库基础第二章:(六)连接查询

图1 内衔接

Mysql数据库基础第二章:(六)连接查询

图2 右外衔接
Mysql数据库基础第二章:(六)连接查询
图3 左外衔接
Mysql数据库基础第二章:(六)连接查询
图4 全外衔接

二、内衔接

内衔接可以增加排序、分组、挑选等 挑选条件

2.1基本语法

select column
from table 1
inner join table 2
on 衔接条件

2.2 分类:

  • 等值衔接
# 1 查询职工名、部分名
# 其间职工名在employees表,部分名在departments表
SELECT last_name,department_name
FROM departments d
JOIN  employees e
ON e.`department_id` = d.`department_id`;
#2.查询名字中包含e的职工名和工种名(增加挑选)
SELECT last_name,job_title
FROM employees e
INNER JOIN jobs j
ON e.`job_id`=  j.`job_id`
WHERE e.`last_name` LIKE '%e%';
#3. 查询部分个数>3的城市名和部分个数,(增加分组+挑选)
SELECT city, COUNT(*) 部分个数 
FROM departments d
INNER	JOIN locations l
ON d.`location_id` = l.`location_id`
GROUP BY city
HAVING 部分个数 > 3;
#4.查询职工名、部分名、工种名,并按部分名降序(增加三表衔接)
SELECT last_name,department_name,job_title
FROM employees e
INNER JOIN departments d ON e.`department_id`=d.`department_id`
INNER JOIN jobs j ON e.`job_id` = j.`job_id`
ORDER BY department_name DESC;
  • 非等值衔接
#1. 查询工资级别的个数>20的个数,而且按工资级别降序
 SELECT COUNT(*),grade_level
FROM employees e
 JOIN job_grades g
 ON e.`salary` BETWEEN g.`lowest_sal` AND g.`highest_sal`
 GROUP BY grade_level
 HAVING COUNT(*)>20
 ORDER BY grade_level DESC;
  • 自衔接
#1.查询职工的名字、上级的名字
 SELECT e.last_name,m.last_name
 FROM employees e
 JOIN employees m
 ON e.`manager_id`= m.`employee_id`;
#2.查询名字中包含字符k的职工的名字、上级的名字 
 SELECT e.last_name,m.last_name
 FROM employees e
 JOIN employees m
 ON e.`manager_id`= m.`employee_id`
 WHERE e.`last_name` LIKE '%k%';

三、 外衔接

用于查询一个表中有,另一个表没有的记载 外衔接的查询成果为主表中的一切记载 假如从表中有和它匹配的,则显现匹配的值 假如从表中没有和它匹配的,则显现null 外衔接查询成果=内衔接成果+主表中有而从表没有的记载 全外衔接=内衔接的成果+表1中有但表2没有的+表2中有但表1没有的

3.1 左外衔接

# 查询没有职工的部分的信息
 SELECT d.*,e.employee_id
 FROM departments d
 LEFT OUTER JOIN employees e
 ON d.`department_id` = e.`department_id`
 WHERE e.`employee_id` IS NULL;

3.2 右外衔接

 SELECT d.*,e.employee_id
 FROM employees e
 RIGHT OUTER JOIN departments d
 ON d.`department_id` = e.`department_id`
 WHERE e.`employee_id` IS NULL;

上述两种方法得到成果共同,部分信息在departments表中,departments表没有employee_id,这时我们想要的成果是部分信息+职工id,因而,假如使用左外衔接,则departments为主表,右外衔接则employees为主表。

四、操练

#1.显现一切职工的名字,部分号和部分名称。
SELECT last_name,d.department_id,department_name
FROM employees e,departments d
WHERE e.`department_id` = d.`department_id`;
#2.查询90号部分职工的job_id和90号部分的location_id
SELECT job_id,location_id
FROM employees e,departments d
WHERE e.`department_id`=d.`department_id`
AND e.`department_id`=90;
#3.	选择一切有奖金的职工的 last_name , department_name , location_id , city
SELECT last_name , department_name , l.location_id , city
FROM employees e,departments d,locations l
WHERE e.department_id = d.department_id
AND d.location_id=l.location_id
AND e.commission_pct IS NOT NULL;
#4.选择city在Toronto作业的职工的
last_name , job_id , department_id , department_name 
SELECT last_name , job_id , d.department_id , department_name 
FROM employees e,departments d ,locations l
WHERE e.department_id = d.department_id
AND d.location_id=l.location_id
AND city = 'Toronto';
#5.查询每个工种、每个部分的部分名、工种名和最低工资
SELECT department_name,job_title,MIN(salary) 最低工资
FROM employees e,departments d,jobs j
WHERE e.`department_id`=d.`department_id`
AND e.`job_id`=j.`job_id`
GROUP BY department_name,job_title;
#6.查询每个国家下的部分个数大于2的国家编号
SELECT country_id,COUNT(*) 部分个数
FROM departments d,locations l
WHERE d.`location_id`=l.`location_id`
GROUP BY country_id
HAVING 部分个数>2;
#7、选择指定职工的名字,职工号,以及他的管理者的名字和职工号,成果类似于下面的格局
employees	Emp#	manager	Mgr#
kochhar		101	    king	100
SELECT e.last_name employees,e.employee_id "Emp#",m.last_name manager,m.employee_id "Mgr#"
FROM employees e,employees m
WHERE e.manager_id = m.employee_id
AND e.last_name='kochhar';