用SQL进行嵌套查询

王孝银5090

王孝银5090

2016-02-19 17:37

人生本是一个不断学习的过程,在这个过程中,图老师就是你们的好帮手,下面分享的用SQL进行嵌套查询懂设计的网友们快点来了解吧!

  在select查询语句里可以嵌入select查询语句,称为嵌套查询。有些书上将内嵌的select语句称为子查询,子查询形成的结果又成为父查询的条件。

  子查询可以嵌套多层,子查询操作的数据表可以是父查询不操作的数据表。子查询中不能有order by分组语句。

  4.4.1 简单嵌套查询

  在执行下列语句。

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal=(select sal from scott.emp where ename='WARD');   ―――――――――――――――――――――――――――――――――――――

  单击按钮,出现如图4.19所示的结果。

  :第4章4.4441.sql。

  在这段代码中,子查询select sal from scott.emp where ename='WARD'的含义是从emp数据表中查询姓名为WARD的员工的薪水,父查询的含义是要找出emp数据表中薪水大于等于WARD的薪水的员工。上面的查询过程等价于两步的执行过程。

  (1)执行select sal from scott.emp where ename='WARD',得出sal=1250;

  (2)执行select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal=1250;

  4.4.2 带的嵌套查询

  在执行下列语句。

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal in (select sal from scott.emp where   ename='WARD');

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  单击按钮,出现如图4.20所示的结果。

  :第4章4.4442.sql。

  上述语句完成的是查询薪水和WARD相等的员工,也可以使用来进行查询。 4.4.3 带的嵌套查询

  在执行下列语句。

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal any(select sal from scott.emp where   job='MANAGER');

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  单击按钮,出现如图4.21所示的结果。

  :第4章4.4443.sql。

  带any的查询过程等价于两步的执行过程。

  (1)执行select sal from scott.emp where job='MANAGER',其结果如图4.22所示。

  :第4章4.4443-1.sql。

  (2)查询到3个薪水值2975、2850和2450,父查询执行下列语句。

  :第4章4.4443-2.sql。

  ――――――――――――――――――――――――――――――――――――――

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal 2975 or sal2850 or sal2450;   ――――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  4.4.4 带的嵌套查询

  在执行下列语句。

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =some(select sal from scott.emp where   job='MANAGER');

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  单击按钮,出现如图4.23所示的结果。

  :第4章4.4444.sql。

  带some的嵌套查询与any的步骤相同。

  (1)子查询,执行select sal from scott.emp where job='MANAGER',其结果如图4.22所示。

  (2)父查询执行下列语句。

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =2975 or sal=2850 or sal=2450;   ―――――――――――――――――――――――――――――――――――――

  :第4章4.4444-2.sql。

  带的嵌套查询和的嵌套查询功能是一样的。早期的SQL仅仅允许使用,后来的版本为了和英语的相区分,引入了,同时还保留了关键词。

  4.4.5 带的嵌套查询

  在执行下列语句。

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal all(select sal from scott.emp where   job='MANAGER');

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  单击按钮,出现如图4.24所示的结果。

  :第4章4.4445.sql。

  带all的嵌套查询与的步骤相同。

  (1)子查询,结果如图4.22所示。

  (2)父查询执行下列语句。

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal 2975 and sal2850 and sal2450;

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  :第4章4.4445-2.sql。

  4.4.6 带的嵌套查询

  在执行下列语句。

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp,scott.dept where exists (select * from scott.emp where   scott.emp.deptno=scott.dept.deptno);

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  单击按钮,出现如图4.25所示的结果。

  :第4章4.4446.sql。

  4.4.7 并操作的嵌套查询

  并操作就是集合中并集的概念。属于集合A或集合B的元素总和就是并集。 在执行下列语句。

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  (select deptno from scott.emp) union (select deptno from scott.dept);   ―――――――――――――――――――――――――――――――――――――

  单击按钮,出现如图4.26所示的结果。

  :第4章4.4447.sql。

  4.4.8 交操作的嵌套查询

  交操作就是集合中交集的概念。属于集合A且属于集合B的元素总和就是交集。 在执行下列语句。

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  (select deptno from scott.emp) intersect (select deptno from scott.dept);   ―――――――――――――――――――――――――――――――――――――

  单击按钮,出现如图4.27所示的结果。

  :第4章4.4448.sql。

  4.4.9 差操作的嵌套查询

  差操作就是集合中差集的概念。属于集合A且不属于集合B的元素总和就是差集。

  在执行下列语句。

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  (select deptno from scott.dept) minus (select deptno from scott.emp);

  ―――――――――――――――――――――――――――――――――――――

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  单击按钮,出现如图4.28所示的结果。

  :第4章4.4449.sql。

  并、交和差操作的嵌套查询要求属性具有相同的定义,包括类型和取值范围。

展开更多 50%)
分享

猜你喜欢

用SQL进行嵌套查询

编程语言 网络编程
用SQL进行嵌套查询

用SQL进行函数查询

编程语言 网络编程
用SQL进行函数查询

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

用SQL进行多表查询

编程语言 网络编程
用SQL进行多表查询

用SQL进行单表查询

编程语言 网络编程
用SQL进行单表查询

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

使用SQL Server 2005 FOR XML嵌套查询

编程语言 网络编程
使用SQL Server 2005 FOR XML嵌套查询

SQL Server数据库的嵌套子查询

编程语言 网络编程
SQL Server数据库的嵌套子查询

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

缩短网址服务在网络营销中的应用

缩短网址服务在网络营销中的应用

做论坛的几个成功秘诀

做论坛的几个成功秘诀
下拉加载更多内容 ↓