前言: 由于大一学习数据库不扎实,学到后面有点吃力,所以回过头来认真学习一边,写一些学习总结,提醒自己。也要告诫读者,把
基础打扎实
。读者觉得有用的话可以收藏点赞哦!
上一篇:MySQL函数查询——MySQL 基础查询你会了吗?
年代划分 分sql92 和sql99语法,sql92语法支持内连接,不支持外连接。
sql99 支持内连接,也支持外连接和交叉连接。
1、等值连接
# 案例1:查询女神名和对应的男神名
SELECT `name`,boyName
FROM boys,beauty
WHERE beauty.boyfriend_id=boys.id
# 案例二: 查询员工名和对应的部门名
SELECT CONCAT(last_name,first_name),department_name
FROM departments,employees
WHERE employees.department_id=departments.department_id
/*
注意: 如果为表起了别名,则查询的字段就不能使用原来的表明去限定
*/
# 查询员工名,工种号,工种名
SELECT last_name,e.job_id,job_title
FROM employees e,jobs j
WHERE e.job_id=j.job_id
3、可以加筛选吗? 可以d
# 案例一: 查询有奖金的员工名和部门名
SELECT e.last_name,d.department_name,e.commission_pct
FROM employees e,departments d
WHERE e.commission_pct IS NOT NULL AND e.department_id=d.department_id
# 案例二:城市名中第二个字符为O的部门名和城市名
SELECT department_name,city
FROM departments d,locations l
WHERE d.location_id=l.location_id AND city LIKE '_o%'
4、可以加分组吗?
# 案例1、查询每个城市的部门个数
SELECT COUNT(*),city
FROM locations l,departments d
WHERE d.location_id=l.location_id
GROUP BY city
# 案例2、查询有奖金的每个部门的部门名和部门领导编号和该部门的最低工资
SELECT d.department_name,d.manager_id,MIN(e.salary)
FROM departments d,employees e
WHERE commission_pct IS NOT NULL AND d.department_id=e.department_id
GROUP BY 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(*) DESC
案例: 查询员工名、部门名和所在的城市
SELECT e.last_name,d.department_name,l.city
FROM employees e,departments d,locations l
WHERE e.department_id=d.department_id AND d.location_id=l.location_id
案例1、查询员工的工资和工资级别
SELECT salary,grade_level
FROM employees e,job_grades j
WHERE salary BETWEEN j.lowest_sal AND j.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
# 案例1、查询员工名,部门名
SELECT last_name,department_name
FROM employees e
INNER JOIN departments d
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 COUNT(*)>3
# 案例4、查询那个部门的部门员工个数>3的部门名和员工个数,并按个数降序
SELECT department_name,COUNT(*)
FROM departments d
INNER JOIN employees e
ON d.department_id=e.department_id
GROUP BY department_name
HAVING COUNT(*)>3
ORDER BY Count(*) desc
# 案例5、查询员工名、部门名、工种名、并按部门名降序
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
# 查询工资级别的个数>20的个数,并且按工资级别进行降序
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 e.last_name 员工,m.last_name 领导
FROM employees e
INNER JOIN employees m
ON e.manager_id=m.employee_id
# 案例: 查询 员工名和上级的名称
SELECT e.last_name 员工,m.last_name 领导
FROM employees e
INNER JOIN employees m
ON e.manager_id=m.employee_id
WHERE e.last_name LIKE '%k%'
外连接 只有sql99语法支持
应用场景: 一个表中有另一个表中没有的记录。
特点:
1、外连接的查询结果为主表的所有记录
如果从表中有和它匹配的,则显示匹配的值。
如果从表中没有和它匹配,则显示为null
外连接查询结果为=内连接查询结果+主表中有而从表中没有的。
2、左外连接,LEFT OUTER JOIN 左边的表是主表
右外连接,RIGHT JOIN 右边的表是主表3、 左外和右外可以实现同样的效果
引入: 查询没有男朋友的女神名
SELECT b.`name`
FROM beauty b
LEFT OUTER JOIN boys bo
ON b.boyfriend_id=bo.id
WHERE bo.id IS NULL
SELECT b.`name`
FROM boys bo
RIGHT OUTER JOIN beauty b
ON b.boyfriend_id=bo.id
WHERE bo.id IS NULL
SELECT b.*,bo.*
FROM boys bo
LEFT OUTER JOIN beauty b
ON b.boyfriend_id=bo.id
WHERE b.id IS NULL
案例一: 查询哪个部门没有员工
# 左外
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
# 全外连接(不支持)
SELECT b.*,bo.*
FROM beauty b
FULL OUTER JOIN boys bo
ON b.boyfriend_id=bo.id
SELECT b.*,bo.*
FROM beauty b
CROSS JOIN boys bo