Home Database Mysql Tutorial Oracle数据库之SQL连接查询

Oracle数据库之SQL连接查询

Jun 07, 2016 pm 05:28 PM
sql query

现实中有这么一种需求,有时候我们需要查询的数据是在多个表中的,那么如何从多个表中查询数据呢?这就需要通过表的连接来实现查询

前言

现实中有这么一种需求,有时候我们需要查询的数据是在多个表中的,那么如何从多个表中查询数据呢?这就需要通过表的连接来实现查询了。

笛卡尔积

在定义连接之前我们需要简单的了解一下笛卡尔积,笔者不会纠结与其定义,只是看看其实际效果

其实笛卡尔积就是没有连接条件或者连接条件无效的连接,例如:

select *from emp ,dept;--其结果倾向于一个巨大的记录数,无实际意义

上面可以看出笛卡尔积得出的是一个巨大的无意义的记录集合,我们可以通过在where子句中使用有效的连接来避免这种情况,使之具有实际意义。

连接定义

就是在笛卡尔积的基础上使用连接条件,例如根据两个表的相同列进行连接。若果要进行n个表的连接那么就的添加n-1个连接条件。例如:

select *from emp ,dept where emp.deptno = dept.deptno;

连接类型

连接主要有两种类型等值连接、非等值连接.

还包括一些其他的连接方式:多连接、自连接、 定置运算符

等值连接

上面已经使用过了,即是表连接的where条件是一个表的列等于另一个表的列,通常情况下是主键和外键的判等连接。

select *from emp ,dept where emp.deptno = dept.deptno;

Tips:因为deptno在两个表中都存在,所以要使用表.字段的形式,否则Oracle认为其歧义

等值连接中同样还可以添加约束条件

select *from emp ,dept where emp.deptno = dept.deptno and emp.ename ='CLARK';

--结果只取一条记录

同样可以定义标的别名,不过需要注意的是定义了别名之后,,必须通过别名访问字段,不能通过表明来访问了

select *from emp e ,dept d where e.deptno = d.deptno and e.ename ='CLARK';

非等值连接

通过观察scott用户下的emp表和salgrade表,可以知道的是他们之间没有直接的对应项,而是

emp的sal列的值在salgrade表中的LOSAL和HISAL之间,他们不是等值连接。

select * from emp e,salgrade s where e.sal between s.losal and s.hisal;

linux

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Meituan interview question: Have you ever encountered slow SQL? How was it solved? Meituan interview question: Have you ever encountered slow SQL? How was it solved? Aug 24, 2023 pm 03:41 PM

MySQL's slow query log is a log record provided by MySQL. It is used to record statements in MySQL whose query time exceeds (greater than) the set threshold (long_query_time) and records them in the slow query log.

PHP and PDO: How to execute complex SQL queries PHP and PDO: How to execute complex SQL queries Jul 28, 2023 pm 03:43 PM

PHP and PDO: How to execute complex SQL query statements When processing database operations, PHP provides a powerful extension library PDO (PHPDataObjects) to simplify interaction with the database. PDO supports a variety of databases, such as MySQL, SQLite, etc., and also provides a wealth of functions and methods to facilitate developers to perform various database operations. This article will introduce how to use PDO to execute complex SQL query statements, and attach corresponding code examples. Connect to the database

What are the common table operations in PHP programming? What are the common table operations in PHP programming? Jun 12, 2023 am 09:46 AM

In web development, tables are the most basic and commonly used elements, and PHP is a popular server-side programming language. There are many common techniques and methods in table operations. This article will introduce common table operations in PHP programming. Displaying data tables In PHP, you can use the table tag in HTML to display data tables. It is worth noting that the table must be generated in a PHP script. Here is an example of a basic HTML table tag: <table><tr>

Why does my Go program error when executing SQL queries? Why does my Go program error when executing SQL queries? Jun 09, 2023 pm 06:10 PM

In Go programming, using SQL queries is a common task. However, sometimes errors occur when executing SQL queries, causing the program to fail to execute correctly. In order to solve these errors, we need to have a deep understanding of how SQL queries and the Go language interact. Below are some possible errors and corresponding solutions. Lack of database driver In Go language, you need to use a specific database driver to connect and operate the database. If you try to perform a database query and the database driver is not properly installed and configured

How to deal with SQL query errors in PHP language development? How to deal with SQL query errors in PHP language development? Jun 09, 2023 pm 06:13 PM

PHP language is a commonly used server-side scripting language used in fields such as website development. In PHP language development, SQL query is a common operation, but due to various reasons, SQL query may cause errors. Therefore, developers need to handle these errors in their code to ensure the stability and correctness of the application. Generally speaking, there are several common ways to handle SQL query errors in PHP language: 1. Use try/catch block to handle exceptions. In PHP language, you can use try/catch

Most SQL query efficiency optimization practices in PHP programming Most SQL query efficiency optimization practices in PHP programming Jun 23, 2023 am 10:37 AM

With the development of network technology, PHP programming has become the mainstream of website development for many companies. In PHP programming, SQL query efficiency is an issue that requires every programmer to pay attention and deal with. Inefficient SQL query will lead to slow website response, high system load or other unfriendly effects. Therefore, this article will focus on introducing various SQL query efficiency optimization practices in PHP programming to improve the execution efficiency of the program and the response speed of the entire system. Database index Database index is a basic method to improve the speed of database query

How to optimize SQL query statements and index usage in PHP development? How to optimize SQL query statements and index usage in PHP development? Nov 02, 2023 pm 12:12 PM

How to optimize SQL query statements and index usage in PHP development? In PHP development, database query is a very common operation. However, when the amount of data increases, query performance may be affected, causing the application to slow down. In order to improve query performance, we need to optimize the use of SQL query statements and indexes. This article will introduce some optimization tips and best practices to help you improve SQL query performance in PHP development. 1. Use the correct index: Index is an important part of the database to improve query performance. in design data

PHP query statement usage example PHP query statement usage example Mar 23, 2024 am 11:27 AM

PHP is a powerful server-side scripting language that is widely used in web development. In web development, we often need to interact with the database and execute query statements to obtain data. This article will introduce you to how to write query statements and usage examples in PHP. 1. Connect to the database Before using PHP to query the database, you first need to establish a connection with the database. Generally, we will use the MySQL database as an example. The code to connect to the database is as follows: $servername=

See all articles