您的位置:首页 > 数据库 > MySQL

MySQL表查询实训笔记

2020-06-21 04:41 801 查看

MySQL表查询实训笔记

设bookstores数据库有三张表如下:

book表:

b_id b_type b_name b_author b_publish b_time b_price b_number b_dis
B116628 计算机 计算机基础 张小华 清华大学出版社 2017/9/8 40 10 0.8
B116629 计算机 数据库概论 李雪 人民邮电出版社 2018/5/6 24.5 20 0.9
B116630 计算机 C语言程序设计 王天明 高等教育出版社 2015/4/9 33.5 32 0.8
B116631 数学 高等数学 赵夏天 清华大学出版社 2017/10/12 37 45 0.7
B116632 数学 线性代数 李晓婷 高等教育出版社 2018/12/4 45 33 0.9
B116633 数学 离散数学 张天天 清华大学出版社 2018/1/3 55 56 0.8
B116634 数学 数学分析 王敏 人民邮电出版社 2015/11/22 49 71 0.9
B116635 英语 新概念英语1 赵虎 人民教育出版社 2016/6/8 43 25 0.7
B116636 英语 新概念英语2 李欣甜 高等教育出版社 2017/9/7 34.5 43 0.8
B116637 英语 新概念英语3 唐晓米 人民教育出版社 2016/9/5 28 44 0.9

member表:

m_id m_name m_gender m_pw m_tel m_time
V00001 张星星 123456 13428743747
V00002 李阳阳 666666 13437528739
V00003 王泰 888888 13765784980
V00004 赵诗 234567 13876453728 2018/1/4
V00005 李莎 123456 18848739012 2018/1/5
V00006 张群 123456 2017/3/9
V00007 陈小军 123456 2017/3/10
V00008 张金龙 123456 2017/3/11
V00009 王雪 123456 2017/3/12
V00010 赵兰芬 123456 2017/3/13

bookorser表

m_id b_id o_id o_number o_time o_fh o_sh o_jq
V00001 B116628 1000000001 5 2019/12/10
V00001 B116629 1000000001 2 2019/12/10
V00001 B116633 1000000001 3 2019/12/10
V00002 B116629 1000000002 2 2020/1/10
V00002 B116634 1000000002 3 2020/1/10
V00003 B116637 1000000003 2 2020/2/27
V00004 B116630 1000000004 1 2020/3/20
V00001 B116630 1000000004 1 2020/3/20
V00002 B116631 1000000005 3 2020/3/26
V00002 B116628 1000000005 2 2020/3/26

已吸收的知识点回顾:

父查询

where (子查询)

in、any[some]、all、exists 、比较运算符   --> 作为父查询和子查询的链接条件
=   	// 值1 = 值2

= all   // 值集合(所有值都相等)

x = all(1,2,3)  [需要满足x=1 and x=2 and x=3时才成立]

= any	// 值集合(任一值相等则满足条件)

x = any(1,2,3)  [只要满足x=1 or x=2 or x=3任一一个条件即成立]

--> in 和 =any 效果一样
--> 在实际运用中看子查询条件返回几个值,如果是一个则用 =
--> 当你不能确定子查询返回值是一个或是多个时,则用 =any 和 =all

子查询习题

  • 1、查找比《高等数学》库存量还要高的图书编号和书名。
select b_id as 图书编号,b_name as 会员姓名 from book
where b_number >
(select b_number from book where b_name='高等数学');

也可以使用链接查询:在这里插入代码片
select s2.b_id,s2.b_name
from book as s1 join book as s2
on s2.b_number>s1.b_number
where s1.b_name='高等数学';
  • 2、查询member表中是否存在编号V00011的会员,如果存在,则查询book表中所有记录。
select * from  book
where exists
(select * from member where m_id='v00011');  //无v00011会员,返回数据为空。

//拓展:查询member表中是否存在编号V00001的会员,如果存在,则查询bookorder表中该会员记录。

select * from  bookorder
where exists
(select * from member where m_id='v00001');

运行后查询的是所有会员记录....

在子查询中添加bookorder.m_id=m_id试试 :

select * from  bookorder
where exists
(select * from member where bookorder.m_id=m_id and  m_id='v00001');

这里得出一个结论,只能在子查询引用父查询的表,父查询无法引用子查询的表。
  • 3、查询会员编号为“V00001”的会员购买的所有图书的编号、书名、价格。
select b_id as 图书编号,b_name as 会员姓名,b_price as 图书价格 from book
where b_id in [这里也可以使用/=any]
(select b_id from bookorder  where m_id ='v00001');
  • 4、查询book表中比所有“高等教育出版社”出版的图书库存量都低的图书详细信息。
select * from  book
where b_number <all
(select b_number from book
where b_publish ='高等教育出版社');
  • 5、在bookstore数据库中查找购买了《计算机基础》这本图书的会员信息。
select * from member //查找会员信息
where m_id = any
(select m_id as 会员编号 from bookorder //查询购买了计算机基础这本书的会员编号
where b_id =
(select b_id from book where b_name = '计算机基础')); //查找计算机基础这本书

三重查询

在bookstore数据库中完成以下题目:

  • 1、统计没有收货的订单一共有多少个,输出数量及其单号。
select count(distinct o_id) as 数量,group_concat( distinct o_id) as 订单号
from bookorder where o_sh='否';

// count 统计行数
// group_concat 汇总函数
  • 2、查找价格最高的图书编号、书名和价格。
select b_id as 图书编号,b_name as 书名 ,b_price as 价格 from book
where b_price=(select max(b_price) from book) ;

//或者用按价格降序(从高到低)排序完成
//limit 1  取第一行
//order by  字段 desc  降序排序

select b_id as 图书编号,b_name as 书名 ,b_price as 价格
from book order by b_price desc limit 0,1;
  • 3、按出版年份统计价格30元以上的图书有多少本,输出其中大于两本的。
select year(b_time) as 年份,count(*) as 数量
from book
where b_price>30
group by year(b_time)
having count(*)>2;

// group by 分组后取字段中年份
// having 分组后条件筛选

这里需要记住一个知识点,group by year(b_time) 分组后,
前面的select查询条件也可以跟上year(b_time)查询条件。
  • 4、使用子查询查找没有订购清华大学出版社出版的图书的会员编号和会员名。
select m_id as 会员编号,m_name as 会员姓名 from member
where not m_id = any
(select distinct m_id  from bookorder
where b_id in
(select b_id from book where b_publish = '清华大学出版社'));

注意: 表中可以看出有会员多次都买清华大学出版社出版的图书,distinct 是去除重复
筛选条件还可以用 not in / !=all
  • 5、查找所有张姓会员的姓名、性别,以及订购图书的编号、订购数量与订购时间。

  • 6、使用连接查询查找销售量最高的三本图书的书名、作者和价格。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: