# 分页# 返回[0-20)条记录select employee_id,last_name from employees limit0,20;# 返回[20-40)条记录select employee_id,last_name from employees limit20,20;
多表查询
# 查到的结果是employees x departments笛卡尔积,没有实际意义select employee_id,department_name
from employees,departments;# 加上连接筛选条件select employee_id,department_name
from employees,departments
where employees.department_id = departments.department_id;# SQL99语法# inner可以省略select employee_id,department_name
from employees
innerjoin departments on employees.department_id = departments.department_id;# 连接多个表select employee_id,department_name,city
from employees
innerjoin departments on employees.department_id = departments.department_id
innerjoin locations on departments.location_id = locations.location_id;# 建议每个字段都指明所在的表select employees.employee_id,departments.department_name
from employees,departments
where employees.department_id = departments.department_id;# 可以给表起别名select t1.employee_id,t2.department_name
from employees t1,departments t2
where t1.department_id = t2.department_id;
# 自连接# 员工id,姓名及其管理者的id和姓名select emp.employee_id,emp.last_name,mgr.employee_id,mgr.last_name
from employees emp,employees mgr
where emp.manager_id = mgr.employee_id;
# 上面的都是内连接(也称为自然连接,只有两个表相匹配的行才在结果集中出现)# 外连接# 左外连接(左表全部列出,如果左表有的行在右表中没有与之相匹配的,对应的右表中的字段值为null)select last_name,department_name
from employees
leftjoin departments on employees.department_id = departments.department_id;# 右外连接(右表全部列出,...)select last_name,department_name
from employees
rightjoin departments on employees.department_id = departments.department_id;# 满外连接select employee_id,department_name
from employees
leftjoin departments on employees.department_id = departments.department_id
unionallselect employee_id,department_name
from employees
rightjoin departments on employees.department_id = departments.department_id
where employees.department_id isnull;