使用来自不同表的元素

时间:2017-03-27 15:48:04

标签: sql database oracle

我有这些表格:

Member(MID, Name, Surname, DateOfBirth)
Library(LID, Name, YearOfOpenning)
Book(ISBN, LID, Name)
Renting(RID, MID, ISBN, From, Until)

我需要找到从最古老的图书馆租来至少一本书的图书馆最老的成员。

找到最老的成员很容易,但是当我需要与最古老的图书馆结合时,问题就出现了。

select *
from member
where member.DATEOFBIRTH = (
select min (member.dateofbirth)
from member)

另外,我需要找出所有图书馆所在的图书馆,这些图书馆的帐户租金最多。 我试了3个小时这两个问题,但我几乎无处可去。

到目前为止,我设法找到了租房最多的人,但我不知道如何编写一个只显示图书馆的代码,根据我已经为租房最多的会员所做的条目(而不是把它分成几块。)

select renting.mid, count(renting.mid) as total
from renting
where renting.mid in (
select member.mid
from member
where renting.mid = member.mid)
group by mid
order by count(renting.mid) desc;
select * 
from library
where library.lid in (
select renting.lid
from renting
where renting.mid = 3);

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你应该得到最旧的一本出租书(会员必须加入租房)

  SELECT * FROM member 
  WHERE member.dateofbirth = 
  ( SELECT MIN(member.dateofbirth) 
      FROM member 
      INNER JOIN renting on member.mid = renting.mid) 

这也应该管理最早的图书馆

  SELECT * FROM member 
  WHERE member.dateofbirth = 
  ( SELECT MIN(member.dateofbirth) 
      FROM member 
      INNER JOIN renting on member.mid = renting.mid
      INNER JOIN Book on Book.ISBN = renting.ISBN
      INNER JOIN Library on Library.LID = Book.LID
      where rownum = 1
      ORDER BY Library.YearOfOpening )