mysql——多表——子查询——示例
子查询: 子查询是将一个查询语句嵌套在另外一个查询语句中,内层查询语句的查询结果,可以作为外来层查询语句提供查询条件。 因此在特定条件下,一个查询语句的条件,需要另外一个查询语句来获取。
前期准备表:
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——多表——子查询——示例的更多相关文章
- MySQL 表子查询
MySQL 表子查询 表子查询是指子查询返回的结果集是 N 行 N 列的一个表数据. MySQL 表子查询实例 下面是用于例子的两张原始数据表: article 表: aid title conten ...
- MySQL里面的子查询
一.子查询定义 定义: 子查询允许把一个查询嵌套在另一个查询当中. 子查询,又叫内部查询,相对于内部查询,包含内部查询的就称为外部查询. 子查询可以包含普通select可以包括的任何子句,比如:dis ...
- 详细讲述MySQL中的子查询操作 (来自脚本之家)
继续做以下的前期准备工作: 新建一个测试数据库TestDB: ? 1 create database TestDB; 创建测试表table1和table2: ? 1 2 3 4 5 6 7 8 9 1 ...
- MySQL中IN子查询会导致无法使用索引
今天看到一个博客园的一篇关于MySQL的IN子查询优化的案例,一开始感觉有点半信半疑(如果是换做在SQL Server中,这种情况是绝对不可能的,后面会做一个简单的测试.)随后动手按照他说的做了一个表 ...
- MySQL多表数据查询(DQL)
数据准备: /* ------------------------------------创建班级表------------------------------------ */ CREATE TAB ...
- MySQL中in子查询会导致无法使用索引问题(转)
MySQL的测试环境 测试表如下 create table test_table2 ( id int auto_increment primary key, pay_id int, pay_time ...
- sql字段拆分 ,连表子查询获取值
1.连表子查询获取值 select bas.name,bas.id_card_num,bas.mobil_no,gender,bas.birthday,bas.height,bas.weight,pr ...
- 【MySQL】02_子查询与多表查询
子查询 指一个查询语句嵌套在另一个查询语句内部的查询,这个特性从MySQL 4.1开始引入. SQL 中子查询的使用大大增强了 SELECT 查询的能力,因为很多时候查询需要从结果集中获取数据,或者 ...
- mysql 子句、子查询、连接查询
一.mysql查询的五种子句 where子句(条件查询):按照“条件表达式”指定的条件进行查询. group by子句(分组):按照“属性名”指定的字段进行分组.group by子句通常和count( ...
随机推荐
- WebService帮助类改良版,支持多webservice
帮助类代码 using System; using System.CodeDom; using System.CodeDom.Compiler; using System.Collections.Ge ...
- 18-Node.js学习笔记-Express-请求处理-构建模块化路由
构建模块化路由 const express = require('express') //创建路由对象 const home = express.Router(); //将路由和请求路径进行匹配 ap ...
- layui 批量上传
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="piclist.aspx.c ...
- ASP.NET上传断点续传
IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...
- android 小音频频繁播放
android中多媒体文件(音乐和视频)的播放是用MediaPlayer方式是大家比较熟悉的,但是现在要来说一下另外一种音乐文件播放的方式SoundPool,相比较而言,用MediaPlayer来播放 ...
- Python3学习笔记(十二):闭包
闭包定义: 在一个外函数中定义了一个内函数,内函数里引用了外函数的临时变量,并且外函数的返回值是内函数的引用.这样就构成了一个闭包. 我们先来看一个简单的函数: def outer(a): b = 1 ...
- BZOJ 2655 calc (组合计数、DP、多项式、拉格朗日插值)
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2655 题解 据说有一种神仙容斥做法,但我不会. 以及貌似网上大多数人的dp和我的做法都不 ...
- vue-router中,require代替import解决vue项目首页加载时间过久的问题
vue的路由配置文件(routers.js),一般使用import引入的写法,当项目打包时路由里的所有component都会打包在一个js中,在项目刚进入首页的时候,就会加载所有的组件,所以导致首页加 ...
- linux 文件目录介绍
网上的资源 太多了,copy 过来 留给自己用吧! 摘自 : http:/ 使用linux也有一年多时间了 最近也是一直在维护网站系统主机 下面是linux目录结构说明 本人使用的是centos系 ...
- TCP 之 TCP首部
TCP首部图 TCP首部说明 源端口,目的端口 用于寻找发送端和接收端应用进程.(源IP,源端口,目的IP,目的端口) 四元组确定唯一一个TCP连接:(IP,端口)也称为一个插口(socket): 序 ...