标签:
学习目标:
Oracle连接
等值连接:
使用连接在多个表中查询数据。
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1= table2.column2;
select e.employee_id,e.last_name,d.department_id from employees e,departments d where e.department_id = d.department_id三个表的连接
select e.employee_id,e.last_name,d.department_id,l.city from employees e,departments d,locations l where e.department_id = d.department_id and d.location_id = l.location_id
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
--左外连接
select e.employee_id,e.last_name,d.department_name from employees e,departments d where e.department_id = d.department_id(+)
--右外连接
select e.employee_id,e.last_name,d.department_name from employees e,departments d where e.department_id(+) = d.department_id
使用连接从多个表中查询数据:
SELECT table1.column, table2.column
FROM table1
[CROSS JOINtable2] |
[NATURAL JOINtable2] |
[JOINtable2 USING(column_name)] |
[JOINtable2 ON(table1.column_name = table2.column_name)] |
[LEFT|RIGHT|FULL OUTER JOINtable2 ON(table1.column_name =table2.column_name)];
select e.employee_id,e.last_name,d.department_name from employees e natural join departments d
使用
USING子句创建连接:
JOIN和USING子句经常同时使用。
select e.employee_id,e.last_name,d.department_name from employees e join departments d using(department_id)
这种方法有局限性:如果两个表中的列名(一个叫department_id,另外一个叫id)不一样,这个方法就失效了。
使用ON子句创建连接(常用):
<pre name="code" class="sql">select e.employee_id,e.last_name,d.department_name from employees e join departments d on e.department_id = d.department_id这个和等值连接是很相似的
select e.employee_id,e.last_name,d.department_name,l.city from employees e <strong>join departments d</strong> on e.department_id = d.department_id <strong>join locations l</strong> on d.location_id = l.location_id
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u014338577/article/details/47687605