Sql 如何根据不共享字段的表筛选查询?

Sql 如何根据不共享字段的表筛选查询?,sql,oracle-sqldeveloper,oracle-xe-18.4,Sql,Oracle Sqldeveloper,Oracle Xe 18.4,好的,所以我已经研究这个SQL语句有一段时间了,只是不能理解它。我需要能够看到客户在2017年借来的前5位作者。我的桌子看起来像这样 1. Client - fields (clientId,clientFirstName,clientlastName,clidentDoB) 2.author - fields (AuthorID, AuthorFirstname, AuthorLastname,AuthorNation ) 3. book - fields (BookId, BookAutho

好的,所以我已经研究这个SQL语句有一段时间了,只是不能理解它。我需要能够看到客户在2017年借来的前5位作者。我的桌子看起来像这样

1. Client - fields (clientId,clientFirstName,clientlastName,clidentDoB)
2.author - fields (AuthorID, AuthorFirstname, AuthorLastname,AuthorNation )
3. book - fields (BookId, BookAuthor,BookTitle,BookGenre)
4. borrower - fields (borrowID,BorrowDate, ClientID,BookId)
因此,我知道我需要从author表中提取名称,根据借阅的图书数量,我还知道借款人.bookId等于book.bookId,而author.authord等于book.BookAuthor。 我应该能够设置它,这样它可以看到2017年借来的书籍,然后根据最流行的书籍进行过滤,方法是获取借来的bookID并将同一Id的每个实例添加到一起,查看bookID与book表中BookAuthor的匹配情况,然后使用该Id比较Id以打印出名字和姓氏

我试过了

SELECT author.authorfirstname,author.authorlastname 
FROM author Join ON author.authorid = book.bookauthor   
WHERE (borrower.borrowdate <='31/12/2017' AND borrower.borrowdate >= '01/01/2017'); 
书桌

(1,1,bobsbook,fantasy) 
(2,1,bobagain,fantasy) 
(3,1,returnofbob,fantasy)
 (4,2,fredsadventure,fantasy) 
(5, 2, fedagain, fantasy) 
(6, 2, fedstrikes, fantasy) 
(7,3,alexjoes, fantasy)
 (8, 3, alexjoeagain,fantasy)
(9,4, dansbook, fantasy)
借书台

(1, 20/01/2017,,1, 1)
(2, 20/01/2017,,3, 2)
(3, 20/01/2017,,2, 1)
(4, 20/01/2017,,1, 3)
(5, 20/01/2017,,6, 2)
(6, 20/01/2017,,8, 4)
(7, 20/01/2017,,4, 4)
(8, 20/01/2017,,9, 6)
(9, 20/01/2017,,2, 7)
(10, 20/01/2017,,3, 9)
(11, 20/01/2017,,4, 9)
最终结果将是

AuthorFirstName  AuthorLastname 
bob               ross
Fred              Martin
Dan               Reed 
这是因为在2017年的日期范围内,他们拥有最多的借款,依次是bob 5岁、fred 3岁和dan 2岁。它也只打印前三名,因此alex joe不在列表中

由@fahmi提供的代码

SELECT author.authorfirstname,author.authorlastname 
FROM author 
Join book ON author.authorid = book.bookauthor
join borrower on book.bookid=borrower.bookid
WHERE borrower.borrowdate <='31/12/2017' AND borrower.borrowdate >= '01/01/2017'
选择author.authorfirstname,author.authorlastname
来自作者
在author.authord=book.bookauthor上加入book
在book.bookid=借款人.bookid上加入借款人
其中借款人借款日期='2017年1月1日'
)


这给了我一个作者列表,它列出了burrow的每个实例,但我需要合并该列表,以便我只看到其中一个名称,这样大多数借阅者都会将其按顺序排列,并对其自身进行限制。

您需要使用
book.bookid=booker.bookid
关系再次加入到借阅者

SELECT top 3 author.authorfirstname,author.authorlastname,count(borrower.bookid) as cnt
FROM author 
Join book ON author.authorid = book.bookauthor
join borrower on book.bookid=borrower.bookid
WHERE borrower.borrowdate <='31/12/2017' AND borrower.borrowdate >= '01/01/2017'
group by author.authorfirstname,author.authorlastname
order by cnt desc
选择前三名author.authorfirstname、author.authorlastname、count(借款人.bookid)作为cnt
来自作者
在author.authord=book.bookauthor上加入book
在book.bookid=借款人.bookid上加入借款人
其中借款人借款日期='2017年1月1日'
按作者分组。authorfirstname,author.authorlastname
按cnt desc订购
使用Oracle的查询功能

select au.authorfirstname
      ,au.authorlastname 
  from author au
  join book bk
    on au.authorid = bk.bookauthor
  join borrower bw
    on bk.bookid=bw.bookid
 where extract(year from bw.borrowdate) 
       = 2017
group by au.authorfirstname
        ,au.authorlastname
order by count(bk.bookid) desc
fetch first 3 rows with ties;

这很有帮助,我没有想过像那样使用第二个连接。所以这给了我一个名单,我需要把他们结合起来,按作者的最高数量排序。我试着按author.authorfirstname、author.authorlastname按计数顺序(author.authorlastname)添加GROUB,这样我就可以知道前5位作者了?@comraderago,你能在问题中添加示例数据和你想要的结果吗。这将有助于人们理解您的要求。我已为原始问题添加了一些示例代码和预期输出。@同志,我已更新了我的答案-请用版本对数据库进行查找标记。您尝试并提供的代码不完整,您可能需要查看一下。提供一些示例数据和预期输出,这将有助于更快地扩展帮助。
select au.authorfirstname
      ,au.authorlastname 
  from author au
  join book bk
    on au.authorid = bk.bookauthor
  join borrower bw
    on bk.bookid=bw.bookid
 where extract(year from bw.borrowdate) 
       = 2017
group by au.authorfirstname
        ,au.authorlastname
order by count(bk.bookid) desc
fetch first 3 rows with ties;