1 SQL99
1.1 SQL99语法
select 查询列表 from 表1 [连接类型] join 表2 on 连接条件 where 筛选条件;
- 连接类型:
- 内连接 :inner
- 外连接:
- 左外连接: left outer
- 右外连接:right outer
- 交叉连接:cross
1.2 SQL99语法的内连接
1.2.1 内连接的语法
select 查询列表 from 表1 inner join 表2 on 连接条件 [where 筛选条件];
1.2.2 应用
- 示例:查询员工名和部门名
SELECT e.last_name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id;
- 示例:查询名字中包含e的员工名和工种名
SELECT e.last_name, j.job_title FROM employees e INNER JOIN jobs j ON e.job_id = j.job_id WHERE e.last_name LIKE ‘%e%‘;
- 示例:查询部门个数>3的城市名和部门个数
SELECT l.city, count(*) `count` FROM departments d INNER JOIN locations l ON d.location_id = l.location_id GROUP BY l.city HAVING `count` > 3;
- 示例:查询那个部门的员工个数>3的部门名和员工个数,并按照个数降序
SELECT d.department_name, count(*) 员工个数 FROM employees e INNER JOIN departments d ON e.department_id = d.department_id GROUP BY d.department_name HAVING 员工个数 > 3;
- 示例:查询员工名、部门名、工种名,并按照部门名降序
SELECT e.last_name 员工名, d.department_name 部门名, j.job_title 工种名 FROM employees e INNER JOIN departments d INNER JOIN jobs j ON e.department_id = d.department_id AND e.job_id = j.job_id ORDER BY 部门名;
- 示例:
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/9533062.html
时间: 2024-09-30 19:05:11