mysql 子查询 博客_mysql——多表——子查询——示例
子查询:
子查询是将一个查询语句嵌套在另外一个查询语句中,内层查询语句的查询结果,可以作为外来层查询语句提供查询条件。
因此在特定条件下,一个查询语句的条件,需要另外一个查询语句来获取。
前期准备表:
create table employee ( num int(50),
d_idint(50),
namevarchar(50),
ageint(50),
sexvarchar(50),
homeaddvarchar(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 * fromemployee;create table department ( d_id int(50),
d_namevarchar(50),
functionevarchar(50),
addressvarchar(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);notexists与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),
scoreint(50)
);insert into schoarship(levela,score) values(1,90);insert into schoarship values(2,80);insert into schoarship values(3,70);select * fromschoarship;create table computer_stu ( id int(50),
namevarchar(50),
scoreint(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 fromschoarship );/*结果显示,有7个人获得奖学金,只有id为1005的人没有,因为分数为65,不高于奖学金指定最低分数的任何一个*/
5、带all关键字的子查询
all关键字表示满足所有条件。使用all关键字时,只有满足内层查询语句返回的所有结果,才可以执行外层查询语句。
select * from computer_stu where score >= all ( select score fromschoarship );/*结果显示,只有两个人获得奖学金。因为这两个人的分数比所有奖学金要求的分数都高*/
mysql 子查询 博客_mysql——多表——子查询——示例相关推荐
- mysql多表查询分页面_mysql多表联合查询分点经验给大家
你的位置: 问答吧 -> MySQL -> 问题详情 mysql多表联合查询分点经验给大家 我在工作中天天研究zen cart的程序,那个叫人痛苦,最近比较痛苦的是经常碰见mysql多表联 ...
- mysql 多表查询实例讲解_mysql多表连接查询实例讲解
实际的项目,存在多张表的关联关系.不可能在一张表里面就能检索出所有数据.如果没有表连接的话,那么我们就需要非常多的操作.比如需要从A表找出限制性的条件来从B表中检索数据.不但需要分多表来操作,而且效率 ...
- mysql数据库引擎博客_mysql 数据库引擎常识全集
1.mysql引擎简介: MyISAM引擎是mysql关系数据库管理系统的默认存储引擎(mysql 5.5以前).这种mysql表存贮结构从的旧的ISAM代码扩展出许多有用的功能.在新版本的mysql ...
- mysql老叶博客_MySQL binlog后面的编号最大是多大?【老叶茶馆公众号】
MySQL binlog后面的编号最大是多大? 具体文章请关注微信公众号:izhishuedu [知数堂] 知数堂版权所有. 这里我就不啰嗦了,直接上贴代码: 版本:5.7.18 mysql-5.7. ...
- 【java毕业设计】基于javaEE+SSM+MySql的个人博客系统设计与实现(毕业论文+程序源码)——个人博客系统
基于javaEE+SSM+MySql的个人博客系统设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于javaEE+SSM+MySql的个人博客系统设计与实现,文章末尾附有本毕业设计的论文和源 ...
- Django 3.2.5博客开发教程:体验数据查询
进行数据查询之前,我们需要先进入我们的管理后台,在里面添加一些数据.然后我们用Pycharm打开我们的数据库.具体操作方法:用Pycharm可视化操作数据库 我们的数据结构长这般模样: 双击blog_ ...
- 基于Java/Mysql的个人博客网站
3年前写的一个技术博客...纪念一下. OpenIdea Blog - 开源灵感博客 a personal blog site based on Java/Mysql - 基于Java/Mysql的个 ...
- Wordpress博客首页能打开 子页打不开解决办法
刚安装了wordpress最新版本3.8.2,结果wordpress博客首页倒是能打开,但是子页面,分类页面,tag页面都打不开,全部是404错误,这是什么原因呢? Wordpress博客首页能打开 ...
- mysql对所有id求积_MySQL学习笔记(二)—查询
一.多表连接查询 新建两张表t_user.t_order. 1.内连接 返回满足条件的所有记录. (1)显式内连接 使用inner join关键字,在on子句中设定连接条件. SELECT ...
最新文章
- libgdx 学习笔记一 开发环境搭建
- 2_Python实现基于人脸特征的美颜算法(20181224)
- Qt中线程的生命期问题
- leetcode 986. Interval List Intersections | 986. 区间列表的交集(双指针)
- springmvc跨域问题
- 通过MOXy实现使JAXB更加清洁
- win7驱动程序未经签名可以使用吗_手把手教你解决win7系统驱动程序签名强制禁用的设置技巧...
- 逗比学树莓派之GPIO
- 爬取知乎回答点赞数_python3 爬虫 之只需要问题id爬取知乎问题全部回答
- Atitit nlp 自然语言处理的艺术 attilax著作 v2 t55.docx Atitit nlp 自然语言处理attilax总结 目录 1.1. 主要范畴	1 1.2. 研究难点
- matlab全安装多大_COMSOL Multiphysics 5.3 软件安装教程
- VirtualBoX下linux中安装增强功能
- invoke-obfuscation使用遇到的问题及Encoding免杀
- 【渗透测试】--- rbash逃逸方法简述
- 强大的Tale博客系统v2.0.2源码 支持多主题
- 猜拳php代码,使用JavaScript如何实现猜拳游戏(详细教程)
- mysql求和语句大全_经典SQL语句大全(1)
- 飞控中的IIR二阶滤波器
- 微信公众号标题怎么写更吸引人?
- 2款免费的安卓后台游戏
热门文章
- 第6章 循环结构程序设计
- GARFIELD@05-02-2005
- 使用Python+md5删除本地重复(同一张不重名)的照片
- linux 的常用命令---------第十一阶段(rpm、yum的仓库搭建)
- [算法] 十个经典排序算法
- oralce EM企业管理器
- java-通过JDBC操作数据库
- po3580SuperMemo(splay)
- 【J2me3D系列学习文章之三】(立即模式)对立方体进行变换操作-旋转、缩放、平移...
- Django 分页查询并返回jsons数据,中文乱码解决方法