当前位置:   article > 正文

mysql子查询sysdate_MySQL中的子查询(第八章)

查询图书信息表,显示图书类别不是数据库的信息

1 子查询的介绍

ca1c03621406a5893fc7fa01bd837914.png

d00ecfc85390525057117fa26eb1c3cf.png

5e4e3c7b6245c008511e090b08a65921.png

014bf4506873aac10bb8c99474305d85.png

-- 查找图书价格

SELECT price FROM bookinfo WHERE book_id = 20150301;

-- 更新读者信息的余额

UPDATE readerinfo SET balance = balance - 60 * 0.05 WHERE card_id = '210210199901015555';

这里初始信息是:身份证号为’ 210210199901015555’的用户的余额=500,借一本书的费用为图书价格*5%。

更新用户余额信息之后,再次查看用户的余额信息

结果:

![用户余额信息]](https://img-blog.csdnimg.cn/20200604143819113.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MDA1NjI3OQ==,size_16,color_FFFFFF,t_70)

恢复身份证号为’ 210210199901015555’的用户的余额=500,然后使用子查询的方式:

update readerinfo set balance = balance - (select price from bookinfo where book_id = 20150301) * 0.05 where card_id = '210210199901015555';

结果:

![用户余额信息]](https://img-blog.csdnimg.cn/20200604143819113.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MDA1NjI3OQ==,size_16,color_FFFFFF,t_70)

2 使用比较运算符的子查询

516c837447b6338b1a5bf04779936453.png

90c50045631c0b636a117d0b42b1be4c.png

-- 1.查询借阅信息表, 显示借 《 Spring源码深度解析》这本书的借阅记录

SELECT * FROM borrowinfo WHERE book_id = (SELECT book_id FROM bookinfo WHERE book_name = 'Spring源码深度解析');

结果:

c28947842fed24f2504e15bb57fa367f.png

-- 2.查询图书信息表, 显示图书价格小于图书平均价格的所有图书信息

SELECT * FROM bookinfo WHERE price < (SELECT ROUND(AVG(price),2) FROM bookinfo);

结果:

1d8f31f3c08fedc0c434be99e563d629.png

-- 3.查询图书信息表,显示图书类别不是’数据库’的所有图书信息

SELECT * FROM bookinfo WHERE book_category_id != (SELECT category_id FROM bookcategory WHERE category = '数据库');

结果:

4887e34f62ac4b7e920f327470da4f5d.png

bfdcb6a1437710d8a89a7c2e9d48e06f.png

2d841320cae2c214da49fe98fec58153.png

-- 查询图书信息表,显示图书类别为’计算机’的所有图书信息

SELECT * FROM bookinfo WHERE book_category_id = ANY(SELECT category_id FROM bookcategory WHERE parent_id = 1);

结果:

07f80d96d4699c05972a136cbbcebec9.png

-- 查询图书信息表,显示价格大于图书类别为4的任意一个价格的所有图书信息(大于子查询的最小值即可)

SELECT * FROM bookinfo WHERE price > ANY (SELECT price FROM bookinfo WHERE book_category_id = 4);

结果:

254615752491bf56a32ebd2e0675b197.png

d8888dc057c105c96b8986dac635c04d.png

-- 查询图书信息表,显示价格大于图书类别为4的所有价格的所有图书信息(大于子查询的最大值)

SELECT * FROM bookinfo WHERE price > ALL (SELECT price FROM bookinfo WHERE book_category_id = 4);

结果:

622aaff3d43f277358cba2e4820921da.png

3 使用[NOT] IN 或EXISTS的子查询

c44fa7eb4b52d44bc89e9c13e4dfc315.png

f0d5a61a05321741cbe7eef54b8e7dd0.png

-- 查询图书信息表,显示图书类别为’医学’的所有图书信息。

-- 方式1

SELECT * FROM bookinfo WHERE book_category_id = ANY (SELECT category_id FROM bookcategory WHERE parent_id = 2);

-- 方式2

-- in 后面的子查询返回一个数据列,等于数据列里的任意一个值都是满足条件的。

SELECT * FROM bookinfo WHERE book_category_id IN (SELECT category_id FROM bookcategory WHERE parent_id = 2);

结果:

171407e6c7ad9798416c4fd84df4bd25.png

781cbda584f26bab6b7e6b61e00b4e7e.png

c1b1e340134f97c0b9a91d89dc48212c.png

0bc3cd29561fa738db1b19b97bb747d2.png

-- 查看图书类别表中是否有’临床医学’的类别,如果有,则查看图书信息表。

SELECT * FROM bookinfo WHERE EXISTS (SELECT * FROM bookcategory WHERE category = '临床医学');

结果:

895ba108ed38421c73af3970f363a5c2.png

4 插入记录时使用子查询

313e9caefbc81fbf4e76e833c150e15b.png

7ecf4af36e3567ccd10e9d7f19850b19.png

CREATE TABLE readerfee(

book_id INT ,

card_id CHAR(18),

return_date DATE,

actual_return_date DATE,

book_fee DECIMAL(7,3),

PRIMARY KEY(book_id, card_id)

);

将borrowinfo中查询到的数据使用子查询的方式插入到表readerfee中:

INSERT INTO readerfee (book_id, card_id, return_date) SELECT book_id, card_id, return_date FROM borrowinfo WHERE DATEDIFF(SYSDATE(), return_date) > 0 AND STATUS = '否';

结果:

49fa01a36d157ce3d21f480bdfca31af.png

94d3b914f41c0f993530186cc3d5c00a.png

-- 1.更新借阅信息表,将借阅状态(status)更新为‘是’

UPDATE borrowinfo SET STATUS = '是' WHERE book_id = 20151101 AND card_id = '210210199901012222';

-- 2.更新罚款记录信息表,更新实际还书日期和罚款金额,罚款金额为每超出一天扣0.2元

UPDATE readerfee SET actual_return_date = SYSDATE(), book_fee = DATEDIFF(SYSDATE(), return_date) * 0.2 WHERE book_id = 20151101 AND card_id = '210210199901012222';

结果:查看罚款记录信息表:SELECT * FROM readerfee;

3d4e7247fe56835420cbb1a73bd23f4c.png

这里我们采用第2种方法:

UPDATE readerfee SET actual_return_date = DATE_FORMAT(SYSDATE(), '%Y-%m-%d'), book_fee = DATEDIFF(SYSDATE(), return_date) * 0.2 WHERE book_id = 20151101 AND card_id = '210210199901012222';

结果:

893b18d4b7ef20c974c10e8e35c55a98.png

d982168f4517514a817a466c002d9a3a.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/546012
推荐阅读
相关标签
  

闽ICP备14008679号