子查询:

子查询是将一个查询语句嵌套在另外一个查询语句中,内层查询语句的查询结果,可以作为外来层查询语句提供查询条件。

因此在特定条件下,一个查询语句的条件,需要另外一个查询语句来获取。

前期准备表:

create table employee ( num int(50),
d_id int(50),
name varchar(50),
age int(50),
sex varchar(50),
homeadd varchar(50)
); insert into employee values(1,1001,'zhangsan',26,'nan','beijing');
insert into employee values(2,1001,'lisi',24,'nv','hunan');
insert into employee values(3,1002,'wangwu',25,'nan','jiangsu');
insert into employee values(4,1004,'aric',15,'nan','yingguo'); select * from employee; create table department ( d_id int(50),
d_name varchar(50),
functione varchar(50),
address varchar(50)
); insert into department values(1001,'keyanbu','yanfachanpin','3lou5hao');
insert into department values(1002,'shengchanbu','shengchanchanp','5louyiceng');
insert into department values(1003,'xiaoshoubu','cehuaxiaoshou','1louxiaoshoudating'); select * from department;

一、带in关键字的子查询

select * from employee where d_id in (select d_id from department );

select * from employee where d_id not in (select d_id from department );

二、带exists关键字的子查询

exists关键字表示存在,使用exists关键字时,内层查询语句不用返回查询的记录。而是返回一个真假值。

如果内层查询语句查询到满足条件的记录,就返回一个真值(true);否则,返回一个假值(false);

当返回值为true时,外层查询语句将进行查询;而返回false时,外层查询语句不进行查询或者查询不出任何记录。

select * from employee where exists (select d_name from department where d_id = 1003);

select * from employee where exists (select d_name from department where d_id = 1004);

其它:

exists关键字可以与其他查询条件一起使用。条件表达式与exists关键字之间用and或者or来连接。

select * from employee where age > 24 and exists (select d_name from department where d_id = 1003);

select * from employee where age > 24 and exists (select d_name from department where d_id = 1004);

not exists与exists相反。

select * from employee where age > 24 and not exists (select d_name from department where d_id = 1003);

select * from employee where age > 24 and not exists (select d_name from department where d_id = 1004);

===================================================================

准备语句:

create table schoarship ( levela int(50),
score int(50)
);

insert into schoarship(levela,score) values(1,90);
insert into schoarship values(2,80);
insert into schoarship values(3,70); select * from schoarship; create table computer_stu ( id int(50),
name varchar(50),
score int(50)
); insert into computer_stu(id,name,score) values (1001,'lily',85); insert into computer_stu(id,name,score) values (1002,'tom',91),
(1003,'jim',87),
(1004,'aric',77),
(1005,'lucy',65),
(1006,'andy',99),
(1007,'ada',85),
(1008,'jeck',70);

select * from computer_stu;

select * from schoarship;

select * from computer_stu;

3、带比较运算符的子查询

select id,name,score from computer_stu where score >= (select score from schoarship where levela = 1);

/* 查询获得一等奖学金的学生有哪些,第一个表为奖学金等级和最低分数*/

select d_id,d_name from department where d_id != (select d_id from employee where age = 24);

/*只有生产部和销售部没有年龄等于24岁的员工*/

4、带any关键字的子查询

any关键字表示满足其中任何一个条件。使用any关键字时,只要满足内查询语句返回的结果中的任何一个,就可以通过该条件来执行外层查询语句

select * from computer_stu where score >= any ( select score from schoarship );

/*结果显示,有7个人获得奖学金,只有id为1005的人没有,因为分数为65,不高于奖学金指定最低分数的任何一个*/

5、带all关键字的子查询

all关键字表示满足所有条件。使用all关键字时,只有满足内层查询语句返回的所有结果,才可以执行外层查询语句。

select * from computer_stu where score >= all ( select score from schoarship );

/*结果显示,只有两个人获得奖学金。因为这两个人的分数比所有奖学金要求的分数都高*/

mysql——多表——子查询——示例的更多相关文章

  1. MySQL 表子查询

    MySQL 表子查询 表子查询是指子查询返回的结果集是 N 行 N 列的一个表数据. MySQL 表子查询实例 下面是用于例子的两张原始数据表: article 表: aid title conten ...

  2. MySQL里面的子查询

    一.子查询定义 定义: 子查询允许把一个查询嵌套在另一个查询当中. 子查询,又叫内部查询,相对于内部查询,包含内部查询的就称为外部查询. 子查询可以包含普通select可以包括的任何子句,比如:dis ...

  3. 详细讲述MySQL中的子查询操作 (来自脚本之家)

    继续做以下的前期准备工作: 新建一个测试数据库TestDB: ? 1 create database TestDB; 创建测试表table1和table2: ? 1 2 3 4 5 6 7 8 9 1 ...

  4. MySQL中IN子查询会导致无法使用索引

    今天看到一个博客园的一篇关于MySQL的IN子查询优化的案例,一开始感觉有点半信半疑(如果是换做在SQL Server中,这种情况是绝对不可能的,后面会做一个简单的测试.)随后动手按照他说的做了一个表 ...

  5. MySQL多表数据查询(DQL)

    数据准备: /* ------------------------------------创建班级表------------------------------------ */ CREATE TAB ...

  6. MySQL中in子查询会导致无法使用索引问题(转)

    MySQL的测试环境 测试表如下 create table test_table2 ( id int auto_increment primary key, pay_id int, pay_time ...

  7. sql字段拆分 ,连表子查询获取值

    1.连表子查询获取值 select bas.name,bas.id_card_num,bas.mobil_no,gender,bas.birthday,bas.height,bas.weight,pr ...

  8. 【MySQL】02_子查询与多表查询

    子查询 指一个查询语句嵌套在另一个查询语句内部的查询,这个特性从MySQL 4.1开始引入. SQL 中子查询的使用大大增强了 SELECT 查询的能力,因为很多时候查询需要从结果集中获取数据,或者 ...

  9. mysql 子句、子查询、连接查询

    一.mysql查询的五种子句 where子句(条件查询):按照“条件表达式”指定的条件进行查询. group by子句(分组):按照“属性名”指定的字段进行分组.group by子句通常和count( ...

随机推荐

  1. 读取web.xml中设置的参数

    以获取Filer元素里设置的参数为例 先在web.xml文件中配置如下 <?xml version="1.0" encoding="UTF-8"?> ...

  2. vue data数据恢复初始化

    Object.assign(this.$data, this.$options.data())

  3. C++--- Lambda匿名函数表达式

    视频教程:https://www.bilibili.com/video/av66419552/ 格式: [capture](parameters)mutable->returm-type{};  ...

  4. Rust学习-阶段1学习总结

    学习Rust已经两周了,基本上是断断续续的在学,或者是在上下班坐公交时,或者是在ODC没事做时.现在已经学习了Rust程序设计语言的前5章,是时候做一个总结了.关于数据类型或者if else这种内容我 ...

  5. Qt:The CDB Process Terminated!调试失败

       一般是找不到DLL库导致的CDB终止.

  6. BZOJ 3732: Network Kruskal 重构树

    模板题,练练手~ Code: #include <cstdio> #include <algorithm> #define N 80000 #define setIO(s) f ...

  7. 16位masm汇编实现记忆化递归搜索斐波那契数列第50项

    .model small ;递归fib,使用压缩BCD码,小端派 .data y1 byte 6 dup(0) y2 byte 6 dup(0) vis byte 1,1,1,61 dup(0) ;便 ...

  8. 【IOI2018】组合动作

    还是自己水平不够,想了两天没想出来--(然后我就被其他人吊打了) 这种题目看了题解就秒会,自己想就想不出来-- 下面是我的心路历程(我就在想出来又叉掉的不断循环中度过--) 开始把题目看成了查询限制 ...

  9. Selenium 环境安装

    前言: 本人在学习Selenium时,用的版本是Python3.6+Selenium3,后续写的所有学习资料都是基于这套环境.在安装Selenium3前,请确保本机已安装好了Python3,如未安装可 ...

  10. 前端MVC、MVVM的简单实现

    MVC MVC是一种设计模式,它将应用划分为3个部分:数据(模型).展示层(视图)和用户交互层.结合一下下图,更能理解三者之间的关系.换句话说,一个事件的发生是这样的过程 用户和应用交互 控制器的事件 ...