#查询员工姓名和所在部门名称 SELECT last_name,department_name FROM employees, departments; SELECT last_name,department_name FROM employees CROSS JOIN departments; SELECT last_name,department_name FROM employees INNER JOIN departments; SELECT last_name,department_name FROM employees JOIN departments; # 结果会将last_name所有值和department_name所有值做排列组合
SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1 = table2.column2; #连接条件
- 阿里开发规范:【强制】对于数据库中表记录的查询和变更,只要涉及多个表,都需要在列名前加表的别名(或表名)进行限定。否则后续多表新增同名字段时,导致ambiguous模糊异常。
# 表名前缀 SELECT employees.employee_id, employees.last_name, employees.department_id, departments.location_id FROM employees, departments WHERE employees.department_id = departments.department_id; # 别名 SELECT e.last_name, d.department_id, e.department_id FROM employees e, departments AS d WHERE e.department_id = d.department_id; # 连接多个表,连接 n个表,至少需要n-1个连接条件。 SELECT e.employee_id, e.last_name, e.department_id, d.department_name, d.location_id, l.location_id, l.city FROM employees e, departments d, locations l WHERE e.department_id = d.department_id AND d.location_id = l.location_id ORDER BY e.employee_id, l.location_id;
# 非等值连接 SELECT e.last_name, e.salary, j.grade_level FROM employees AS e, job_grades AS j WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal ORDER BY grade_level;
# CONCAT() 可实现字段和字符串拼接 SELECT CONCAT (worker.last_name,' works for ', manager.last_name) FROM employees worker, employees manager WHERE worker.manager_id = manager.manager_id;
# 内连接(INNER JOIN)的实现 SELECT 字段列表 FROM A表 JOIN B表 ON 关联条件 WHERE 等其他子句;
# 例一 SELECT e.employee_id, e.last_name, e.department_id,d.department_id, d.location_id FROM employees e JOIN departments d ON e.department_id = d.department_id; # 效果相同: SELECT e.employee_id, e.last_name, e.department_id,d.department_id, d.location_id FROM employees e, departments d WHERE e.department_id = d.department_id; # 例二 SELECT employee_id, city, department_name FROM employees e JOIN departments d ON d.department_id = e.department_id JOIN locations l ON d.location_id = l.location_id; # 效果相同 SELECT employee_id, city, department_name FROM employees e, departments d, locations l WHERE e.department_id = d.department_id AND d.location_id = l.location_id;
#左外连接 SELECT 字段列表 FROM A表 LEFT JOIN B表 ON 关联条件 WHERE 等其他子句; #右外连接 SELECT 字段列表 FROM A表 RIGHT JOIN B表 ON 关联条件 WHERE 等其他子句;
#左外连接 SELECT e.last_name, e.department_id, d.department_id, d.department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id;
结果包括employees表的全部行,employees表中的Grant没有department_id,departments表中无对应数值,仍显示在结果中(NULL)。
# 右外连接 SELECT e.last_name, e.department_id, d.department_id, d.department_name FROM employees e RIGHT JOIN departments d ON e.department_id = d.department_id;
结果包括departments表的全部行,employees表中无对应department_id,仍显示在结果中(NULL)。
SELECT column,... FROM table1 UNION SELECT column,... FROM table2 SELECT column,... FROM table1 UNION ALL SELECT column,... FROM table2
SELECT * FROM employees WHERE email LIKE '%a%' OR department_id > 90; SELECT * FROM employees WHERE email LIKE '%a%' UNION SELECT * FROM employees WHERE department_id > 90;
SELECT id,cname FROM t_chinamale WHERE csex='男' UNION ALL SELECT id,tname FROM t_usmale WHERE tGender='male';
1、
# A ∩ B SELECT e.last_name, e.department_id, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id;
2、
SELECT e.last_name, e.departments_id, d.department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id;
3、
SELECT e.last_name, e.departments_id, d.department_name FROM employees e RIGHT JOIN departments d ON e.department_id = d.department_id;
4、
# A - A∩B SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id WHERE d.department_id IS NULL;
5、
# B-A∩B SELECT e.last_name, e.department_id, d.department_name FROM employees e RIGHT JOIN departments d ON e.department_id = d.department_id WHERE e.department_id IS NULL;
6、
# A∪B SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id UNION SELECT e.last_name, e.department_id, d.department_name FROM employees e RIGHT JOIN departments d ON e.department_id = d.department_id WHERE e.department_id IS NULL;
7、
SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id WHERE d.department_id IS NULL UNION SELECT e.last_name, e.department_id, d.department_name FROM employees e RIGHT JOIN departments d ON e.department_id = d.department_id WHERE e.department_id IS NULL
# 1.显示所有员工的姓名,部门号和部门名称。 -- 1.1等值连接WHERE,只显示满足连接条件的结果 SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id; -- 1.2内连接join on,只显示满足连接条件的结果,1/2方法等效 SELECT last_name, e.department_id, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id; -- 1.3外连接left join,显示主表左表的全部结果 -- 因为存在无主管的员工(deptid和deptname填null),结果比1/2更充分 SELECT last_name, e.department_id, d.department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id; # 2.查询90号部门员工的job_id和90号部门的location_id SELECT e.department_id, e.job_id, d.location_id FROM employees e, departments d WHERE e.department_id = 90 AND e.department_id = d.department_id; -- 先通过join on 连接两表,再添加筛选条件 SELECT e.department_id, e.job_id, d.location_id FROM employees e JOIN departments d ON e.department_id = d.department_id WHERE e.department_id = 90; # 3.选择所有有奖金的员工的 last_name , department_name , location_id , city SELECT last_name, d.department_name, d.location_id, city FROM employees e, departments d, locations l WHERE e.commission_pct IS NOT NULL AND e.department_id = d.department_id AND d.location_id = l.location_id; SELECT last_name, d.department_name, d.location_id, city FROM employees e JOIN departments d ON e.department_id = d.department_id JOIN locations l ON d.location_id = l.location_id WHERE commission_pct IS NOT NULL; # 4.选择city在Toronto工作的员工的 last_name , job_id , department_id , department_name SELECT e.last_name, e.job_id, e.department_id, d.department_name FROM employees e, departments d, locations l WHERE e.department_id = d.department_id AND d.location_id = l.location_id AND l.city = 'Toronto'; SELECT e.last_name, e.job_id, e.department_id, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id JOIN locations l ON d.location_id = l.location_id WHERE l.city = 'Toronto'; #5.查询员工所在的部门名称、部门地址、姓名、工作、工资,其中员工所在部门的部门名称为’Executive’ SELECT d.department_name, l.street_address, e.last_name, e.job_id, e.salary FROM employees e, departments d, locations l WHERE e.department_id = d.department_id AND d.location_id = l.location_id AND d.department_name = 'Executive'; SELECT d.department_name, l.street_address, e.last_name, e.job_id, e.salary FROM employees e JOIN departments d ON e.department_id = d.department_id JOIN locations l ON d.location_id = l.location_id WHERE d.department_name = 'Executive'; #6.选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式employees Emp# manager Mgr#kochhar 101 king 100 SELECT e1.last_name, e1.employee_id, e1.manager_id, e2.last_name manager_name FROM employees e1, employees e2 WHERE e1.manager_id = e2.employee_id; SELECT e1.last_name, e1.employee_id, e1.manager_id, e2.last_name manager_name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.employee_id; -- left join on 会包含employee没有manager的结果 SELECT emp.last_name employees, emp.employee_id "Emp#", mgr.last_name manager, mgr.employee_id "Mgr#" FROM employees emp LEFT OUTER JOIN employees mgr ON emp.manager_id = mgr.employee_id; # 7.查询哪些部门没有员工 SELECT d.department_id, d.department_name FROM departments d LEFT JOIN employees e ON d.department_id = e.department_id WHERE e.department_id IS NULL; SELECT d.department_id, d.department_name FROM departments d WHERE NOT EXISTS ( SELECT * FROM employees e WHERE e.department_id = d.department_id); # 8. 查询哪个城市没有部门 SELECT l.location_id, l.city FROM locations l LEFT JOIN departments d ON l.location_id = d.location_id WHERE d.location_id IS NULL; # 9. 查询部门名为 Sales 或 IT 的员工信息 SELECT e.last_name, e.employee_id, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id AND d.department_name IN ('Sales', 'IT');