【1】mysql子查询
子查询的引入
-- 身份证号为`52242855555`的用户,今天借了一本图书编号为`4454545`的图书。完成下列需求
-- 更新读者信息表得到余额
-- 先将这个数据插入图书信息表 更新以下他的价格
insert into bookinfo(book_id) values('4454545');
update bookinfo set price = price+23.33 where book\_id=4454545;
-- 查看这条数据
select price from bookinfo where book_id =4454545;
-- 插入身份信息到读者信息表
insert into readerinfo(card_id) values('52242855555');
-- 由于该读者借了书 就要给钱 所以扣钱 更新他的钱 通过查询的方式获取数据 即子查询
update readerinfo set balance = balance- 0.05*(select price from bookinfo where book_id =4454545)
where card\_id=52242855555;
子查询:(select column_name from tab_name where column_name = value)
【2】mysql比较运算符子查询
先插入数据,在演示
-- 向bookparent 插入数据
insert into bookparent(book_name,parent_id)values('眼科学',2),('临床医学',2);
-- 向bookinfo 插入数据
insert into bookinfo(book_id,book_copy_id,book_name,author,price,store,press,pubdate)
values
(20151101,6,'临床诊断学','小明,小红等',115,10,'人民出版社','2015-06-01'),
(20151102,6,'临床诊断学1','小明1,小红1等',115.5,10,'人民出版社1','2015-07-01');
-- 将readerinfo 的52242855555的余额改为500
update readerinfo set balance = 500 where card_id ='52242855555';
-- 向 borrowinfo 插入一条数据
insert into borrowinfo(book_id,card_id,borrow_date,return_date,status)
values(20151101,'52242855555','2017-10-10','2017-11-10','否');
开始演示子查询,设计多个表之间的联系
-- 查询借阅信息表,显示借《C++》这本书的借阅记录
-- 通过书名 获取book_id 通过book_id查看信息
select *from borrowinfo where book_id =(select book_id from bookinfo where book_name ='C++');
-- 查询图书信息表,显示图书价格小于图书平均价格的图书信息
-- 先求平均价格 并保留两位小数
select *from bookinfo where price<(select round(AVG(price),2) from bookinfo);
-- 查询图书信息表,显示图书类别不是'数据库'的所有图书信息
-- 在bookparen获取'数据库'的类别ID即可
select *from bookinfo where book_copy_id <>(select book_id from bookparent where book_name ='数据库');
【1.1】any\all\some关键字修饰子查询
-- 查询图书信息表 bookparent 显示图书类别为'计算机'的所有图书信息
select *from bookinfo where book_copy_id = ANY(select book_id from bookparent where parent_id = 1);
-- 查询图书信息表 bookparent 显示图书类别为'计算机'的所有图书信息
select *from bookinfo where book_copy_id = some(select book_id from bookparent where parent_id = 1);
【3】mysql [NOT]IN或exists的子查询
-- 查询图书信息表 ,显示图书类别为'医学'的所有图书
select *from bookinfo where book_copy_id = any(select book_id from bookparent where parent_id = 2);
异曲同工之妙 相当于 =等于in 仅在这里具有可比性
-- 查询图书信息表 ,显示图书类别为'医学'的所有图书
select *from bookinfo where book_copy_id in (select book_id from bookparent where parent_id = 2);
-- 查询图书信息表 ,显示图书类别不是为'医学'的所有图书
select *from bookinfo where book_copy_id not in (select book_id from bookparent where parent_id = 2);
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新