子查询:

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

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

前期准备表:

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. 乱花渐入迷人眼------从解决jqueryEasyUI上传插件提交ajax请求谈网页调试

    由于要给格斗男神写搏击俱乐部ERP系统,就要用到jquery Easyui插件规范数据和表单的录入,其中一项功能就是上传商品图片, 而且是在datagrid-detailview中使用filebox完 ...

  2. Acwing-101-最高的牛(差分)

    链接: https://www.acwing.com/problem/content/103/ 题意: 有 N 头牛站成一行,被编队为1.2.3-N,每头牛的身高都为整数. 当且仅当两头牛中间的牛身高 ...

  3. SpringBoot项目构建、测试、热部署、配置原理、执行流程

    SpringBoot项目构建.测试.热部署.配置原理.执行流程 一.项目构建 二.测试和热部署 三.配置原理 四.执行流程

  4. 如何在matalb图像上添加公式符号

    方法: legend({'$\sigma(t)$'},'interpreter','latex') 效果如下:

  5. 利用 IntelliJ IDEA 进行代码对比的方法

    Sometimes,我们会有这样的需求,即:想对比出两个不同版本代码的区别.如何实现? 第 1 种:如果我们是从 SVN 检出的项目,并且想比较本地代码与从 SVN 检出时的代码相比都有那些区别,可以 ...

  6. 利用python的图像分块与拼接

    import os import matplotlib.pyplot as plt import cv2 import numpy as np def divide_img(img_path, img ...

  7. 使用C#语言,将DataTable 转换成域模型

    DataTable dt = SqlHelper.Query(strQuery); ) * size).Take(pagesize); List<Model> listData = new ...

  8. erlang创建100万个进程,每一个进程花费多少时间呢?

    最近工作需要,需要先测试一下erlang启动进程的时间开销: 看了一片博客,感觉挺好的,学习erlang推荐http://www.blogjava.net/yongboy/ 于是参照他的文章里面的一个 ...

  9. leetcode-hard-array-54. Spiral Matrix-NO

    mycode 思路:这种方格图一定要预先设置定位的变量,例如最大的长.宽,变化中的长.宽,在while循环中也要不断判断是否满足break条件 class Solution(object): def ...

  10. react native props上存在的属性,显示不存在

    问题:类型“Readonly<{}> & Readonly<{ children?: ReactNode; }>”上不存在属性“navigation”.ts(2339) ...