MySQL - 嵌套排名从最小到最大

时间:2013-12-08 03:35:11

标签: mysql sql ranking

我有以下代码:

select publ_id
, title
, t1.page_count
, ( select count(page_count)
from a_bkinfo.books as t2
where t2.page_count < t1.page_count
and t2.publ_id = t1.publ_id) as Rank
from a_bkinfo.books as t1|
where page_count is not null
and page_count <> 0
and publ_id is not null
order by publ_id, rank

我得到以下结果:

+---------+-------------------------------------- . -+------------+------+
| publ_id | title                                 .  | page_count | Rank |
+---------+-------------------------------------- . -+------------+------+
|    9000 | Practical Standards for VB.NET        .  |        250 |    1 |
|    9000 | Programming SQL Server with VB.NET    .  |        300 |    2 |
|    9000 | T_SQL Programming (Inside series)     .  |        390 |    3 |
|    9000 | T_SQL Querying (Inside series)        .  |        391 |    4 |
|    9000 | .Net Development for Microsoft Office .  |        500 |    5 |
|    9000 | Applied .NET Framework Programming VB .  |        608 |    6 |
|    9000 | Programming Visual Basic 2005: The La .  |        980 |    7 |
|    9020 | Bird Sense                            .  |        265 |    0 |
|    9020 | The Unfeathered Bird                  .  |        304 |    1 |
|    9021 | Outstanding Mosses and Liverworts of  .  |          9 |    0 |
|    9021 | Winter Weed Finder: A Guide to Dry Pl .  |         64 |    1 |
|    9021 | The Great Agnostic: Robert Ingersoll  .  |        256 |    2 |
|    9021 | Bark: A Field Guide to Trees of the N .  |        280 |    3 |
|    9021 | Hornworts and Liverworts in your Gard .  |        501 |    4 |
|    9021 | Lichens of North America              .  |        828 |    5 |
|    9021 | Outstanding Bryophytes                .  |        956 |    6 |
|    9022 | The Leafcutter Ants: Civilization by  .  |        160 |    0 |
|    9022 | The Social Conquest of Earth          .  |        352 |    1 |
|    9022 | The Ants                              .  |        732 |    2 |
...                                               
+---------+-------------------------------------- . -+------------+------+

这是完整的downloadable csv file

我希望第一个排名从1开始,但有些从1开始,有些从0开始。如果我添加更改

  

其中t2.page_count&lt; t1.page_count

  

其中t2.page_count&lt; = t1.page_count

然后一些publ_id以2开头,一些以1开头。

如何通过让所有排名从1开始来解决这个问题?

我使用以下代码来获取所有形式的源表,即a_bkinfo.books

select * from a_bkinfo.books

和输出

这是完整的downloadable csv file

1 个答案:

答案 0 :(得分:0)

要获得一致的结果,您必须在子查询中的WHERE子句中具有与外部select中相同的条件。试试这种方式

SELECT publ_id
      ,title
      ,page_count
      ,(
         SELECT 1 + COUNT(page_count)   -- start with 1
           FROM books
          WHERE page_count < t.page_count
            AND publ_id = t.publ_id
            AND page_count IS NOT NULL  -- use the same conditions as in the outer select 
            AND page_count > 0          -- use the same conditions as in the outer select
            AND publ_id IS NOT NULL     -- use the same conditions as in the outer select
       ) rank 
  FROM books t
 WHERE page_count IS NOT NULL
   AND page_count > 0
   AND publ_id IS NOT NULL
 ORDER BY publ_id, rank

这是 SQLFiddle 演示