用于查找签出最多的书籍的mysql查询

时间:2011-04-13 01:25:33

标签: mysql

BOOK_COPIES (Book_ISBN [pk,fk], Branch_ID [pk,fk], Num_Copies)
BOOK_LOANS (Book_ISBN [pk,fk], Branch_ID [pk,fk],Card_Num [pk,fk], Date_Out,Date_Due)                          
BORROWER (Card_Num [pk], Name, Address, Phone)

问题

  

列出已签出最多图书的借款人的卡号和姓名。

解决方案:

select br.cardnum,br.name from borrower br, bookloans bl
where br.card_num=bl.card_num 
group by br.cardnum,br.name
having count(bl.book_isbn)>=all(
    select count(*) from bookloans bl
    group by bl.card_num
);

请你帮我完成查询。

1 个答案:

答案 0 :(得分:0)

好像你在考虑你的家庭作业:

select BOOK_LOANS.Card_Num, Name, COUNT(Book_ISBN) AS BorrowedCount
from BOOK_LOANS
left join BORROWER on 
group by BOOK_LOANS.Card_Num
order by BorrowedCout desc
limit 1

不需要嵌套查询。