• <legend id='qsLDs'><style id='qsLDs'><dir id='qsLDs'><q id='qsLDs'></q></dir></style></legend>

      <small id='qsLDs'></small><noframes id='qsLDs'>

      <i id='qsLDs'><tr id='qsLDs'><dt id='qsLDs'><q id='qsLDs'><span id='qsLDs'><b id='qsLDs'><form id='qsLDs'><ins id='qsLDs'></ins><ul id='qsLDs'></ul><sub id='qsLDs'></sub></form><legend id='qsLDs'></legend><bdo id='qsLDs'><pre id='qsLDs'><center id='qsLDs'></center></pre></bdo></b><th id='qsLDs'></th></span></q></dt></tr></i><div id='qsLDs'><tfoot id='qsLDs'></tfoot><dl id='qsLDs'><fieldset id='qsLDs'></fieldset></dl></div>

      1. <tfoot id='qsLDs'></tfoot>

        • <bdo id='qsLDs'></bdo><ul id='qsLDs'></ul>

        如何选择每组的前N行?

        How to select the first N rows of each group?(如何选择每组的前N行?)

          • <bdo id='hgioS'></bdo><ul id='hgioS'></ul>

              <tbody id='hgioS'></tbody>

            <small id='hgioS'></small><noframes id='hgioS'>

          • <legend id='hgioS'><style id='hgioS'><dir id='hgioS'><q id='hgioS'></q></dir></style></legend>

              <tfoot id='hgioS'></tfoot>
                <i id='hgioS'><tr id='hgioS'><dt id='hgioS'><q id='hgioS'><span id='hgioS'><b id='hgioS'><form id='hgioS'><ins id='hgioS'></ins><ul id='hgioS'></ul><sub id='hgioS'></sub></form><legend id='hgioS'></legend><bdo id='hgioS'><pre id='hgioS'><center id='hgioS'></center></pre></bdo></b><th id='hgioS'></th></span></q></dt></tr></i><div id='hgioS'><tfoot id='hgioS'></tfoot><dl id='hgioS'><fieldset id='hgioS'></fieldset></dl></div>
                  本文介绍了如何选择每组的前N行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个这样的 SQLite 表:

                  I have two SQLite tables like this:

                   AuthorId | AuthorName
                  ----------------------
                   1        | Alice
                   2        | Bob
                   3        | Carol
                   ...      | ....
                  
                  
                   BookId | AuthorId | Title
                  ----------------------------------
                   1      | 1        | aaa1
                   2      | 1        | aaa2
                   3      | 1        | aaa3
                   4      | 2        | ddd1
                   5      | 2        | ddd2
                   ...    | ...      | ...
                   19     | 3        | fff1
                   20     | 3        | fff2
                   21     | 3        | fff3
                   22     | 3        | fff4
                  

                  我想做一个 SELECT 查询,它将返回每个 AuthorId 的前 N(例如两)行,按标题排序(选择每个作者的前两本书").

                  I want to make a SELECT query that will return the first N (e.g. two) rows for each AuthorId, ordering by Title ("Select the first two books of each author").

                  示例输出:

                   BookId |  AuthorId | AuthorName | Title
                  ------------------------------------------
                   1      |  1        |   Alice    | aaa1
                   2      |  1        |   Alice    | aaa1
                   4      |  2        |   Bob      | ddd1
                   5      |  2        |   Bob      | ddd2
                   19     |  3        |   Carol    | fff1
                   20     |  3        |   Carol    | fff2
                  

                  如何构建此查询?

                  (是的,我找到了一个类似的主题,我知道如何只返回一行(第一行或顶部).问题在于两者).

                  (Yes, I found a similar topic, and I know how to return only one row (first or top). The problem is with the two).

                  推荐答案

                  您可以使用相关子查询进行计数:

                  You can do the counting using a correlated subquery:

                  SELECT b.BookId, a.AuthorId, a.AuthorName, b.Title
                  FROM Author a join
                       Book b
                       on a.AuthorId = b.AuthorId
                  where (select count(*)
                         from book b2
                         where b2.bookId <= b.BookId and b2.AuthorId = b.AuthorId
                        ) <= 2;
                  

                  对于小型数据库,这应该没问题.如果您在 Book(AuthorId, BookId) 上创建复合索引,那么这将有助于查询.

                  For a small database this should be fine. If you create a composite index on Book(AuthorId, BookId) then that will help the query.

                  这篇关于如何选择每组的前N行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                  <tfoot id='ZPoU4'></tfoot>

                  <legend id='ZPoU4'><style id='ZPoU4'><dir id='ZPoU4'><q id='ZPoU4'></q></dir></style></legend>

                      <tbody id='ZPoU4'></tbody>

                        <bdo id='ZPoU4'></bdo><ul id='ZPoU4'></ul>

                          <small id='ZPoU4'></small><noframes id='ZPoU4'>

                            <i id='ZPoU4'><tr id='ZPoU4'><dt id='ZPoU4'><q id='ZPoU4'><span id='ZPoU4'><b id='ZPoU4'><form id='ZPoU4'><ins id='ZPoU4'></ins><ul id='ZPoU4'></ul><sub id='ZPoU4'></sub></form><legend id='ZPoU4'></legend><bdo id='ZPoU4'><pre id='ZPoU4'><center id='ZPoU4'></center></pre></bdo></b><th id='ZPoU4'></th></span></q></dt></tr></i><div id='ZPoU4'><tfoot id='ZPoU4'></tfoot><dl id='ZPoU4'><fieldset id='ZPoU4'></fieldset></dl></div>