如何将子查询的结果与另一个表联接?

时间:2019-11-10 00:15:11

标签: sql oracle join subquery analytic-functions

。试图将借书人的结果与作者联系起来。

所需结果:

AUTHORID         AUTHORFIRSTNAME      AUTHORLASTNAME
1                     JIM                   SPARKS
2                     JAMES                 ALLEN
3                     MARCUS                RASHFORD
20                    PAUL                  POGBA
22                    THIERRY               HENRY

但不确定如何链接返回的顶级作者ID来检索authorfirstname和lastname,但我在子查询中未提及author表

2 个答案:

答案 0 :(得分:1)

您可以在子查询中按<textarea></textarea>的降序将这三个表与<form action="/question" method="post" id="myForm"> <label for="sessionName">Session Name</label><input type="text" name="sessionName"> <div class="question"> <label for="question">Question: </label><input type="textarea" name="question"> <ul> <li><label for="answer">Answer 1: </label><input type="text" name="answer1"><input type="checkbox" name="tick"></li> <li><label for="answer">Answer 2: </label><input type="text" name="answer2"><input type="checkbox" name="tick"></li> <li><label for="answer">Answer 3: </label><input type="text" name="answer3"><input type="checkbox" name="tick"></li> <li><label for="answer">Answer 4: </label><input type="text" name="answer4"><input type="checkbox" name="tick"></li> </ul> </div> <input type="submit" value="Create Session"> </form> <script> // Get all the checkboxes into a collection let checkboxes = document.querySelectorAll("input[type='checkbox']"); // Loop over the checkboxes the modern way checkboxes.forEach(function(cb, i) { // Use the click event since change only fires when the field gets a new value cb.addEventListener("click", () => { // Set the value of the checkbox to the value of the textbox that comes // right before it. cb.value = cb.previousSibling.value; console.log(cb.value); // Log the checkbox value }); }); </script>分析函数连接起来,然后在主查询中取小于等于5的值:

rank()

如果您使用的是数据库版本12c +,获取会更容易:

count

如果有的话,我使用SELECT authorid, authorfirstname, authorlastname FROM ( SELECT a.authorid, a.authorfirstname, a.authorlastname, rank() over (order by count(*) desc) as rnk FROM AUTHOR a LEFT JOIN BOOK bk ON a.authorid = bk.authorid LEFT JOIN BORROWER br ON br.bookid = bk.bookid WHERE br.borrowdate between date'2017-01-01' and date'2017-12-31' GROUP BY a.authorid, a.authorfirstname, a.authorlastname ) WHERE rnk <= 5 ORDER BY rnk 而不是SELECT a.authorid, a.authorfirstname, a.authorlastname, rank() over (order by count(*) desc) as rnk FROM AUTHOR a LEFT JOIN BOOK bk ON a.authorid = bk.authorid LEFT JOIN BORROWER br ON br.bookid = bk.bookid WHERE br.borrowdate between date'2017-01-01' and date'2017-12-31' GROUP BY a.authorid, a.authorfirstname, a.authorlastname ORDER BY rnk FETCH FIRST 5 ROWS WITH TIES 来使列br.borrowdate between date'2017-01-01' and date'2017-12-31'上的索引受益。

上面的那些查询返回带有联系的行,例如如果多行与第5行的值匹配,则它们带来的行数要多于5行。

请勿出于排名目的使用to_char(br.borrowdate) like '%2017'伪列,因为其值是在订购前计算的,可能会产生错误的结果。

答案 1 :(得分:0)

所以,如果我正确理解它,您想做这样的事情:

select authorid, authorfirstname, authorlastname 
 from 
  (select a.authorid, a.authorfirstname, a.authorlastname 
   from author a, borrower b, book c 
   where a.authorid = c.authorid and c.bookid = b.bookid
   and b.borrowdate like '%2017' 
    group by c.bookauthor 
    order by count(*) desc) xx 
 where rownum <=5