使用连接
SQL 1999
SELECT table1.column, table2.column
FROM table1
[NATURAL JOIN table2] |
[JOIN table2 USING (column_name)] |
[JOIN table2
ON (table1.column_name = table2.column_name)]|
[LEFT|RIGHT|FULL OUTER JOIN table2
ON (table1.column_name = table2.column_name)]|
[CROSS JOIN table2];

自然连接:
SELECT department_id, department_name, location_id, city
FROM departments NATURAL JOIN locations ;

select department_id, department_name,department.location_id, city
from departments, locations
where departments.location_id = locations.location_id

create table t1(c1 number, c2 number, c3 number, c4 number);
create table t2(c1 number, c2 number, c3 number);
insert into t1 values(1,1,1,1);
insert into t1 values(2,2,11,11);
insert into t1 values(3,3,3,4);
insert into t2 values(1,11,11);
insert into t2 values(1,11,11);
insert into t2 values(2,2,111);
insert into t2 values(2,2,111);
insert into t2 values(3,3,3);
insert into t2 values(3,3,3);
select * from t1 natural join t2;
select t1.c1,t1.c2,t1.c3 from t1,t2
where t1.c1 = t2.c1
and t1.c2 = t2.c2
and t1.c3 = t2.c3

using子句
select * from t1 join t2 using(c1);
select * from t1 join t2 using(c1,c2);
oracle语法
select t1.c1,t1.c2,t1.c3 from t1,t2 where t1.c1 = t2.c1
select t1.c1,t1.c2,t1.c3 from t1,t2 where t1.c1 = t2.c1 and t1.c2 = t2.c2;

SELECT employee_id, last_name,
location_id, department_id
FROM employees JOIN departments
USING (department_id) ;

using列出现在别的位置,不能用别名限定
SELECT l.city, d.department_name
FROM locations l JOIN departments d
USING (location_id)
WHERE d.location_id = 1400; (Not OK)

SELECT l.city, d.department_name
FROM locations l JOIN departments d
USING (location_id)
WHERE location_id = 1400; (OK)

on子句
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, department_id, d.location_id
FROM employees e JOIN departments d
USING (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;

oracle语法
SELECT employee_id, city, department_name
FROM employees e,departments d, locations l
WHERE d.department_id = e.department_id
AND d.location_id = l.location_id;

在join中应用额外条件
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)
AND e.manager_id = 149 ;

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)
WHERE e.manager_id = 149 ;

自连接:
SELECT worker.last_name emp, manager.last_name mgr
FROM employees worker JOIN employees manager
ON (worker.manager_id = manager.employee_id);

非等值连接
select ename, sal, grade
from emp join salgrade
on (sal between losal and hisal);

select ename, sal, grade
from emp,salgrade
where sal between losal and hisal;

外连接
create table e as select * from emp;
create table d as select * from dept;
select distinct deptno from e;
select deptno from d;
select ename, e.deptno,d.deptno from e join d on (e.deptno = d.deptno);
select ename, e.deptno,d.deptno from e right join d on (e.deptno = d.deptno);
select ename, e.deptno,d.deptno from e,d where e.deptno(+) = d.deptno;

insert into e values(8888,'ZhangHua','salesman',7698,'08-SEP-87',800,0, 50);
select ename, e.deptno,d.deptno from e left join d on (e.deptno = d.deptno);
select ename, e.deptno,d.deptno from e,d where e.deptno = d.deptno(+);

交叉连接(笛卡尔积)
SELECT ename,dname FROM emp cross join dept;
SELECT ename,dname FROM emp,dept;

Oracle SQL Lesson (6) - 使用Join进行联合查询的更多相关文章

  1. SQL Server中INNER JOIN与子查询IN的性能测试

    这个月碰到几个人问我关于"SQL SERVER中INNER JOIN 与 IN两种写法的性能孰优孰劣?"这个问题.其实这个概括起来就是SQL Server中INNER JOIN与子 ...

  2. Oracle SQL——inner jion;left join;right join的区别和使用场景

    背景 在一次面试的时候,面试官让我说一下这三者的使用场景和区别,当时瞬间懵逼,哈哈.回来赶快看一看,记下来. 详解 inner join 等值查询:返回两张表中,联结字段值相等的组合记录 举例:所有学 ...

  3. Oracle SQL Lesson (9) - 操作数据(增删改)

    使用INSERT语句INSERT INTO table [(column [, column...])]VALUES (value [, value...]); INSERT INTO departm ...

  4. Oracle SQL Lesson (2) - 限制和排序数据

    重建scott用户@?/rdbms/admin/utlsampl.sql@--执行?--$ORACLE_HOME 字符区分大小写:SELECT last_name, job_id, departmen ...

  5. Oracle SQL Lesson (1) - 使用SQL Select语句获取数据

    第一节课: 启动数据库并且使用特定用户连接:su - oracle; 启动sqlplus并且使用sys连接:conn / as sysdba; 启动数据库:startup; 解锁用户:alter us ...

  6. Oracle SQL Lesson (3) - 使用单行函数自定义输出

    大小写转换函数LOWER('SQL Course') = sql courseUPPER('SQL Course') = SQL COURSEINITCAP('SQL Course') = Sql C ...

  7. Oracle SQL Lesson (4) - 使用转换函数和条件表达式

    隐式转换select * from emp where empno='7788'字符(char,varchar2)转换为数字(number)或日期(date)数字或日期转换为字符 显式转换字符转换为数 ...

  8. Oracle SQL Lesson (11) - 创建其他数据库对象(试图/序列/索引/同义词)

    schema(模式)一个用户下一组对象的集合,一般与用户名一致. 视图 CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias].. ...

  9. Oracle SQL Lesson (5) - 使用组函数输出聚合数据

    组函数AVGCOUNTMAXMINSUMVARIANCE:方差STDDEV:标准差 SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary)F ...

随机推荐

  1. iOS_时间相关

    计算两个时间之差,并以倒计时的方式显示: 比如:剩余XX天XX小时XX分XX秒 效果图例如以下: NSDate的分类: 使用场景: watermark/2/text/aHR0cDovL2Jsb2cuY ...

  2. HTML中Id和Name的区别

    源地址:http://www.cnblogs.com/laodai/articles/2244215.html 在html中:name指的是用户名称,ID指的是用户注册是系统自动分配给用户的一个序列号 ...

  3. Delphi事件的广播2

    上篇文章写了将事件分离成类的方法来实现事件的广播,这次将参考观察者模式来实现事件的广播.模式中主要有这两个角色: 发布者:发布者保存着一张观察者的列表,以便在必要的时候调用观察者的方法. 观察者:观察 ...

  4. jquery clone方法

    引用自http://www.w3school.com.cn/tiy/t.asp?f=jquery_manipulation_clone <html> <head> <sc ...

  5. MIPS平台目标机安装Oprofile时出现error

    在Debian下安装Oprofile 重要:应该使linux内核选项支持 在 .config 文件中设置CONFIG_PROFILING=y和CONFIG_OPROFILE=y. 重新编译,烧写.使用 ...

  6. python中逐行读取文件的最佳方式_Drupal_新浪博客

    python中逐行读取文件的最佳方式_Drupal_新浪博客 python中逐行读取文件的最佳方式    (2010-08-18 15:59:28)    转载▼    标签:    python   ...

  7. 彻底删除&quot;提示删除文件和目录&quot;时出错的文件或目录

    当删除文件是出现上图时  能够用以下的方法删除文件 策略一:系统大法 第一招  进程帮你搞定 很多时候乱码文件名称里的文件是explorer.exe进程联系在一起的. 假设要删除的话能够这样:首先命令 ...

  8. win7如何清理图标缓存

    rem 关闭Windows外壳程序explorer taskkill /f /im explorer.exe rem 清理系统图标缓存数据库 attrib -h -s -r "%userpr ...

  9. Extjs4.2 Desktop 拖动黑色和白色的桌面图标的解决方案

    最近做了一个extjs4.2的desktop桌面demo,该desktop从原来的包中剥离出来,并实现了桌面图标休息,拖动桌面图标,但是,用户抱怨拖动桌面图标会出现黑色和白色,测试,在 extjs4. ...

  10. Oracle Client: TNS: Connect timeout ocurred.

    1. 检查Oracle Server 的防火墙是否关闭. 2. Client, Server 重启.