SELECT e.last_name,e.salary,j.grade_level FROM employees e,job_grades j #where e.`salary` between j.`lowest_sal` and j.`highest_sal`; WHERE e.`salary` >= j.`lowest_sal` AND e.`salary` <= j.`highest_sal`;
#方式1 SELECT*FROM employees WHERE email LIKE'%a%'OR department_id>90;
#方式2 SELECT*FROM employees WHERE email LIKE'%a%' UNION SELECT*FROM employees WHERE department_id>90;
7种SQL JOINS的实现
代码实现
#中图:内连接 A∩B SELECT employee_id,last_name,department_name FROM employees e JOIN departments d ON e.`department_id` = d.`department_id`;
#左上图:左外连接 SELECT employee_id,last_name,department_name FROM employees e LEFTJOIN departments d ON e.`department_id` = d.`department_id`;
#右上图:右外连接 SELECT employee_id,last_name,department_name FROM employees e RIGHTJOIN departments d ON e.`department_id` = d.`department_id`;
#左中图:A - A∩B SELECT employee_id,last_name,department_name FROM employees e LEFTJOIN departments d ON e.`department_id` = d.`department_id` WHERE d.`department_id` ISNULL
#右中图:B-A∩B SELECT employee_id,last_name,department_name FROM employees e RIGHTJOIN departments d ON e.`department_id` = d.`department_id` WHERE e.`department_id` ISNULL
#左下图:满外连接 # 左中图 + 右上图 A∪B SELECT employee_id,last_name,department_name FROM employees e LEFTJOIN departments d ON e.`department_id` = d.`department_id` WHERE d.`department_id` ISNULL UNIONALL #没有去重操作,效率高 SELECT employee_id,last_name,department_name FROM employees e RIGHTJOIN departments d ON e.`department_id` = d.`department_id`;
#右下图 #左中图 + 右中图 A ∪B- A∩B 或者 (A - A∩B) ∪ (B - A∩B) SELECT employee_id,last_name,department_name FROM employees e LEFTJOIN departments d ON e.`department_id` = d.`department_id` WHERE d.`department_id` ISNULL UNIONALL SELECT employee_id,last_name,department_name FROM employees e RIGHTJOIN departments d ON e.`department_id` = d.`department_id` WHERE e.`department_id` ISNULL
语法格式小结
左中图
#实现A - A∩B select 字段列表 from A表 leftjoin B表 on 关联条件 where 从表关联字段 isnulland 等其他子句;
右中图
#实现B - A∩B select 字段列表 from A表 rightjoin B表 on 关联条件 where 从表关联字段 isnulland 等其他子句;
左下图
#实现查询结果是A∪B #用左外的A,union 右外的B select 字段列表 from A表 leftjoin B表 on 关联条件 where 等其他子句 union select 字段列表 from A表 rightjoin B表 on 关联条件 where 等其他子句;
右下图
#实现A∪B - A∩B 或 (A - A∩B) ∪ (B - A∩B) #使用左外的 (A - A∩B) union 右外的(B - A∩B) select 字段列表 from A表 leftjoin B表 on 关联条件 where 从表关联字段 isnulland 等其他子句 union select 字段列表 from A表 rightjoin B表 on 关联条件 where 从表关联字段 isnulland 等其他子句
SELECT employee_id,last_name,department_name FROM employees e JOIN departments d ON e.`department_id` = d.`department_id` AND e.`manager_id` = d.`manager_id`;
在SQL99中可以写成:
SELECT employee_id,last_name,department_name FROM employees e NATURALJOIN departments d;
#关联条件 #把关联条件写在where后面 SELECT last_name,department_name FROM employees,departments WHERE employees.department_id = departments.department_id; #把关联条件写在on后面,只能和JOIN一起使用 SELECT last_name,department_name FROM employees INNERJOIN departments ON employees.department_id = departments.department_id; SELECT last_name,department_name FROM employees CROSSJOIN departments ON employees.department_id = departments.department_id; SELECT last_name,department_name FROM employees JOIN departments ON employees.department_id = departments.department_id; #把关联字段写在using()中,只能和JOIN一起使用 #而且两个表中的关联字段必须名称相同,而且只能表示= #查询员工姓名与基本工资 SELECT last_name,job_title FROM employees INNERJOIN jobs USING(job_id); #n张表关联,需要n-1个关联条件 #查询员工姓名,基本工资,部门名称 SELECT last_name,job_title,department_name FROM employees,departments,jobs WHERE employees.department_id = departments.department_id AND employees.job_id = jobs.job_id; SELECT last_name,job_title,department_name FROM employees INNERJOIN departments INNERJOIN jobs ON employees.department_id = departments.department_id AND employees.job_id = jobs.job_id;
注意:
我们要 控制连接表的数量。多表连接就相当于嵌套 for 循环一样,非常消耗资源,会让 SQL 查询性能下 降得很严重,因此不要连接不必要的表。在许多 DBMS 中,也都会有最大连接表的限制。