第三章 高级查询

1,查询没有借阅信息的读者编号和读者姓名

SELECT rid'读者编号',rname'读者姓名' FROM reader r WHERE NOT EXISTS(SELECT nif FROM borrow WHERE r.rid=borrow.rid);

2,查找未还书应还书但未还书的读者姓名,书名,应归还日期

SELECT (SELECT rName FROM reader WHERE reader.rid=borrow.rid)AS' 读者姓名',
(SELECT bName FROM book WHERE book.bid=borrow.nif)AS'图书名称',
borrow.willDate'应归还日期' FROM borrow WHERE borrow.returnDate>borrow.willDate

3,查询各种图书未借出的本数,显示书名和本数

SELECT bName'图书书名',bCount'现存数量' FROM book WHERE NOT EXISTS(
SELECT(SELECT bName FROM book)FROM borrow WHERE borrow.nif=book.bid)

4,从已完成借阅记录中统计借书次数和姓名

SELECT rName'读者姓名',lendNum'借书次数'FROM reader 
INNER JOIN borrow ON reader.rid=borrow.rid 
WHERE returnDate IS NOT NULL 
GROUP BY rName

5,查询罚款金额大于5元的读者姓名和罚款金额

SELECT(SELECT rName FROM reader WHERE reader.rid=penalty.rid)AS'读者姓名',amount'罚款金额' FROM penalty
GROUP BY rid
HAVING amount>5

6,统计已完成借阅记录中每本书的借阅次数,显示借阅次数排在前5位的书名和借阅次数

SELECT (SELECT bName FROM book WHERE book.bid=borrow.nif)AS'图书名称',COUNT(nif)AS'借出次数'FROM borrow 
WHERE returnDate IS NOT NULL 
GROUP BY nif
LIMIT 5

 

你可能感兴趣的:(第三章 高级查询)