Home Database Mysql Tutorial Oracle基础教程:多表查询、SQL99

Oracle基础教程:多表查询、SQL99

Jun 07, 2016 pm 05:11 PM

多表查询 等连接 通过两个表具有相同意义的列,建立连接条件. 查询结果只显示两个列中的值是等值条件的行数据 表中同名列被选择

多表查询
 等连接
  通过两个表具有相同意义的列,建立连接条件.
  查询结果只显示两个列中的值是等值条件的行数据
  表中同名列被选择时必须添加表名前缀进行修饰 否则无法确定这一列是属于哪个表
  N张表等值连接,条件至少有N-1个,否则会产生笛卡尔积
 不等连接
  A表中的某列数据和B表中一列或多列的关系是非等值关系,大于,小于,不等于,等条件都属于不等连接的范畴
 自连接
  数据都来自一张表,所以在from字句中需要对表添加别名,添加表别名后才能合法化的引用表中的列名.
  本质就是将一张表虚拟成了两张表
 外连接
  即是选择出满足等连接条件及其以外的行
  (+)修饰符号用法:放置在选出结果只包含等连接的列后,则另一列的结果就是等值行+非等值行
 全连接(Oracle 不提供连接关键字,,需要用集合模式实现)
 
 SQL1999(SQL99) 有全连接的关键词

当数据需要从多个表中获得时

idle> select * from emp,dept;
...
...
56 rows selected.
这样得到的结果是 56 行,其实两个表产生了笛卡尔集 14 * 4
idle> select count(*) from emp;

  COUNT(*)
----------
 14

idle> select count(*) from dept;

  COUNT(*)
----------
  4

idle>

在连接中给定一个等值条件 一般是主键和外键的关系
例如deptno在dept表中是主键 在emp表中是外键
观看dept(部门表)和emp(员工表)的关系
他们是主从关系,dept是主表,emp是从表
emp中的deptno一定要隶属于dept中的deptno
所以他们两个列是等值的
具体的主键和外键关系 我们在以后建表时再详细阐述

 


等连接

按deptno的等值关系联合两张表的所有列

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

    DEPTNO DNAME   LOC       EMPNO ENAME      JOB        MGR HIREDATE        SAL COMM  DEPTNO
---------- -------------- ------------- ---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
 10 ACCOUNTING   NEW YORK       7782 CLARK      MANAGER       7839 1981-06-09 00:00:00      2450       10
 10 ACCOUNTING   NEW YORK       7839 KING       PRESIDENT     1981-11-17 00:00:00      5000       10
 10 ACCOUNTING   NEW YORK       7934 MILLER     CLERK       7782 1982-01-23 00:00:00      1300       10
 20 RESEARCH   DALLAS       7566 JONES      MANAGER       7839 1981-04-02 00:00:00      2975       20
 20 RESEARCH   DALLAS       7902 FORD       ANALYST       7566 1981-12-03 00:00:00      3000       20
 20 RESEARCH   DALLAS       7876 ADAMS      CLERK       7788 1987-05-23 00:00:00      1100       20
 20 RESEARCH   DALLAS       7369 SMITH      CLERK       7902 1980-12-17 00:00:00       800       20
 20 RESEARCH   DALLAS       7788 SCOTT      ANALYST       7566 1987-04-19 00:00:00      3000       20
 30 SALES   CHICAGO       7521 WARD       SALESMAN       7698 1981-02-22 00:00:00      1250  500      30
 30 SALES   CHICAGO       7844 TURNER     SALESMAN       7698 1981-09-08 00:00:00      1500    0      30
 30 SALES   CHICAGO       7499 ALLEN      SALESMAN       7698 1981-02-20 00:00:00      1600  300      30
 30 SALES   CHICAGO       7900 JAMES      CLERK       7698 1981-12-03 00:00:00       950       30
 30 SALES   CHICAGO       7698 BLAKE      MANAGER       7839 1981-05-01 00:00:00      2850       30
 30 SALES   CHICAGO       7654 MARTIN     SALESMAN       7698 1981-09-28 00:00:00      1250 1400      30

14 rows selected.

idle>
这才是我们要的结果.笛卡尔积几乎我们不会需要.多表查询时基本都带有where子句来描述多个表的关系 避免笛卡尔集


当两个表中有相同的列名时,为了区分 要在列前加上表名作前缀.
不冲突时 直接使用
idle> select deptno,empno,ename,dname,sal from emp a,dept b where b.deptno=a.deptno;
select deptno,empno,ename,dname,sal from emp a,dept b where b.deptno=a.deptno
       *
ERROR at line 1:
ORA-00918: column ambiguously defined
因为deptno 没有表前缀 冲突

idle>
在联合的两个表内取数据:描述scott在哪个部门
idle> select empno,ename,dname,sal from emp,dept where emp.deptno=dept.deptno and ename='SCOTT';

     EMPNO ENAME      DNAME      SAL
---------- ---------- -------------- ----------
      7788 SCOTT      RESEARCH     3000

idle>


表的别名
 格式:表名 别名
给表取别名是很有必要的,因为有的表名很长 不便于引用时书写.

idle> select a.deptno,empno,ename,dname,sal from emp a,dept b where b.deptno=a.deptno and ename='SCOTT';

    DEPTNO EMPNO ENAME  DNAME         SAL
---------- ---------- ---------- -------------- ----------
 20  7788 SCOTT  RESEARCH       3000

idle>


不等连接
 就是排除完全相等条件以外的 >,=, between and
 主要在于不同表之间显示特定范围的信息(也可以理解成包含关系)
例如: SALGRADE表把工资划分了5个等级
idle> select * from SALGRADE;

     GRADE LOSAL    HISAL
---------- ---------- ----------
  1   700     1200
  2  1201     1400
  3  1401     2000
  4  2001     3000
  5  3001     9999

idle>

查处每个员工的工资等级
idle> select ename,sal,grade from emp a,salgrade b where sal between losal and hisal;

ENAME    SAL    GRADE
---------- ---------- ----------
SMITH    800        1
JAMES    950        1
ADAMS   1100        1
WARD   1250        2
MARTIN   1250        2
MILLER   1300        2
TURNER   1500        3
ALLEN   1600        3
CLARK   2450        4
BLAKE   2850        4
JONES   2975        4
SCOTT   3000        4
FORD   3000        4
KING   5000        5

14 rows selected.

idle>

练习:
 1.查询名字是S开头的员工名,员工号,部门号,部门名,部位地理位置
 select empno,ename,emp.deptno,dept.dname,dept.loc
 from emp,dept
 where ename like 'S%' and emp.deptno=dept.deptno;
 2.查询员工名,部门名,工资,工资等级
 select empno,ename,grade
 from emp,salgrade
 where sal between losal and hisal;
 3.查询工作在NEW YORK的所有员工
 select empno,ename,emp.deptno,dept.dname,dept.loc
  from emp,dept
   where emp.deptno=dept.deptno and dept.loc='NEW YORK';

 

自连接
 同一张表内的连接查询 即把一个表映射成两个表
 主要用于表的自参照关系 比如emp中的上下级或层次关系
 因为自连接是同一张表之间的链接查询 所以必须定义表别名

例子:根据empno和mgr的对应关系,可以查找出KING的下属都有谁

idle> select b.ename||'''s manager is '||a.ename from emp a,emp b where a.empno = b.mgr and a.ename='KING';

B.ENAME||'''SMANAGERIS'||A.ENAME
----------------------------------
BLAKE's manager is KING
JONES's manager is KING
CLARK's manager is KING

idle>

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

How does MySQL index cardinality affect query performance? How does MySQL index cardinality affect query performance? Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

Explain the InnoDB Buffer Pool and its importance for performance. Explain the InnoDB Buffer Pool and its importance for performance. Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

See all articles