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 );

2b9e8e6cb44b941b37d265fba6c1c6cc.png

select * from employee where d_id not in (select d_id from department );

53ef659afd55c8a5fda1c8b00080a1e8.png

二、带exists关键字的子查询

exists关键字表示存在,使用exists关键字时,内层查询语句不用返回查询的记录。而是返回一个真假值。

如果内层查询语句查询到满足条件的记录,就返回一个真值(true);否则,返回一个假值(false);

当返回值为true时,外层查询语句将进行查询;而返回false时,外层查询语句不进行查询或者查询不出任何记录。

select * from employee where exists (select d_name from department where d_id = 1003);

8d7b5bd0c13d8b7ab38c16f71cc48e21.png

select * from employee where exists (select d_name from department where d_id = 1004);

589175b80dccbdd7139dc80d5eb20e7e.png

其它:

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;

7126fa982b38392c41e09104dc8b46aa.png

select * from computer_stu;

5b1026e3c624551f78aed23eff515a13.png

3、带比较运算符的子查询

select id,name,score from computer_stu where score >= (select score from schoarship where levela = 1);/*查询获得一等奖学金的学生有哪些,第一个表为奖学金等级和最低分数*/

dcc3f4825269f37524527892f61dbf8a.png

select d_id,d_name from department where d_id != (select d_id from employee where age = 24);/*只有生产部和销售部没有年龄等于24岁的员工*/

e6fc5959eea9420fb1b7e0ef4126a314.png

4、带any关键字的子查询

any关键字表示满足其中任何一个条件。使用any关键字时,只要满足内查询语句返回的结果中的任何一个,就可以通过该条件来执行外层查询语句

select * from computer_stu where score >= any ( select score fromschoarship );/*结果显示,有7个人获得奖学金,只有id为1005的人没有,因为分数为65,不高于奖学金指定最低分数的任何一个*/

ad03975db4343b81d06dd62ddeb74cda.png

5、带all关键字的子查询

all关键字表示满足所有条件。使用all关键字时,只有满足内层查询语句返回的所有结果,才可以执行外层查询语句。

select * from computer_stu where score >= all ( select score fromschoarship );/*结果显示,只有两个人获得奖学金。因为这两个人的分数比所有奖学金要求的分数都高*/

4e67fdbdc1e3336c879f55fd3863f9cd.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值