赞
踩
-- 查找图书价格
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)
-- 1.查询借阅信息表, 显示借 《 Spring源码深度解析》这本书的借阅记录
SELECT * FROM borrowinfo WHERE book_id = (SELECT book_id FROM bookinfo WHERE book_name = 'Spring源码深度解析');
结果:
-- 2.查询图书信息表, 显示图书价格小于图书平均价格的所有图书信息
SELECT * FROM bookinfo WHERE price < (SELECT ROUND(AVG(price),2) FROM bookinfo);
结果:
-- 3.查询图书信息表,显示图书类别不是’数据库’的所有图书信息
SELECT * FROM bookinfo WHERE book_category_id != (SELECT category_id FROM bookcategory WHERE category = '数据库');
结果:
-- 查询图书信息表,显示图书类别为’计算机’的所有图书信息
SELECT * FROM bookinfo WHERE book_category_id = ANY(SELECT category_id FROM bookcategory WHERE parent_id = 1);
结果:
-- 查询图书信息表,显示价格大于图书类别为4的任意一个价格的所有图书信息(大于子查询的最小值即可)
SELECT * FROM bookinfo WHERE price > ANY (SELECT price FROM bookinfo WHERE book_category_id = 4);
结果:
-- 查询图书信息表,显示价格大于图书类别为4的所有价格的所有图书信息(大于子查询的最大值)
SELECT * FROM bookinfo WHERE price > ALL (SELECT price FROM bookinfo WHERE book_category_id = 4);
结果:
-- 查询图书信息表,显示图书类别为’医学’的所有图书信息。
-- 方式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);
结果:
-- 查看图书类别表中是否有’临床医学’的类别,如果有,则查看图书信息表。
SELECT * FROM bookinfo WHERE EXISTS (SELECT * FROM bookcategory WHERE category = '临床医学');
结果:
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 = '否';
结果:
-- 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;
这里我们采用第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';
结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。