子查询:

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

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

前期准备表:

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. Qt:The CDB Process Terminated!调试失败

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

  2. docker部署war+tomcat8

    注意:本文只是将jenkins.war作为一个war包来操作,实际上要使用jenkins还要同时安装git.maven等. 1.购买阿里云服务器(Ubuntu 18.04), 设置密码,root+pa ...

  3. Codeforces 1213D Equalizing by Division

    cf题面 中文题意 给n个数,每次可以把其中一个数字位运算右移一位(即整除以二),问要至少操作几次才能让这n个数中有至少k个相等. 解题思路 这题还有个数据范围更小的简单版本,n和k是50,\(a_i ...

  4. [CSP-S模拟测试]:中间值(二分)

    题目背景 $Maxtir$喜欢序列的中间值. 题目传送门(内部题127) 输入格式 第一行输入两个正整数$n,m$,其中$m$是操作和询问次数. 接下来两行每行输入$n$个非负整数,每一行分别表示两个 ...

  5. Mysql 里CHAR和VARCHAR的最大长度及一些注意事项

    先写出结论: Mysql 5中 非空CHAR的最大总长度是255[字节]:非空VARCHAR的最大总长度是65533[字节]. 可空CHAR的最大总长度是254[字节]:可空VARCHAR的最大总长度 ...

  6. 小程序踩坑之获取不到e.target.dataset的值

    在页面与js传值中我们经常用到data-id="1"的方式,然后通过e.target.dataset.id取id的值今天在获取值时怎么也取不到,后来发现e对象有currentTar ...

  7. C++入门经典-例3.10-根据输入的字符输出字符串

    1:代码如下: // 3.10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #inc ...

  8. spring的AOP——采用注解完成AOP

    AOP的两种配置方式:XML配置和Aspectj注解方式. 一.项目的目录: 二.文件配置 我们采用的是JDK代理,所以首先将接口和实现类代码附上: public interface UserMana ...

  9. Java JDBC 基础

    JDBC API 包含以下几个核心部分: 1:JDBC 驱动 2:Connections (连接) 3:Statements (声明) 4:Result Sets (结果集) JDBC: 打开数据库连 ...

  10. nginx-location正则表达式匹配规则及动静分离

    nginx-location正则表达式匹配规则及动静分离  发表于 2018年03月5日 |  分类于 nginx|  0 nginx,location常用正则表达式,及nginx动静分离 nginx ...