为什么需要子查询?

现实中,很多情况需要进行以下条件的判断

  • 集合成员资格

    某一元素是否是某一个集合的成员

  • 集合之间的比较

    某一个集合是否包含另一个集合

  • 集合基数的测试

    测试集合是否为空

    测试集合是否存在重复元素

子查询定义

出现在 Where 子句中的 Select 语句被称为子查询(subquery),子查询返回了一个集合,可以通过与这个集合的比较来确定另一个查询集合。

基本语法

表达式 [not] in (子查询)

  • 语法中,表达式最简单的形式就是列名或者常数
  • 语义:判断某一表达式的值是否在子查询的结果中

例1

在一张表中查询tom、david同学的信息

+----------------+
| name |
+----------------+
| tom |
| david |
| lily |
| jony |
+----------------+

如果没有子查询,我们会这么写

mysql>select * from test where name='tom' or name ='david';
+----------------+
| name |
+----------------+
| tom |
| david |
+----------------+

使用子查询,我们可以这么写.此处直接使用了某一子查询的结果集合(该集合是已知的固定的)

mysql>select * from test where name in ("tom","david");
+----------------+
| name |
+----------------+
| tom |
| david |
+----------------+

例2

欲查询即学过课程001 又学过课程002的同学

+----------------+------------------+
| name | course |
+----------------+------------------+
| tom | 001 |
| tom | 002 |
| david | 001 |
+----------------+------------------+

如果没有子查询,可能会这么写

select t1.name
from test as t1, test as t2
where t1.name = t2.name
and t1.course='001'
and t2.course='002'
+----------------+
| name |
+----------------+
| tom |
+----------------+

实际上是表对自身做了笛卡儿积

+----------------+------------------+----------------+------------------+
| name | course | name | course |
+----------------+------------------+----------------+------------------+
| tom | 001 | tom | 001 |
| tom | 002 | tom | 001 |
| david | 001 | tom | 001 |
| tom | 001 | tom | 002 |
| tom | 002 | tom | 002 |
| david | 001 | tom | 002 |
| tom | 001 | david | 001 |
| tom | 002 | david | 001 |
| david | 001 | david | 001 |
+----------------+------------------+----------------+------------------+

然后再进行了挑选。

使用子查询改写

select name
from test
where course = '001'
and name in
(
select name
from test
where course = '002'
)

查询结果

+----------------+
| name |
+----------------+
| tom |
+----------------+

这里的逻辑是:从选修了002课程的学生中再选出选修了001课程的同学,思路更加清晰。

IN-子查询的更多相关文章

  1. 深入理解MySql子查询IN的执行和优化

    IN为什么慢? 在应用程序中使用子查询后,SQL语句的查询性能变得非常糟糕.例如: SELECT driver_id FROM driver where driver_id in (SELECT dr ...

  2. Mysql - 性能优化之子查询

    记得在做项目的时候, 听到过一句话, 尽量不要使用子查询, 那么这一篇就来看一下, 这句话是否是正确的. 那在这之前, 需要介绍一些概念性东西和mysql对语句的大致处理. 当Mysql Server ...

  3. 在SQL Server中为什么不建议使用Not In子查询

        在SQL Server中,子查询可以分为相关子查询和无关子查询,对于无关子查询来说,Not In子句比较常见,但Not In潜在会带来下面两种问题: 结果不准确 查询性能低下       下面 ...

  4. 读书笔记--SQL必知必会11--使用子查询

    11.1 子查询 查询(query),任何SQL语句都是查询.但此术语一般指SELECT语句. SQL还允许创建子查询(subquery),即嵌套在其他查询中的查询. 作为子查询的SELECT语句只能 ...

  5. 你真的会玩SQL吗?无处不在的子查询

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

  6. Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)

    Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...

  7. 当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。

    当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式.比如 select * from T_Employee where FNumber not in ( select top 5*  ...

  8. Oracle学习笔记五 SQL命令(三):Group by、排序、连接查询、子查询、分页

    GROUP BY和HAVING子句 GROUP BY子句 用于将信息划分为更小的组每一组行返回针对该组的单个结果 --统计每个部门的人数: Select count(*) from emp group ...

  9. [转]HQL中的子查询

    原文地址:http://blog.csdn.net/xb12369/article/details/8638683 子查询:   子查询是SQL语句中非常重要的功能特性,它可以在SQL语句中利用另外一 ...

  10. MySQL 子查询与连接操作笔记

    SQL语句之间是可以进行连接操作的,在一些复杂的数据操作中必须用到连接操作.简单的说就是一个SQL语句的结果可以作为相连接的SQL操作的一部分.SQL结构化查询语句,子查询是指的所有的SQL操作,并非 ...

随机推荐

  1. Analyzing 'enq: HW - contention' Wait Event (Doc ID 740075.1)

    Analyzing 'enq: HW - contention' Wait Event (Doc ID 740075.1) In this Document   Symptoms   Cause   ...

  2. Linux 修改本地时间 (centos为例)

    1.  tzselect [root@xxxx etc]# tzselect --- 选择时区命令 Please identify a location so that time zone rules ...

  3. 1 Introduction

    1. Introduction 1.1. License Flowable is distributed under the Apache V2 license. 1.2. Download http ...

  4. 新Chrome浏览器不支持html5的问题

    window.applicationCache事件,最新chrome浏览器已经不能判断是否支持html5: 之前,在IE和Google中 为ApplicationCache对象,而在FF中为 Offl ...

  5. AngularJS 1.x系列:AngularJS服务-Service、Factory、Provider、Value及Constant(5)

    1. AngularJS服务 AngularJS可注入类型包括:Service.Factory.Provider.Value及Constant. 2. Service AngularJS Servic ...

  6. P1226 【模板】快速幂||取余运算

    https://www.luogu.org/problemnew/show/P1226 模板题 直接上代码吧 #include<bits/stdc++.h> using namespace ...

  7. git 的简单实用

    一. 安装 Git(git_for_windows.xp510.com.rar) 二. 使用 a) 进入到 git bash(命令行工具) b) 初始化user.name,user.email $ g ...

  8. edusoho -A5: ApiBundle UML

    edusoho -A5:  ApiBundle UML

  9. Magento2.X 后端开发简要1

    Megento2.X 后端开发简要 根目录位置 组件的根目录是其文件夹和文件所在的组件的顶级目录.根据您安装的MaMeto开发环境,组件的根目录可以位于两个位置: 1.<Magento inst ...

  10. tmux 使用说明

    安装Mac:brew install tmux若未安装libevent,需要先brew install libeventCentos:yum -y install tmuxUbuntu:apt-get ...