0
点赞
收藏
分享

微信扫一扫

【LeetCode热题100】2. 两数相加(链表)

菜菜捞捞 2024-03-14 阅读 6

数据库的子查询

子查询充当条件

-- //子查询--充当条件
-- 例1:查询王昭君的成绩,要求显示成绩(标量子查询)
-- 直接用'='
select studentNo from students where name='王昭君';
select * from scores where studentNo=(select studentNo from students where name= '王昭君');
-- 例2:查询18岁的学生的成绩,要求显示成绩(列子查询)
-- 列的话不能用'='而是要用in
select studentNo from students where age =18;
select score from scores where studentNo in(select studentNo from students where age =18);
-- 例3:查询和王昭君同班、同龄的学生信息(行子查询)
select 	class ,age from students where name='王昭君';
select * from students where(class,age)=(select 	class ,age from students where name='王昭君');

子查询充当数据源

-- //子查询--充当数据源
-- 例1:查询数据库和系统测试的课程成绩
-- 拆解:查询数据库和系统测试的成绩
select * from courses where name in ( '数据库', '系统测试');
-- 内连接实现
select * from scores as sc 
inner join (
select * from courses where name in ( '数据库', '系统测试')
) as co on sc.courseNo=co.courseNo;

子查询练习

-- //子查询--练习
-- 1、查询大于平均年龄的学生
select avg(age) from students;
select * from students where age > (select avg(age) from students);
-- 2、查询年龄在18-20之间的学生的成绩
select * from students where age between 18 and 20;
select * from scores as sc inner join (
select * from students where age between 18 and 20
)as stu on sc.courseNo=stu.studentNo;

查询演练

数据准备

/*
**创建部门表
*/
drop table if exists departments;
create table departments (
deptid int(10) primary key,
deptname varchar(20) not null -- 部门名称
);
insert into departments values ( '1001 ', '市场部');
insert into departments values ( '1002 ', '测试部');
insert into departments values ( '1003 ', '开发部');
/*
**创建员工表
*/
drop table if exists employees;
create table employees (
empid int(10) primary key,
empname varchar(20) not null, -- 姓名
sex varchar(4) default null, -- 性别
deptid int(20) default null, -- 部门编号
jobs varchar(20) default null, -- 岗位
politicalstatus varchar(20) default null, -- 政治面貌
leader int(10) default null
);
insert into employees values ( '1 ', '王昭君', '女', '1003 ', '开发', '群众', '9');     insert into employees values ( '2 ', '诸葛亮', '男 ', '1003 ', '开发经理', '群众', null); insert into employees values ( '3 ', '张飞', '男 ', '1002 ', '测试', '团员', '4');      insert into employees values ( '4', '白起', '男 ', '1002 ', '测试经理', '党员', null);  insert into employees values ( '5 ', '大乔', '女', '1002 ', '测试', '党员', '4');      insert into employees values ( '6', '孙尚香', '女', '1001 ', '市场', '党员', '12');   insert into employees values ( '7 ', '百里玄策', '男 ', '1001 ', '市场', '团员', '12');  insert into employees values ( '8 ', '小乔', '女', '1002 ', '测试', '群众', '4');      insert into employees values ( '9 ', '百里守约 ', '男 ', '1003 ', '开发', '党员', '9');   insert into employees values ( '10', '妲己', '女', '1003 ', '开发', '团员', '9');     insert into employees values ( '11 ', '李白 ', '男 ', '1002 ', '测试', '团员', '4');     insert into employees values ( '12 ', '孙膑', '男 ', '1001 ', '市场经理', '党员', null);
/*
**创建工资表
*/
drop table if exists salary;
create table salary (
sid int(10) primary key,
empid int(10) not null,
salary int(10) not null -- 工资
);
insert into salary values ( '1 ', '7 ', '2100');
insert into salary values ( '2 ', '6', '2000');
insert into salary values ( '3 ', '12 ', '5000');
insert into salary values ( '4', '9 ', '1999');
insert into salary values ( '5 ', '10', '1900');
insert into salary values ( '6', '1 ', '3000');
insert into salary values ( '7 ', '2 ', '5500');
insert into salary values ( '8 ', '5 ', '2000');
insert into salary values ( '9 ', '3 ', '1500');
insert into salary values ( '10', '8 ', '4000');
insert into salary values ( '11 ', '11 ', '2600');
insert into salary values ( '12 ', '4', '5300');

演练1:列出总人数大于4的部门号和总人数

-- 1、  列出总人数大于4的部门号和总人数。  (要统计所有部门的人数,  需要使用分组 , 同时也要使用聚合函数)
-- 部门人数》4
-- 分组,以部门号 分组;分组完需要过滤
select deptid, count(*) from employees group by deptid having count(*) > 4;

演练2:列出开发部和和测试部的职工号、姓名

-- 2、  列出开发部和和测试部的职工号、  姓名
-- 有2个表,部门表有开发部和测试部,员工表有职工号、姓名
-- 部门表departments和员工表empname===》 部门号deptid
-- 1)先查部门表中的departments中开发部和测试部对应的部门id
-- 2)员工表再和上面进行关联
select deptid from departments where deptname in ( '开发部', '测试部');
-- 员工表再和子查询结果关联
select empid, empname from employees as emp inner join(
select deptid from departments where deptname in ( '开发部', '测试部'))
as dept on dept.deptid=emp.deptid;

演练3:求出各部门党员的人数,要求显示部门名称

-- 3、  求出各部门党员的人数,  要求显示部门名称。
-- 分组,group by部门 过滤 党员
-- 要显示部门名称,部门名称employees和departments关联
select * from employees as emp
inner join departments as dep on emp.deptid=dep.deptid
where emp.politicalstatus= '党员';

演练4:列出市场部的所有女职工的姓名和政治面貌

-- 4、  列出市场部的所有女职工的姓名和政治面貌。
select * from employees as emp
inner join departments as dep on emp.deptid=dep.deptid
where emp.sex= '女' and dep.deptname= '市场部';

演练5:显示所有职工的姓名、部门名和工资数

select * from employees as emp
inner join departments as dep on emp.deptid=dep.deptid
inner join salary as sa on sa.empid=emp.empid;
举报

相关推荐

0 条评论