连接查询:

连接查询是指基于多张表或视图的查询。使用连接查询时,应指定有效的查询条件,不然可能会导致生成笛卡尔积。如现有部门表dept,员工表emp,以下查询因查询条件无效,而产生笛卡尔积:   (各语句中字段不做解释,主要显示逻辑关系)

select dept.dname,emp.ename from dept,emp where dept.name = 'sales';

有效查询条件应该指定dept表与emp表之间的连接关系。表之间连接关系多如下:

1.相等连接(=)

相等连接主要用于查询主从表之间的相关数据,如下:

select table1.col1,table2.col2 from table1,table2 where table1.col1 = table2.col2  ;

同时可以用and指定其他查询条件,如修改第一句:

select dept.dname,emp.ename from dept,emp where dept.deptno = emp.deptno and dept.name = 'sales';

2.不等连接

不等连接指连接条件中使用除相等比较的其他比较操作,多用于在不同表之间显示特定范围信息。如下:

select a.enma, a.sal .b.grade from emp a, salgrade b where a.sal between b.losal and b.hisal;

3.自连接

指在同一张表中的连接查询,主要用在自参照表上显示上下级关系或者层次关系。自参照表是指在不同列之间具有参照关系或者主从关系的表。如下,emp表中包含具有主从关系的两个字段,empno(雇员号),mgr(管理者号),为显示雇员与管理者之间的对应关系,可以用自连接:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOoAAABoCAIAAABwjvvKAAAIWklEQVR4nO2dyXLcOAyG9SA55u3zHOMth/HSqRy95ZqqeQPMoR2aTQIgSAFcFPzV5ZIgLgD5iaJEdXv7599n//hn0c823AP/+Kf54/j6Z+HPB75wRP38+XO0CybyuIKOjO+PHz9Gu2AijyvoA99vLtf0IvFVOHfm0+l0Gu2CiTyuIMd3PXlcQY7vevK4ghzf9eRxBTm+68njCrrAd8uU2KltYfr8qKlCc7T5eTYmu1Xt0C0u1H8m3j7tXyuF0Zfqv6SZ5OmTNExedcXNUesnY6lqBwslcRX9GdX+tdLEFx14YB++Ej4UheJLxYUeYtJT7dCBiSK+zLnk+ErxhYyAOfHN/cwPxcbl8I11fHyDLtIR3bxdik8/Ft+quCRGqh3UA0mUn5bFdo7Td/OzVi34Xt0/X90/f+5XjlJUK0yIL1qj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QqWJ45JOxFfBF4hbhC1TOEQtWzB5w4QM3S6Wgx5Vl/VqovBfw8ZGNOqiPSknxFXxGGQsvqbyReM2jVpDdnwv5Pg2awjB57iqqv7AV/fdeNcBNORbGHylJL5qZ9BMQgM+gLrF1XkMbvnFKsd3OfWMqxvB4dts32jluRzf9dQ5rg4En6sI+P5HKM/o+K6n/nGZEhwKj/EV5j0yvi5F/f7y1QLinWU6vq4KKRKscj5s30+/vp9+qTjk+hu0HzvFgdzxdbWoDUH1Gcj/ijhzScBrq+kAAAAASUVORK5CYII=" alt="" />

select manager.ename  from emp manager, emp worker where manager.empno = worker.mgr and worker.ename = 'KING';

4.内连接与外连接

select table1.column, table2.column from table1 [inner | left | right | full] join table2 on table1.column1 = table2.column2;

以上分别为内连接、左连接,有链接,完全连接,on子句用于指定连接条件。

内连接:用于返回满足查询条件的所有记录,默认情况下,在指定连接查询时如果没有指定任何连接操作,那其就为内连接,目前以上查询语句皆为内连接,举例,以下两句相同:

select dept.dname,emp.ename from dept,emp where dept.deptno = emp.deptno and dept.name = 'sales';

select dept.dname,emp.ename from dept inner join emp on dept.deptno = emp.deptno and dept.name = 'sales';

左外链接:返回满足连接条件的所有记录,同时返回不满足连接条件的连接操作符左边表的其他行,即左边返回所有记录,右表只返回满足查询条件的记录。为方便,引用他人的例子说明查询效果(参考:http://www.blogjava.net/hello-yun/archive/2011/04/08/347890.html),如下:

SELECT * FROM TESTA LEFT OUTER JOIN TESTB ON TESTA.A=TESTB.A;

查询效果如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZQAAABFCAIAAADisofJAAAFQUlEQVR4nO2d25WjMBAFnREREZCzcTDOh/1g7JGEHlc0O7bUVV9rYOeYOs0FhGjftoDbi2ThFnPcIFyS/Dv7348LAQC6aATTFiRaSLhK/AvXfWcAgEN4ZbYQ4klfCwBwCQQNAAwJ4QUAQ0J4AcCQEF4AMCSZSQ8AAN9GO7z+b1SODHJEECWCKB3CywRyRBAlgiida8Lrsd5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Kh/AygRwRRIkgSofwMoEcEUSJIEpHCi8AgC+kHV7X5uVMIEcEUSKI0iG8TCBHBFEiiNKxhtepBr7RmqGnnFTlhBNskhlKtIGGPIjSsYXXmQa+U/V8KcuJdizeZ9pAQxFE6ZjC60wD38NbVUO3Cy3KSfc/CCzaQEMZROlYwsvaD6C1ZgD0UotbR9AGGvIgSscSXtZOTIW/MhJyqVVy3XkbaIhAlM6nw2vw12rVUkv6DdIGGgogSuez4TV4dKmldkgn2kBDAUTpfDK8JhjSEUqt2fTSeRtoiECUzscG7Oe4IWqVWm4iBG2goQyidK6eKiE18H39dOb5r/0t1Eqt9BsttIGGMojSsU9S7W3gu08uH/6aa6coZ0+u8uQQ2kBDFkTp2MKrv4FvYVBn1COzJCf/Q5/xI8Vk5z23gYY3iNKxhpdzkCOCKBFE6RBeJpAjgigRROkQXiaQI4IoEUTpEF4mkCOCKBFE6UjhBQDwhTTCCwBgFAgvABgSwgsAhoTwAoAh+QcV9ZFahZrQAwAAAABJRU5ErkJggg==" alt="" />

多表左外连接:

SELECT * FROM TESTA  LEFT OUTER JOIN TESTB ON TESTA.A=TESTB.A LEFT OUTER JOIN TESTC ON TESTA.A=TESTC.A

右外连接:如左外连接,即右表返回所有记录,坐标只返回满足查询条件的记录,如:

SELECT * FROM TESTA RIGHT JOIN TESTB ON TESTA.A=TESTB.A;

查询效果如下: 

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAY0AAABACAIAAABpxLxYAAAFU0lEQVR4nO2dwZXqMBAEnREROSCyIRjymX8AFkuW5LZYvtvaqht4D1a9USOEPDtFnWmaNt/RrwIA9EGyAIA75BQAuENOAYA7z5yaAAAsSXLqsKi0BzkiiBJBlA45pYIcEUSJIEqHnFJBjgiiRBCl45pTt3mapulyvR99Iz+4yLlfL6uv7k6ebES98SumiLAS9TCUMt+OvqsFpjl1m6fLPF+cZLnIuV9zLffrxWkWuoj6wbCYIsJK1G3OK+g2W0WVZU49p+J6Rh6JmZzkvduMqBqW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FMqyBFBlAiidMgpFeSIIEoEUTrklApyRNqi+voOLw4Gnf9s9YutiqoPmr7Dh96MNcgRaYnq6ju8POk4xJNFT5oV1Rg0fYeZinWQI9IQ1dV3eLMJxVmRA33d3ZS+w1ABOSJ1UZ/2S4iIkTo076iorB8QfYcPvRlrkCNSF/Vp/6n626dErqhk0PQdZiq2QI7It3LqtUk8xLyL0J9sTwdN32GmYgvkiHx1PTXSimpHRWVZRN/hQ2/GGuSIfDmnxtmh2lVRyX9Qo+/woTdjDXJEvryP/tdzir7DTMUWyBHZdy5hs+9weaPq9BMvmqJag6bvMFOxAXJEto4F7e47nE7MMeZdxEZFNQZN32GmYh3kiLRF9fUdXl4aYNY92Kqo+qDpO3zozViDHBFEiSBKh5xSQY4IokQQpUNOqSBHBFEiiNLJcwoAwJB3TgEA2EJOAYA7/wDcMf0yP3ZHiwAAAABJRU5ErkJggg==" alt="" />

完全外连接:即返回满足查询条件的记录,也返回不满足查询条件的其他记录,举例如下:

SELECT * FROM TESTA FULL JOIN TESTB ON TESTA.A=TESTB.A;

查询效果如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAABUCAIAAAAj0/aNAAAGbklEQVR4nO2dUZKqMBBF3ZErckHshsW4n34fjhICwduKesk750+qpmo41dyJodNzCgCAg3D69S8AAKBCYAHAYZgC6wQAYMl6YH01Kg8FckQQJYIoHQIrDXJEECWCKB0CKw1yRBAlgigd/8AaL6fTZfz1b1FgI2e81N/urTz5iJqwK6aIsBJ1Hc5VTZ2H669/qRL7wLoOw+ViJc1ITlyH8+TG7Wl0EnXDr5giwk7UeJnK6DqcvSLLPbCuwzDOnsrf4yOnCqxA1DaGxRQRdqLKwKo//Rz3wBqH4RrX4WzkzEfOcoVlpMlLVERYFlNE2ImqVlhe+W4eWOPlMobZo2gjJ6otBx9FN5xERXgWU0TYiSr3Rr3SKtwD6zoMt9IajXYeXOREsIeVwbKYIsJOFHtYrzJbP9hoM5ETEYsVu9fiwUmUazFFhJ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Q+/Zbw9qF8XJf/cftgPOn5WLlXRiTDFojS+XgfVrU5M3tGj8nW9+XGvTIiGTZAlM6nO91nk2372Klpytm8V0YkQwtE6bwUWP83yBFBlAiidAisNMgRQZQIonQIrDTIEUGUCKJ0CKw0yBFBlAiidJqBBQBgyHpgfScvjwhyRBAlgigdAisNckQQJYIoHQIrDXJEECWCKB0CKw1yRJ6Iyg3aLs6J99RcGxG6qObs8br9eKJrVwSWBHJEtkSlD3iVI8R6m4H/RFRjxPjkYWakVNXHTPIZBFYa5IhsiMoeoZ9d7+6Y+LNkLwOoXEzNTsAV4yJ7PlJPYKVBjkhb1GtDih5XO1s0yBVVzgKZu5ln+1rA9QKBlQY5Im1Rr42B/Nuh6WvFECGOhJxvSNUTMidz5RH83tIqCKwXQI7I3oG1/sMdkFhhlbNqm4HFHhYUIEfkQ4HV38aMXlGP73haYHX4pZDASoMckQ/tYfX3FCYq6hFH0h5Wh2ssAisNckT2e0tYPXb/0wpr3sQxRbX4lrC1SD0sBFYa5Ijs14dVv9vv6xl8FliNfxgn9mF17IrAkkCOyKc63ft6AiPR6V59uaPTfe0qVCBHBFEiiNIhsNIgRwRRIojSIbDSIEcEUSKI0iGw0iBHBFEiiNJpBhYAgCErgQUAYA6BBQCHgcACgMNAYAHAYfgHyFEDWHeRUwMAAAAASUVORK5CYII=" alt="" />

(+)操作:(+)执行外连接,将其放在显示较少行(完全满足连接条件行)的一端,需要注意的是(+)操作只能用于列,而不能用在表达式上,不能与or和in操作同时使用。

用(+)实现左外连接:将上面左连接语句改为用(+)实现。

SELECT * FROM TESTA LEFT OUTER JOIN TESTB ON TESTA.A=TESTB.A;
==
SELECT * FROM TESTA ,TESTB WHERE TESTA.A=TESTB.A(+);
SELECT * FROM TESTA  LEFT OUTER JOIN TESTB ON TESTA.A=TESTB.A LEFT OUTER JOIN TESTC ON TESTA.A=TESTC.A
==
SELECT * FROM TESTA, TESTB,TESTC where TESTA.A=TESTB.A(+) and TESTA.A=TESTC.A(+);
SELECT * FROM TESTA  LEFT OUTER JOIN TESTB ON TESTA.A=TESTB.A LEFT OUTER JOIN TESTC ON TESTB.A=TESTC.A
==
SELECT * FROM TESTA, TESTB,TESTC where TESTA.A=TESTB.A(+) and TESTB.A=TESTC.A(+); select a.dname, b.ename from dept a left join emp b on a.deptno = b.deptno and a.deptno =10;
==
select a.dname, b.ename from dept a, emp b where a.deptno = b.deptno(+) and a.deptno(+) =10;

用(+)实现右外连接方式同左连接。

用(+)实现完全连接:

SELECT * FROM TESTA FULL JOIN TESTB ON TESTA.A=TESTB.A;
==
SELECT TESTA.*,TESTB.* FROM TESTA LEFT OUTER JOIN TESTB ON TESTA.A=TESTB.A
UNION
SELECT TESTA.*,TESTB.* FROM TESTB LEFT OUTER JOIN TESTA ON TESTA.A=TESTB.A

合并查询

可通过UNION,UNION ALL, INTERSECT, MINUS集合操作符合并多个SELECT语句的查询结果,语法:

Select 语句1 [UNION|UNION ALL| INTERSECT|MINUS| select 语句2

….

操作符具有相同优先级,多个操作符按从左往右执行。使用集合操作符时需保证不同查询的列个数和数据类型都要匹配,如果选择的列表包含了表达式,则必须为其制定列别名。

UNION

UNION用于获取两个结果集的并集,当使用该操作符时,会自动去掉结果集中的重复行,并会以第一列的结果进行排序。

select areaid as puid ,domainid from domain where areaid = '01'

union

select puid,domainid from puinfo  where domainid = '120000' and rownum < 5

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcgAAADqCAIAAACp0EI0AAATrklEQVR4nO2dvZKzOBZA9Rj7uH6UL5/aaOOpcehows062vqqJqT8At4ANy3r90pc4ArOKaoLywKuhDiW1SC7FwAAqOKODgAA4GwgVgAAZRArAIAyiBUAQBnECgCgDGIFAFAGsQIAKINYAQCUQawAIMV1CaNvq6FpKLG73eZlu2hUcKnT6JxLpu+AYjwqpZDsoTvm7RrJbmWv7GGES+C12VUQb+3ceylkQ6yCDbpalYW2eJRYc/TFc2wpykff+iwbOYMWGnM3a+owadV4PfCsjZO2N4j1MBCr7tF3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XW86qcSQAYJmLPiAgxI7O7EQCAFUQawk7OrMTCQBUYXarYTi6qQCAFBOzWx0+2ynzsQKAIiaGAg7XFmIFAEUuKtZqGRErAHRj4narQCKu/ecF5SkrxbppbIXPGMQKMBC7PiAgEWtsnGB9fUq3WHeILedWxAowECZmtyr0Ck2JdYfYECvACTAxu1VBXnGiHbFuERtiBTgBJuYKqMpLt5+4iNXlZ7eSi3WLPixiBRiaAcRaeHl4j1U9NsQKcAKsi1XyroUxVsQKAAvmbrdacgYvk4m5rQopa8S6dWw5qyJWgLG46AMCwoUHBACgA8SKWAFAGWa3GoajmwoASDExu5Xi3nSxozM7kQBAFRNDAYfWQAk7OrMTCQBUuahYq2Wcs9nRmZ1IAKDK3o+0xndcxWJ1xZ8XXJkiLOacLdbZprEVPmMQK8BAmJjd6iOgyDjB+voUSTHnbPfUr7RuGlvOrYgVYCB2HQqQiPWVF83hYt0hNsQKcAL2E2vSqlWxxol2xLpFbIgV4ASMIVbdfuJSTJef3WrOJhHrFn3YGMQKMBADiLXw8vAeq3psiBXgBFgXq+RdC2OsWrEhVoATYE6s7pvgZTIxt1UhRVjMOVvyroDtYstZNY4EACxz0QcEhNjRmZ1IAKAKYi1hR2d2IgGAKsxuNQxHNxUAkGJidqvDZztlPlYAUMTEUMDh2kKsAKDIRcVaLSNiBYBuzP2Y4O/Ub0rPqKSsFOumsRU+YxArwECY+/nr2DjB+vqUbrHuEFvOrYgVYCDMibUgmsPFukNsiBXgBAwwFBAk2hHrFrEhVoATYL3HukU/cRGry89uJRfrFn1YxAowNAOItfDy8B6remyIFeAEWBer5F0LY6yIFQAWzI2xLjmDl8nE3FaFlDVi3Tq2nFURK8BYXPQBAeHCAwIA0AFiRawAoAyzWw3D0U0FAKT0zG4FAAAFECsAgDKIFQBAGcQKAKDM/wEAOqKT0KaQjgAAAABJRU5ErkJggg==" alt="" />

UNION ALL

通UNION操作,不同之处该操作符不会取消重复值,且不会按某列排序。

select areaid as puid ,domainid from domain where areaid = '01'
union all
select puid,domainid from puinfo where domainid = '120000' and rownum < 5

INTERSECT

用于取两个结果集的交集,即只会显示同时存在于两个结果集中的数据,并以第一列进行排序。

MINUS

用于获取两个结果集的差集,也就是只会显示在第一个结果集中存在,而再第二个结果集中不存在的数据,并会以第一列进行排序。(相当于union结果集再去掉第二个结果集)

oracle中的连接查询与合并查询总结的更多相关文章

  1. 彻底理解Oracle中的集合操作与复合查询

    --Oracle中的复合查询 复合查询:包含集合运算(操作)的查询 常见的集合操作有: union: 两个查询的并集(无重复行.按第一个查询的第一列升序排序) union all:两个查询的并集(有重 ...

  2. Oracle中字符串连接的实现方法

    1.和其他数据库系统类似,Oracle字符串连接使用“||”进行字符串拼接,其使用方式和MSSQLServer中的加号“+”一样. 例如: SELECT '工号为'||FNumber||'的员工姓名为 ...

  3. oracle中关于clob类型字段的查询效率问题

    今天,公司项目某个模块的导出报如下错误: HTTP Status 500 – Internal Server Error Type Exception Report Message Handler d ...

  4. ORACLE中关于表的一些特殊查询语句

    1: 如何判断字段的值里面:那些数据包含小写字母或大小字母 判断字段NAME的值里面有小写字母的记录 方式1: SELECT NAME FROM TEST WHERE regexp_like(NAME ...

  5. 在oracle中操作表及字段注释,查询一个表的所有字段名以及属性和约束

    1.查询表注释 SELECT * FROM USER_TAB_COMMENTS; 三列:TABLE_NAME,TABLE_TYPE,COMMENTS 2.查询字段注释 SELECT * FROM US ...

  6. oracle中创建用户,指定要查询的视图 --九五小庞

    --01: 创建PACS用户,并且初始密码为PACScreate user PACS identified by "PACS"; --02: 赋予该用户登录数据库的权限.grant ...

  7. Mysql中类似于Oracle中connect by ... start with的查询语句(木大看懂)

    表结构 create table sys_branch ( id ) not null, parent_id ), branch_name ), delete_flag ), primary key ...

  8. 查询oracle中所有用户信息 禁用用户

    ----查询oracle中所有用户信息 ----1.查询数据库中的表空间名称 ----1)查询所有表空间 select tablespace_name from dba_tablespaces; se ...

  9. 对于Oracle中分页排序查询语句执行效率的比较分析

    转自:http://bbs.csdn.net/topics/370033478 对于Oracle中分页排序查询语句执行效率的比较分析 作者:lzgame 在工作中我们经常遇到需要在Oracle中进行分 ...

随机推荐

  1. CGAffineTransform方法汇总

    CGAffineTransform是二维的仿射变换,可以进行位移,旋转,缩放,CGAffineTransform实际上是一个矩阵. CGAffineTransform { CGFloat a, b, ...

  2. 准标识符(Quasi-dientifier, QI)

    Quasi-identifier From Wikipedia, the free encyclopedia Quasi-identifiers are pieces of information t ...

  3. 使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)

    使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)   原理: 1.使用FileReader 读取图片的base64编码 2.使用ajax,把图片的base64编码 ...

  4. Java类文件最大限制

    今天在往一个jsp文件里添加代码时,项目跑起来访问这个jsp时报错.. The code of method _jspService(HttpServletRequest, HttpServletRe ...

  5. python数据结构与算法——图的广度优先和深度优先的算法

    根据维基百科的伪代码实现: 广度优先BFS: 使用队列,集合 标记初始结点已被发现,放入队列 每次循环从队列弹出一个结点 将该节点的所有相连结点放入队列,并标记已被发现 通过队列,将迷宫路口所有的门打 ...

  6. C开发基础--函数调用栈

    发现有一些问题几乎是所有的新人都会遇到,而且也常因为缺乏一些基本的知识而无从下手.函数调用栈的内容就是其中之一.于是花点时间把以前写的内容整理出来. 程序在运行期间,内存中有一块区域,用来实现程序的函 ...

  7. MD5加密(16/32)

    public string MD5(string str, int code) { if (code == 16) //16位MD5加密(取32位加密的9~25字符) { return System. ...

  8. 【测试】trunc和round的区别

    trunc是截断:round是四舍五入:下面通过一个例子具体看一下trunc和round的不同 SQL),trunc() from dual; TRUNC() TRUNC() ------------ ...

  9. SQL Server 查看死锁的存储过程(转载)

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_who_lock]') and ) drop proc ...

  10. home_work--用户登陆

    前提: 根据文本中的已有用户和密码,实现登陆的交互:在用户输入用户名后,会有一个检测用户是否被锁定的环节:如果用户三次密码输入错误,则该用户则会被添加到锁定文件中,无法登陆. 涉及到的知识点: 1.p ...