1.连表查询

连表查询大多数都作用在外键得基础 ------表与表之间有关联

表与表之间存在得关系

  1. 一对多: 在多得一方添加外键列

  2. 多对多: 需要在创建一个中间表,该表中至少有两个外键列

1.1 内连接:

-- 隐式查询 select 列名.... from 表1,表2 where 连表得条件。-- 连表查询时,如果不使用连表条件则出现笛卡尔集。-- 所谓笛卡尔集 就是A表中每一条记录关联B中中得每条记录
-- 1.查询每一个员工的姓名,及关联的部门的名称〔隐式内连接实现)
select * from tb_emp,tb_dept where tb_emp.dept_id=tb_dept.id;
-- 如果表的名字很长 可以为表起别名
select * from tb_emp e,tb_dept d where e.dept_id=d.id;-- 显示连接: A表 inner join B表 on 连表条件。
-- 2.查询每一个员工的姓名,及关联的部门的名称〔显式内连接实现)
select * from tb_emp  inner join tb_dept on tb_emp.dept_id=tb_dept.id;
-- 如果表的名字很长 可以为表起别名
select * from tb_emp e inner join tb_dept d on e.dept_id=d.id;
-- 上面的inner可以省略
select * from tb_emp e join tb_dept d on e.dept_id=d.id;

1.2 外连接:

-- 语法: select 查询列集 from A表 left join B表 on 连表条件
-- 1.查询emp表的所有数据, 和对应的部门信息(左外连接)
select * from tb_emp e left outer join tb_dept d on e.dept_id=d.id;
select * from tb_emp e left  join tb_dept d on e.dept_id=d.id;-- 2.查询dept表的所有数据,和对应的员工信息(右外连接)
select * from tb_emp e right outer join tb_dept d on e.dept_id=d.id;
select * from tb_emp e right  join tb_dept d on e.dept_id=d.id;

1.3 自联查询:

自己和自己相连接查询  ---- select * from A表 join A表 on 连表条件

-- 自联查询
-- 自己和自己相连接查询 select * from A表 join A表 on 连表条件。
-- -- 1.查询员工及其所属领导的名字-- 你要查询的结果再一张表中,但是还不能使用单表查询得到结果-- 显示内连接
select a.name,b.name from tb_emp a join tb_emp b on a.managerid=b.id;-- 2.查询所有员工 emp及其领导的名字emp ,如果员工没有领导,也需要查询出来
-- 左外连接
select a.name,b.name from tb_emp a left join tb_emp b on a.managerid=b.id;

2. 子查询---嵌套查询

-- 子查询-- 嵌套查询
-- 查询市场部的员工信息:市场部-- tb_dept 市场部的编号,然后再根据编号查询员工信息
-- 1 根据市场部 查询部门编号
select id from tb_dept where name='市场部';
-- 2 根据部门编号 查询该部门下的员工信息
select * from tb_emp where dept_id=2;-- 查询市场部的员工信息:子查询返回的结果一列一条记录, 这个时候可以用=
select * from tb_emp where dept_id=(select id from tb_dept where name='市场部');-- 子查询能能解决的都可以用连表查询,但是连表查询能解决的子查询不一定解决
select e.*from tb_emp e,tb_dept d where e.dept_id=d.id and d.name='市场部';-- 查询市场部和研发部员工的信息  in
-- 查询市场部和研发部员工的信息
-- 1 查询市场部和研发部的编号
select id from tb_dept where name in('市场部','研发部');
-- 2 再员工表中根据部门编号查询员工信息
select * from tb_emp where dept_id in(1,2);
select * from tb_emp where dept_id in(select id from tb_dept where name in('市场部','研发部'));-- 查询在“方东白”入职之后的员工信息
-- 1 查询方东白入职日期
select entrydate from tb_emp where name='方东白';
-- 2 根据方东白的入职日期查询其他员工的信息
select * from tb_emp where entrydate>(select entrydate from tb_emp where name='方东白');-- 查询比财务部所有人工资都高的员工信息
-- 1 求出财务部最高的工资
select max(salary) from tb_emp e join tb_dept d on e.dept_id=d.id where d.name='财务部';
--  根据财务部最高工资查询其他员工信息
select * from tb_emp where salary>(select max(salary) from tb_emp e join tb_dept d on e.dept_id=d.id where d.name='财务部');

3. 组合查询

-- 组合查询
-- 多个查询的结果 组合到一起-- sql union sql --->把这两条sql查询的结果组合到一起,如果有重复记录则合并成一条-- sql union all sql --->把这两条sql查询的结果组合到一起,如果有重复记录,不合并
-- 注意: 这两条sql返回的字段必须一样select * from tb_emp where salary>8000
union all
select * from tb_emp where age>40;select * from tb_emp where salary>8000
union
select * from tb_emp where age>40;

例题1:

create table tb_emp(id int primary key auto_increment COMMENT '员工编号',name varchar(20) COMMENT '员工姓名',age int COMMENT '员工年龄',job varchar(20) COMMENT '岗位',salary int COMMENT '薪水',entrydate date COMMENT '员工入职时间',managerid int COMMENT '员工领导编号',dept_id int COMMENT '员工所在部门编号'
);
create table tb_dept(id int primary key auto_increment,name varchar(20)
);
insert into tb_dept(name) values('研发部'),('市场部'),('财务部'),('销售部'),('总经办');insert into tb_emp values
(null,'金庸',66,'总裁',20000,'2000-01-01',null,5),
(null,'张无忌',20,'项目经理',12500,'2005-12-01',1,1),
(null,'杨逍',33,'开发',8400,'2000-11-03',2,1),
(null,'韦一笑',48,'开发',11000,'2002-03-05',2,1),
(null,'常豫川',43,'开发',10500,'2004-09-01',2,1),
(null,'小昭',19,'程序员鼓励师',6600,'2004-10-12',2,1),
(null,'灭绝',60,'财务总监',8500,'2002-09-12',1,3),
(null,'周芷若',60,'会计',48000,'2006-06-01',7,3),
(null,'丁敏君',19,'出纳',4000,'2009-06-01',7,3),
(null,'赵敏',23,'市场总监',14000,'2009-06-01',1,2),
(null,'鹿仗客',56,'职员',3750,'2009-06-01',10,2),
(null,'鹤比翁',19,'职员',3750,'2009-06-01',10,2),
(null,'方东白',19,'职员',3750,'2009-06-01',10,2),
(null,'张三丰',88,'销售总监',14000,'2004-06-01',1,4),
(null,'玉莲舟',38,'销售',4600,'2009-06-01',14,4),
(null,'宋远桥',40,'销售',4600,'2009-06-01',14,4),
(null,'陈友谅',42,null,2000,'2011-06-01',1,null);create table salgrade(grade int,losal int,hisal int
)
insert into salgrade values (1,0,3000);
insert into salgrade values (2,3001,5000);
insert into salgrade values (3,5001,8000);
insert into salgrade values (4,8001,10000);
insert into salgrade values (5,10001,15000);
insert into salgrade values (6,15001,20000);
insert into salgrade values (7,20001,25000);
insert into salgrade values (8,25001,30000);-- 1:查询雇员表中工资最高的雇员的员工号、员工姓名、工资和部门号
select * from tb_emp where salary=(select max(salary) from tb_emp);-- 2:查询每个雇员和其所在的部门名
select e.*,d.name from tb_emp e ,tb_dept d where e.dept_id=d.id;-- 3:查询每个雇员姓名及其工资所在的等级
select  e.name , s.grade from  tb_emp e , salgrade s where e.salary BETWEEN s.losal and s.hisal;-- 4:查询雇员名第2个字母不是敏的雇员的姓名、所在的部门名、工资所在的等级
select e.name, d.name , s.grade from tb_emp e, salgrade s,  tb_dept d where e.salary BETWEEN s.losal and s.hisal   and e.dept_id=d.id
and e.name not like '_敏%';-- 5:查询每个雇员和其经理的姓名
select a.name,b.name from tb_emp a,tb_emp b where a.managerid=b.id;-- 6:查询每个雇员和其经理的姓名(包括公司老板本身(他上面没有经理))
select a.name,b.name from tb_emp a left join tb_emp b on a.managerid=b.id;-- 7:查询每个雇员的姓名及其所在部门的部门名(包括没有雇员的部门)
select e.name,d.name from tb_emp e right join tb_dept d on e.dept_id=d.id;-- 8:查询每个部门中工资最高的人的姓名、薪水和部门编号
select name,salary,dept_id from tb_emp where (dept_id,salary) in
(select dept_id,max(salary) maxsalary from tb_emp group by dept_id);-- 9:查询每个部门平均工资所在的等级
select d.name 部门名 ,avg(e.salary) 平均工资 , s.grade 工资等级 from tb_emp e ,tb_dept d,salgrade s
where e.dept_id=d.id and (e.salary > s.losal and e.salary <= s.hisal) group by d.name;-- 10.查询员工的姓名、年龄、职位、部门信息―(隐式内连接)
select e.name,e.age,e.job,d.name from tb_emp e , tb_dept d where e.dept_id=d.id;
select * from tb_emp e,tb_dept d where e.dept_id=d.id;
-- 11.查询年龄小于30岁的员工的姓名、年龄、职位、部门信息〈显式内连接)
select e.name,e.age,e.job,d.name from tb_emp e join tb_dept d on e.dept_id=d.id where e.age<30;-- 12.查询拥有员工的部门ID、部门名称
select * from tb_dept where id in(select dept_id from tb_emp where dept_id is not null);
-- 13.查询所有年龄大于40岁的员工,及其归属的部门名称;如果员工没有分配部门,也需要展示出来
select e.*, d.name from tb_emp e left join tb_dept d on e.dept_id=d.id where e.age>40;-- 14.查询所有员工的工资等级
select e.name,s.grade from tb_emp e,salgrade s where e.salary between s.losal and s.hisal;select e.*,s.grade from tb_emp e left join salgrade s on e.salary BETWEEN s.losal and s.hisal;
-- 15.查询“研发部”所有员工的信息及工资等级
select e.*, s.grade from tb_emp e join tb_dept d on d.id=e.dept_id,salgrade s where d.name='研发部' and e.salary>=s.losal and e.salary <=s.hisal;-- 16.查询“研发部”员工的平均工资
select avg(e.salary) from tb_emp e,tb_dept d where e.dept_id=d.id and d.name='研发部';select avg(salary) from tb_emp e join tb_dept d on e.dept_id=d.id where d.name='研发部';
-- 17.查询工资比“灭绝”高的员工信息
select * from tb_emp where salary>(select max(salary) from tb_emp  where name='灭绝');-- 18.查询比平均薪资高的员工信息
select * from tb_emp where salary>(select avg(salary) from tb_emp);
-- 1:查询雇员表中工资最高的雇员的员工号、员工姓名、工资和部门号
select * from tb_emp where salary=(select max(salary) from tb_emp);-- 2:查询每个雇员和其所在的部门名
select e.*,d.name from tb_emp e ,tb_dept d where e.dept_id=d.id;-- 3:查询每个雇员姓名及其工资所在的等级
select  e.name , s.grade from  tb_emp e , salgrade s where e.salary BETWEEN s.losal and s.hisal;-- 4:查询雇员名第2个字母不是敏的雇员的姓名、所在的部门名、工资所在的等级
select e.name, d.name , s.grade from tb_emp e, salgrade s,  tb_dept d where e.salary BETWEEN s.losal and s.hisal   and e.dept_id=d.id
and e.name not like '_敏%';-- 5:查询每个雇员和其经理的姓名
select a.name,b.name from tb_emp a,tb_emp b where a.managerid=b.id;-- 6:查询每个雇员和其经理的姓名(包括公司老板本身(他上面没有经理))
select a.name,b.name from tb_emp a left join tb_emp b on a.managerid=b.id;-- 7:查询每个雇员的姓名及其所在部门的部门名(包括没有雇员的部门)
select e.name,d.name from tb_emp e right join tb_dept d on e.dept_id=d.id;-- 8:查询每个部门中工资最高的人的姓名、薪水和部门编号
select name,salary,dept_id from tb_emp where (dept_id,salary) in
(select dept_id,max(salary) maxsalary from tb_emp group by dept_id);-- 9:查询每个部门平均工资所在的等级
select d.name 部门名 ,avg(e.salary) 平均工资 , s.grade 工资等级 from tb_emp e ,tb_dept d,salgrade s
where e.dept_id=d.id and (e.salary > s.losal and e.salary <= s.hisal) group by d.name;-- 10.查询员工的姓名、年龄、职位、部门信息―(隐式内连接)
select e.name,e.age,e.job,d.name from tb_emp e , tb_dept d where e.dept_id=d.id;
select * from tb_emp e,tb_dept d where e.dept_id=d.id;
-- 11.查询年龄小于30岁的员工的姓名、年龄、职位、部门信息〈显式内连接)
select e.name,e.age,e.job,d.name from tb_emp e join tb_dept d on e.dept_id=d.id where e.age<30;-- 12.查询拥有员工的部门ID、部门名称
select * from tb_dept where id in(select dept_id from tb_emp where dept_id is not null);
-- 13.查询所有年龄大于40岁的员工,及其归属的部门名称;如果员工没有分配部门,也需要展示出来
select e.*, d.name from tb_emp e left join tb_dept d on e.dept_id=d.id where e.age>40;-- 14.查询所有员工的工资等级
select e.name,s.grade from tb_emp e,salgrade s where e.salary between s.losal and s.hisal;select e.*,s.grade from tb_emp e left join salgrade s on e.salary BETWEEN s.losal and s.hisal;
-- 15.查询“研发部”所有员工的信息及工资等级
select e.*, s.grade from tb_emp e join tb_dept d on d.id=e.dept_id,salgrade s where d.name='研发部' and e.salary>=s.losal and e.salary <=s.hisal;-- 16.查询“研发部”员工的平均工资
select avg(e.salary) from tb_emp e,tb_dept d where e.dept_id=d.id and d.name='研发部';select avg(salary) from tb_emp e join tb_dept d on e.dept_id=d.id where d.name='研发部';
-- 17.查询工资比“灭绝”高的员工信息
select * from tb_emp where salary>(select max(salary) from tb_emp  where name='灭绝');-- 18.查询比平均薪资高的员工信息
select * from tb_emp where salary>(select avg(salary) from tb_emp);-- 19.查询所有的部门信息,并统计部门的员工人数
select d.*, (select count(*) from tb_emp e where e.dept_id=d.id) as '人数' from tb_dept d;select d.*, ifnull(c.rs,0) from tb_dept d left join (select dept_id,count(id) rs from tb_emp where dept_id is not null group by dept_id) con d.id=c.dept_id order by d.id
-- 19.查询所有的部门信息,并统计部门的员工人数
select d.*, (select count(*) from tb_emp e where e.dept_id=d.id) as '人数' from tb_dept d;select d.*, ifnull(c.rs,0) from tb_dept d left join (select dept_id,count(id) rs from tb_emp where dept_id is not null group by dept_id) con d.id=c.dept_id order by d.id;

例题2:

-- 创建表readers 读者信息表
create table readers(reader_id int(7) primary key,-- 读者idname varchar(8),-- 读者姓名sex varchar(2) default '男' check(sex in('男','女')),dept varchar(16),-- 读者所在部门status varchar(8),-- 学历address varchar(30)-- 地址
);
-- 图书信息表
create table books(book_id int(6) primary key,-- 图书编号type_id varchar(3),-- 类型编号book_name varchar(50),-- 图书名author varchar(50),-- 作者publisher varchar(50),-- 出版社price int(3)-- 价格
);
-- 借阅信息表
create table borrow_info(reader_id int(7),-- 读者idbook_id int(6),-- 图书Idborrow_time date,-- 借书时间notes varchar(200),-- 标志primary key(reader_id,book_id)
);-- 添加数据readers
insert into readers values('0034103','范元帅','男','艺术学院','本科生','师大南院');
insert into readers values('0034301','杨凡','男','电信学院','教师','八里台禄小区');
insert into readers values('0034429','许丹丹','女','艺术学院','本科生','师大南院');
insert into readers values('0134101','陈超','男','计算机学院','本科生','师大南院');
insert into readers values('0134102','范金良','男','计算机学院','本科生','师大南院');
insert into readers values('0134103','国皓','男','计算机学院','本科生','师大南院');
insert into readers values('0134104','贺云龙','男','计算机学院','本科生','师大南院');
insert into readers values('0134105','刘德文','男','计算机学院','本科生','师大南院');
insert into readers values('0134106','彭俊','男','计算机学院','本科生','师大南院');
insert into readers values('0134107','彭志成','男','计算机学院','本科生','师大南院');
insert into readers values('0134108','沈举','男','计算机学院','本科生','师大南院');
insert into readers values('0134109','田强','男','计算机学院','本科生','师大南院');
insert into readers values('0134110','王旭','男','计算机学院','本科生','师大南院');
insert into readers values('0134111','吴卫','男','计算机学院','本科生','师大南院');
insert into readers values('0134112','熊敏','男','计算机学院','本科生','师大南院');
insert into readers values('0134113','颜东','男','计算机学院','本科生','师大南院');
insert into readers values('0134114','殷建鹏','男','计算机学院','本科生','师大南院');
insert into readers values('0134115','周春林','男','计算机学院','本科生','师大南院');
insert into readers values('0134116','陈洁','女','计算机学院','本科生','师大南院');
insert into readers values('0134117','陈琪','女','计算机学院','研究生','师大南院');
insert into readers values('0134118','丁璐','女','计算机学院','研究生','师大南院');
insert into readers values('0134119','丁岩','女','计算机学院','本科生','师大南院');
insert into readers values('0134120','董蕾','女','计算机学院','本科生','师大南院');
insert into readers values('0134121','何芳','女','计算机学院','本科生','师大南院');
insert into readers values('0134122','蒋莎','女','计算机学院','本科生','师大南院');
insert into readers values('0134123','李颖','女','计算机学院','研究生','师大南院');
insert into readers values('0134124','李芬','女','计算机学院','研究生','师大南院');
insert into readers values('0134125','李锦萍','女','计算机学院','教授','师大南院');
insert into readers values('0134126','李小汐','女','计算机学院','本科生','师大南院');
insert into readers values('0134127','李玥玖','女','计算机学院','本科生','师大南院');
insert into readers values('0134128','郦莎','女','计算机学院','本科生','师大南院');
insert into readers values('0134129','廖英','女','计算机学院','本科生','师大南院');
insert into readers values('0134130','林琳','女','计算机学院','教师','师大南院');
insert into readers values('0134131','林敏','女','计算机学院','本科生','师大南院');insert into books values('109101','BAS','高等数学','李方健','清华大学出版社','20');
insert into books values('109104','BAS','离散数学','孙德风','天津大学出版社','22');
insert into books values('109107','ELC','数字电路','刘国庆','高等教育出版社','13');
insert into books values('109109','CMP','C语言程序设计','谭浩强','清华大学出版社','15');
insert into books values('109110','CMP','数据结构','王志国','高等教育出版社','32');
insert into books values('109111','CMP','操作系统','王志国','高等教育出版社','25');
insert into books values('109112','CMP','计算机组成原理','张小敏','南大在学出版社','27');
insert into books values('109113','CMP','微机原理与接口技术','刘国庆','人民大学出版社','34');
insert into books values('109114','CMP','数据库原理','彭来德','高等教育出版社','16');
insert into books values('109115','CMP','计算机网络','马国露','人民大学出版社','11');
insert into books values('109116','CMP','计算机网络','谭浩强','南大在学出版社','13');
insert into books values('109117','CMP','编译原理','方刚','清华大学出版社','38');
insert into books values('209101','CMP','VB与WINDOWS程序设计','谭浩强','清华大学出版社','30');
insert into books values('209102','CMP','C++与面向对象技术','谭浩强','人民大学出版社','19');
insert into books values('209103','CMP','Java与网络程序设计','付勇','高等教育出版社','20');
insert into books values('209106','CMP','单片机原理及应用','刘国庆','人民大学出版社','22');
insert into books values('209107','CMP','PLC原理及其应用开发','刘国庆','南大在学出版社','16');
insert into books values('209111','CMP','人工智能导论','丁宝康','高等教育出版社','18');
insert into books values('209116','CMP','信息论与编码学概论','丁宝康','高等教育出版社','21');
insert into books values('209117','CMP','密码学基础','张顺志','人民大学出版社','25');
insert into books values('209123','CMP','Internet应用及网页设计','李朋','高等教育出版社','16');
insert into books values('209124','CMP','多媒体技术及应用','谭浩强','高等教育出版社','15');
insert into books values('209130','CMP','PB与数据库应用开发','张华强','清华大学出版社','42');
insert into books values('209132','CMP','计算机图形学','徐志超','人民大学出版社','27');
insert into books values('309102','CMP','计算机学科教学论','丁宝康','高等教育出版社','12');
insert into books values('309103','CMP','中学计算机教材研究与分析','徐志超','人民大学出版社','14');
insert into books values('309104','CMP','信息技术概论','付勇','天津大学出版社','24');
insert into books values('309105','CMP','数据库分析与设计','丁宝康','人民大学出版社','30');
insert into books values('309106','CMP','数据库导论','付勇','清华大学出版社','29');insert into borrow_info values('0034103','109101','2005-6-2 00:00:00','NULL');
insert into borrow_info values('0034301','209130','2005-6-14 00:00:00','NULL');
insert into borrow_info values('0034429','109107','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134101','109109','2005-6-3 00:00:00','NULL');
insert into borrow_info values('0134102','109110','2005-6-30 00:00:00','NULL');
insert into borrow_info values('0134103','109111','2005-6-13 00:00:00','NULL');
insert into borrow_info values('0134104','109112','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134105','109113','2005-6-14 00:00:00','NULL');
insert into borrow_info values('0134106','109114','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134107','109115','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134108','109116','2005-6-12 00:00:00','NULL');
insert into borrow_info values('0134109','109117','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134110','209101','2005-6-22 00:00:00','NULL');
insert into borrow_info values('0134111','209102','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134112','209103','2005-6-16 00:00:00','NULL');
insert into borrow_info values('0134113','209106','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134114','209107','2005-6-11 00:00:00','NULL');
insert into borrow_info values('0134115','209111','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134116','209106','2005-6-14 00:00:00','NULL');
insert into borrow_info values('0034301','209132','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0034301','309102','2005-6-9 00:00:00','NULL');
insert into borrow_info values('0034301','309103','2005-6-21 00:00:00','NULL');
insert into borrow_info values('0034301','309104','2005-6-14 00:00:00','NULL');
insert into borrow_info values('134101','109101','2005-6-1 00:00:00','NULL');
insert into borrow_info values('134102','109104','2005-6-8 00:00:00','NULL');
insert into borrow_info values('134104','109107','2005-6-1 00:00:00','NULL');
insert into borrow_info values('134105','109109','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134426','109110','2005-6-15 00:00:00','NULL');
insert into borrow_info values('0134427','109111','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134428','109112','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134429','109113','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134430','109114','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134431','109115','2005-6-1','NULL');
insert into borrow_info values('0134432','109116','2005-6-14','NULL');
insert into borrow_info values('0134433','109117','2005-6-1','NULL');
insert into borrow_info values('0134434','209101','2005-6-2','NULL');
insert into borrow_info values('0134435','209102','2005-6-1','NULL');
insert into borrow_info values('0134430','209103','2005-6-5','NULL');
insert into borrow_info values('0134430','209106','2005-6-5','NULL');
insert into borrow_info values('0134430','209107','2005-6-21','NULL');
insert into borrow_info values('0134430','209111','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134430','209116','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134430','209124','2005-6-5 00:00:00','NULL');
-- 1、检索读者“杨凡”所在单位
select dept from readers where name='杨凡';-- 2、检索所有读者的全部信息
select * from readers;-- 3、检索图书馆中所有藏书的书名和出版单位
select book_name,publisher from books;-- 4、检索“人民大学出版社”所有的书名和单价,结果按照单价降序排列
select book_name,price from books where publisher='人民大学出版社' order by price desc;-- 5、检索价格在10元至15元之间的图书的名称、作者、单价和分类号,结果按分类号和单价升序排列
select book_name , author , price , type_id from books where price between 10 and 15 ORDER BY type_id , price ;-- 6、检索“人民大学出版社”和“清华大学出版社”的所有图书的名称和作者
select book_name,author from books where publisher in('人民大学出版社','清华大学出版社');-- 7、检索书名以“数据库”开头的所有图书的书名和作者
select book_name,author from books where book_name like '数据库%';-- 8、检索同时借了总编号为209116和209124两本图书的借书证号
select a.reader_id from borrow_info a , borrow_info b where a.book_id = '209116' and b.book_id = '209124';-- 9、检索所有借阅了图书的读者姓名和所在单位
select name,dept from readers r join borrow_info b on r.reader_id=b.reader_id;-- 10、检索“扬凡”所借的所有图书的书名和借阅日期
select book_name,borrow_time from books join borrow_info b on books.book_id=b.book_id where b.reader_id=(select reader_id from readers where name='杨凡');-- 11、检索价格在20元以上且已经借出的图书,结果按单价降序排列
select books.* from books join borrow_info b on books.book_id=b.book_id where price >20 order by price desc;-- 12、检索借阅了“C语言程序设计”一书的读者姓名和所在单位
select name,dept from readers r, borrow_info  b where r.reader_id=b.reader_id and b.book_id=(select book_id from books where book_name='C语言程序设计');-- 13、检索与“杨凡”在同一天借阅了图书的读者的姓名和所在单位
select name,dept from readers r, borrow_info b where r.reader_id=b.reader_id and borrow_time in
(select borrow_time from borrow_info where reader_id(select reader_id from readers where name='杨凡'))and name!='杨凡';-- 14、检索藏书中比“高等教育出版社”的所有图书的单价更高的图书
select books.* from books where price>(select max(price) from books where publisher='高等教育出版社');-- 15、检索藏书中所有与“数据库导论”或“数据库原理”在同一出版社出版的图书
select books.* from books where publisher in(select publisher from books where book_name in('数据库导论','数据库原理'))and book_name not in('数据库导论','数据库原理');-- 16、求该图书馆藏书的总册数
select count(*) from books;-- 17、求“高等教育出版社”的图书中最高的价格、最低的价格以及平均价格
select max(price),min(price),avg(price) from books where publisher='高等教育出版社';-- 18、求“计算机学院”当前借阅了图书的读者人数
select count(*) from borrow_info b ,readers r where r.reader_id=b.reader_id and dept='计算机学院';-- 19、求各个出版社的最高价格、最低价格、平均价格
select max(price),min(price),avg(price) from books group by publisher;-- 20、分别求出各个单位当前借阅图书的读者人数
select count(*) from borrow_info b , readers r where r.reader_id=b.reader_id group by dept;-- 21、求各个出版单位的册书、价格总额,并按总价降序排列,如有总价相同者按出版社名称降序排列
select count(*),sum(price) from books group by publisher order by sum(price) desc; -- 22、检索当前至少借阅了5本图书的读者姓名和所在单位
select count(*),name,dept from readers r join borrow_info b on r.reader_id=b.reader_id group by name having count(*)>=5;-- 23、分别找出借书人数超过10个人的单位和人数
select dept,count(*) from readers r join borrow_info b on r.reader_id=b.reader_id group by dept having count(*)>=10;-- 24、检索没有借阅任何图书的读者姓名和所在单位
select name,dept from readers where reader_id not in(select reader_id from borrow_info); 

sql连表查询、子查询、组合查询相关推荐

  1. Mysql的多表查询(表添加,多表查询练习:笛卡尔积、内连接、外连接、子查询、UNION组合查询)

    https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注! 欢迎关注微信公众号:宝藏女孩的成长日记 如有转载,请注明出处(如不注明,盗者必究) 目录 一.表的创建 ...

  2. oracle多表嵌套查询使用,oracle sql 多表 嵌套子查询 连接查询, join where exist i...

    转:http://hi.baidu.com/delphi_relive/blog/item/d7c0034a49c4932208f7ef21.html in 和 exists也是很好区别的. in 是 ...

  3. oracle sql 多表 嵌套子查询 连接查询, join where exist in 的区别

    sql中exits和in的区别 转:http://hi.baidu.com/delphi_relive/blog/item/d7c0034a49c4932208f7ef21.html in 和 exi ...

  4. 【SQL自学打卡|DAY13】——组合查询

    前言 ❤欢迎大家阅读我的文章呀❤ 今天是SQL必知必会的最后一块练习. 希望你们在我的文章当中能有所收获!!! SLogan:利用有限的时间,撸起袖子加油干! 知识点回顾 内联结:inner join ...

  5. 连表查询使用in_SQL 组合查询

    SQL允许执行多个查询(多条SELECT语句),并将结果作为一个查询结果返回. 使用UNION操作符,可以给出多条SELECT语句,将它们的结果组合成一个结果集. 1.使用UNION 给出每条SELE ...

  6. phpstudy -sql服务器2008r2 -中控考勤机 -php 组合查询考勤

    1.去官网http://www.phpstudy.net/下载并安装phpstudy 2.修改php.ini配置文件如下: mssql.secure_connection = on extension ...

  7. SQL Server表的数据量大小查询

    今天想在服务器上还原一个DB,发现磁盘空间不够,查看发现,其中一个DB竟然有56G了.因此想收缩一下这个DB,发现大小没多大变化.然后在网上找了找SQL脚本,看能不能查看下哪个表的数据量那么大. 网上 ...

  8. 使用python对学生表的查询_多表组合查询——Python操作Mysql数据库

    前面我们介绍了单张表的查询,包括模糊查询.分组.排序.各种筛选条件等等操作,在实际应用中,查询的数据往往不止局限在一张表里,通常需要多张表在一起进行组合查询,今天我们将会对Mysql当中的多张有关联的 ...

  9. 数据库9:联结表 高级联结 组合查询 全文本搜索

    第十五章联结表 Sql最强大的功能之一就是能在数据检索查询的执行中联结(join)表.联结是利用sql的select能执行的最重要的操作,能很好的理解联结及其语法是学习sql的一个极为重要的组成部分. ...

  10. 数据库系统实训——实验三——子查询与组合查询

    一.子查询与组合查询 题号:1 SQL语句描述: /SELECT ORDER_NUM FROM ORDERITEMS where prod_id='tnt2';/ /select cust_id fr ...

最新文章

  1. win10 php mysql_win10 下 apache php mysql 开发环境安装
  2. 文件格式用Latex排版论文(1)如何将Visio画图文件转换成Latex支持的.eps文件
  3. 根据id/类名/元素名称查找元素
  4. OpenStack 部署运维实战
  5. 转载--c语言宏定义(1)
  6. python 生成排列、组合以及选择
  7. 移动文件读/写指针----lseek
  8. LeetCode 421. 数组中两个数的最大异或值(Trie树)
  9. bellman ford java_Java C 实现Bellman-ford算法
  10. inventor2019有无CAE_Inventor2019最新下载_Inventor2019正式版 - 软件帝
  11. 【solr基础教程之中的一个】Solr相关知识点串讲
  12. JS 获取 URL 地址/参数
  13. 设置maven的本地位置和maven镜像
  14. MySQL——事务(Transaction)详解
  15. 钽电容黑色和黄色的区别
  16. UNI-APP 桌面LOGO角标设置(ios)
  17. 画圆形头像的简单画法
  18. 【情报工具】分享15款保护隐私的搜索引擎
  19. 查询出部门名称、部门的员工数、部门的平均工资、部门的最低收入雇员姓名和最高收入雇员的姓名
  20. 股票查询接口功能是什么?

热门文章

  1. QGIS转换shp/img文件坐标系(wgs84/gcj02/bd09)
  2. hmailserver的反垃圾邮件功能
  3. 看见更有远见的技术管理
  4. 火影150集碎片拾忆 记于2014-04-08
  5. 设计模式之工厂模式(C++)
  6. php编码小坑之调用新浪天气api和阿凡达天气api
  7. 关于EFS加密原理及破解浅谈
  8. SXSSFWorkbook操作Excel表格
  9. Linux服务器挂载ntfs硬盘,Linux中挂载NTFS格式的硬盘的方法
  10. 函数的length属性