Mysql 连表查询

# 连表查询
/*
按功能分类: 
内连接
--等值连接
--非等值连接
--自连接	
外连接
--左外连接
--右外连接
--全外连接
交叉连接

*/

#内连接
-- 等值连接【两个表中的交集部分】
-- 查询女生名和对应的男生名
SELECT NAME, boyName
FROM boys,beauty
WHERE beauty.`boyfriend_id`=boys.`id`;

-- 查询员工名和对应的部门名
SELECT`last_name`,`department_name`
FROM`employees`,`departments`
WHERE employees.`department_id`=departments.`department_id`;

# 查询员工名,工种号,工种名--两张表中有相同字段时候,容易有歧义,需要起别名进行分别
SELECT last_name,employees.job_id,job_title
FROM employees,jobs
WHERE employees.`job_id`=jobs.`job_id`;

# 可以添加筛选
-- 查询有奖金的员工名,部门名
SELECT`last_name`,`department_name`,commission_pct
FROM `departments` d,`employees` e
WHERE d.`department_id`=e.`department_id` AND e.`commission_pct`IS NOT NULL ;
-- 查询城市名中第二个字符为o的部门名和城市名;
SELECT `city`,`department_name`
FROM `locations` l,`departments` d
WHERE l.`location_id`= d.`location_id`-- 两表中相同的字段
AND `city`LIKE ‘_o%‘;
-- 添加分组
-- 查询每个城市的部门个数
SELECT COUNT(*) 个数, city
FROM `departments` d,`locations` l
WHERE d.`location_id`=l.`location_id`
GROUP BY city;

-- 查询有奖金的每个部门的部门名和部门的领导编号和该部门的最低工资

SELECT department_name,d.`manager_id`,MIN(`salary`)
FROM `departments` d,`employees` e
WHERE d.`department_id`=e.`department_id`
AND e.`commission_pct` IS NOT NULL
GROUP BY d.`department_name`,d.`manager_id`;

# 添加排序
-- 查询每个工种的工种名和员工个数,并且按照员工个数排序

SELECT  `job_title`,COUNT(*)
FROM`employees` e,`jobs` j
WHERE e.`job_id`= j.`job_id`
GROUP BY `job_title`
ORDER BY COUNT(*) ASC;


# 实现三表连接

-- 查询员工名,部门名 和所在的城市
SELECT last_name,department_name,city
FROM employees e,departments d,locations l
WHERE e.`department_id`=d.`department_id`AND d.`location_id`=l.`location_id`;




# 工资等级表

CREATE TABLE job_grades
(grade_level VARCHAR(3),
 lowest_sal  INT,
 highest_sal INT);

INSERT INTO job_grades
VALUES (‘A‘, 1000, 2999);

INSERT INTO job_grades
VALUES (‘B‘, 3000, 5999);

INSERT INTO job_grades
VALUES(‘C‘, 6000, 9999);

INSERT INTO job_grades
VALUES(‘D‘, 10000, 14999);

INSERT INTO job_grades
VALUES(‘E‘, 15000, 24999);

INSERT INTO job_grades
VALUES(‘F‘, 25000, 40000);

-- 非等值连接
-- 查询出员工的工资和工资级别
SELECT `salary`,`grade_level`
FROM employees e,job_grades j
WHERE salary BETWEEN lowest_sal AND highest_sal;



-- 自连接【只涉及到该表本身】
-- 查询员工名 和其对应的领导编号
SELECT e.`employee_id`,e.`last_name`,m.`employee_id`,m.`last_name`
FROM `employees` e,`employees` m
WHERE e.`manager_id`=m.`employee_id`;

/*
99 语法
语法
select 查询列表
from 表1 别名 【连接类型】
join 表2 别名
on 连接条件
where
group by
having
order by

*/	
# 内连接 -- inner
/*
select 查询列表
from 表1 别名 inner
join 表2 别名
on 连接条件


*/
-- 等值连接
-- 查询员工名,部门名字
SELECT `last_name`,`department_name`
FROM `employees` e JOIN `departments` d
ON e.`department_id`=d.`department_id`;
-- 查询名字中包含e的员工名字和工种名[添加筛选]
SELECT `last_name`,`job_title`
FROM `employees` e INNER
JOIN `jobs` j 
ON e.`job_id`=j.`job_id`
WHERE `last_name` LIKE ‘%e%‘;

-- 查询部门个数大于3的城市名和部门个数[分组筛选]

SELECT city ,COUNT(*)
FROM `locations` l
INNER JOIN `departments` d
ON l.`location_id`=d.`location_id`
GROUP BY city
HAVING COUNT(*)>3;

-- 查询那个部门的部门员工个数大于3的部门名和员工个数
-- 并按照个数降序排列
SELECT department_name,COUNT(*)
FROM employees e
INNER JOIN `departments` d
ON e.`department_id`=d.`department_id`
GROUP BY d.department_id
HAVING COUNT(*)>3
ORDER BY COUNT(*) DESC ;

-- 查询员工名,部门名,工种名,并按部门降序

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;

# 非等值连接
-- 查询员工的工资级别
SELECT salary,grade_level
FROM employees e
INNER JOIN job_grades g
ON e.`salary` BETWEEN g.`lowest_sal`AND g.`highest_sal`;

-- 查询每个工资级别的个数大于2的个数,并且按工资级别降序排列
SELECT COUNT(*),grade_level
FROM employees e
INNER 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;


#外连接-- 用于查询一个表中有,而另一个表中没有 
/*




*/

-- 没有男朋友的女神
-- 主表一般指你查询结果主要来源的那个表。
SELECT beauty.`name`,`boys`.*
FROM beauty
LEFT OUTER JOIN boys 
ON beauty.`boyfriend_id`=boys.`id`
WHERE boys.id IS NULL;

-- 查询哪个部门没有员工
SELECT departments.*,employees.`employee_id`
FROM departments
LEFT JOIN employees
ON departments.`department_id`=employees.`department_id`
WHERE employees.`department_id` IS NULL;


-- 左外连接-- left outer ,left join左边的是主表
-- 右外连接 -- right outer,right join右边是主表
-- 全外连接 full outer
#交叉连接 cross

# 子查询 出现在其他语句中的select 语句

-- 分类
/*
出现的位置
select 后面 仅仅支持标量子查询
from 后面 表子查询
where后面 标量,列子查询,行子查询
having 后面标量,列子查询,行子查询
exists 后面(相关子查询) 表子查询

按结果集的行列数不同:
标量子查询 一行一列
列子查询 一列多行
行子查询  多(一)行多列
表子查询 结果集

*/

# where 或having 后面
-- 标量
-- 列
-- 行
/*
1. 子查询一般放在小括号中
子查询一般放在条件的右侧
标量子查询,一般搭配着单行操作符使用
大于,小于,等于
列子查询,一般搭配着多行操作符使用
in,any/some,all


*/

-- 谁的工资比abel 工资高

SELECT * FROM employees 
WHERE employees.`salary`>
(SELECT `salary` FROM employees 
WHERE employees.`last_name`=‘Abel‘);

-- 返回job——id与141号员工相同,
-- salary 比143号员工多的员工,姓名,job——id和工资
SELECT job_id
FROM employees
WHERE `employee_id`=141;


SELECT `salary`
FROM employees
WHERE `employee_id`=143;

SELECT `last_name`,`job_id`,`salary`
FROM employees
WHERE `job_id`=(SELECT job_id
FROM employees
WHERE `employee_id`=141) 
AND salary>(SELECT `salary`
FROM employees
WHERE `employee_id`=143);


-- 返回公司工资最少的员工的姓名和工资


SELECT MIN(salary)
FROM employees;

SELECT `last_name`,job_id,`salary`
FROM employees
WHERE salary =(SELECT MIN(salary)
FROM employees);


-- 查询最低工资大于50号部门最低工资的部门id和
-- 其最低工资


SELECT MIN(salary),`department_id`
FROM`employees`
WHERE `department_id`=50;	

SELECT MIN(salary),department_id
FROM employees
GROUP BY `department_id`;


SELECT MIN(salary),department_id
FROM employees
GROUP BY `department_id`
HAVING MIN(salary)>(
SELECT MIN(salary)
FROM`employees`
WHERE `department_id`=50
);


-- 列子查询--一列多行
-- 返回locationid是1400或
-- 1700的部门中的所有员工姓名

SELECT `department_id`
FROM `departments`
WHERE `location_id` IN(1400,1700);


SELECT `last_name`
FROM `employees`
WHERE `department_id` 
IN (SELECT `department_id`
FROM `departments`
WHERE `location_id` IN(1400,1700));

-- 返回其他工种中 比jobid为itprog‘部门任一工资低的员工的
-- 工号,姓名,jobid 以及salary

SELECT DISTINCT salary
FROM `employees`
WHERE `job_id`=‘IT_PROG‘;


SELECT `last_name`,employee_id,salary
FROM employees
WHERE salary<ANY(SELECT DISTINCT salary
FROM `employees`
WHERE `job_id`=‘IT_PROG‘)
AND job_id !=‘IT_PROG‘;



-- 行子查询

-- 查询员工编号,且员工共最高的信息
SELECT MIN(employee_id)
FROM employees;
SELECT MAX(salary)
FROM employees;

SELECT * FROM employees WHERE `employee_id`=(SELECT MIN(employee_id)
FROM employees) AND salary=(SELECT MAX(salary)
FROM employees);

# select 后面的查询

-- 查询每个部门的员工数
SELECT d.*,(
SELECT COUNT(*)
FROM employees e
WHERE e.department_id =d.`department_id`
)
FROM `departments` d

  

Mysql 连表查询

上一篇:db#mongodb#start


下一篇:JDBC-02