search
HomeDatabaseMysql TutorialOracle学习笔记:表的联合查询

1、交叉联合:使你对于“联合”的概念开始产生最直观的印象,因为交叉联合的结果就是两个表的笛卡尔积code example: select * f

1、交叉联合:使你对于“联合”的概念开始产生最直观的印象,因为交叉联合的结果就是两个表的笛卡尔积
code example:

select * from T1, T2;

假如表1有2条记录,表2有3条记录,那么查询结果就是2*3=6条记录。

2、等值联合与不等值联合:

等值联合:只显示表1中的数据,以及表2中的、存在于表1中的数据。顾名思义,查询条件/表达式中以等号(“=”)连接。
code example:

select T1.sectionA, T1.sectionB, T2.sectionC from T1, T2

where T1.sectionA = T2.sectionA (and ...);

不等值联合:与等值联合查询类似,只不过在where子句中使用除等号以外的比较符连接,此处不以例详述。

3、内部联合与外部联合

内部联合:产生的结果行数取决于参加联合的行数,也就是说内部联合的行数取决于 WHERE 子句的结果。
code example:

SELECT P.PARTNUM, P.DESCRIPTION, P.PRICE, O.NAME, O.PARTNUM

FROM PART P  JOINORDERS OON ORDERS.PARTNUM = 54;

在这里你使用的语法中的 JOIN ON 不是 ANSI 标准中所指定的,而是我们所使用的解释器的附加语法,你可以用它来指明是内部联合还是外部联合,大多数解释器对些都进行了类似的扩充,注意这种类型的联合没有 WHERE 子句。

内部联合的定义不便介绍,个人理解上面的等值/不等值联合查询例子均为其代表,平时用到的查询也多为内部查询。

在学习过外观查询后有此感受,内部查询结果的直观与简洁与外部查询的“画蛇添足”形成鲜明对比,那么即使我们用到的不是外部联合,那就一定是内部联合了。

此外,对于概念和用法也不必做过多纠缠。这是因为——

大多数的 SQL 产品会判断应该在你的查询中使用哪一种联合,事实上,如果你在过程中使用它(或在程序内使用它),你无需指明联合类型,解释器会为你选择合适的语法形式。

如果你显示地指明了联合类型,那么解释器会用你指明的类型来代替(由解释器自身实现)优化的类型。

外部联合:产生的结果行数取决于参加联合的行数,也就是说内部联合的行数取决于 WHERE 子句的结果,而外部联合则是表间的联合。
code example:

SELECT P.PARTNUM, P.DESCRIPTION, P.PRICE, O.NAME, O.PARTNUM 

FROM PART P RIGHT OUTER JOINORDERS OON ORDERS.PARTNUM = 54;

上述代码示例中使用了 RIGHT OUTER JOIN,它会令 SQL 返回右边表集内的全部记录,对于 ORDERS.PARTNUM54的也会显示这些记录,只不地在相应位置补以空值

左联合也是一样,只不过,由于表1与表2中的记录条数不同,其查询结果也大相径庭。

注意到:在一些解释器中使用+号来代替外部联合。+号的意思就是——显示我的全部内容,包括不匹配的内容。 
code example:

select e.name, e.employee_id, ep.salary, ep.marital_status,

from employee_tbl e, employee_pay_tbl ep

where e.employee_id = ep.employee_id(+) and e.name like '%MITH'

这条语句将会联合两个表,标有+号的 employee_id 将会全部显示,包括不满足条件的记录。

4、表的自我联合:由于联合查询也常被译为“连接”,,因此在有些资料中看到的“自连接”查询指的也是这个概念。它并无特别之处,是指表1与表2均为同一表名。其用处在于检查表中数据的一致性。
比如T1表中两条记录的sectionA字段是同值的,这可能是由于数据录入错误造成的,如果按正常数据使用它,可能造成不可预料的灾难。
code example:

SELECT F.PARTNUM, F.DESCRIPTION, S.PARTNUM, S.DESCRIPTION

FROM PART F, PART S

WHERE F.PARTNUM = S.PARTNUM

AND F.DESCRIPTION S.DESCRIPTION


如果不存在上述异常数据,那么查询结果应该为空;否则1条异常记录对应两条查询结果。可以此来检查数据的一致性。

5、联想到 UNION 与 UNION ALL

UNION与UNION ALL都用来连接两个查询(即两个select子句),但前者返回两个查询的结果并去除其重复的部分,后者一样对查询结果进行合并,但是对于重复记录并不去除。

UNION可以集合运算中的并集运算联系起来,与其对应的是INTERSECT,即交集运算,它返回的是两个查询中共有的部分。

6、补充说明:
上述联合查询仅仅列举重点,在联合查询的分类问题上并未作任何具体而微的阐述,甚至由于联合查询的应用在实际工作中比较少见,对此类概念的理解可以不作深究,但是一知半解是危险的,你为无知付出的代价是昂贵的。本文写作的意义也在于此。

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the different storage engines available in MySQL?What are the different storage engines available in MySQL?Apr 26, 2025 am 12:27 AM

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

What are some common security vulnerabilities in MySQL?What are some common security vulnerabilities in MySQL?Apr 26, 2025 am 12:27 AM

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

How can you identify slow queries in MySQL?How can you identify slow queries in MySQL?Apr 26, 2025 am 12:15 AM

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

How can you monitor MySQL server health and performance?How can you monitor MySQL server health and performance?Apr 26, 2025 am 12:15 AM

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

Compare and contrast MySQL and MariaDB.Compare and contrast MySQL and MariaDB.Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

How does MySQL's licensing compare to other database systems?How does MySQL's licensing compare to other database systems?Apr 25, 2025 am 12:26 AM

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

When would you choose InnoDB over MyISAM, and vice versa?When would you choose InnoDB over MyISAM, and vice versa?Apr 25, 2025 am 12:22 AM

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

Explain the purpose of foreign keys in MySQL.Explain the purpose of foreign keys in MySQL.Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.