2013-12-08 38 views
1

我有以下代碼:MySQL的 - 嵌套等級從最小到最大的

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 < t1.page_count

其中t2.page_count < = t1.page_count

然後一些publ_id用2啓動和一些從1開始。

我該如何解決這個問題,所有排名都以1開頭?

我用這個下面的代碼來獲取所有形成的源表是a_bkinfo.books

select * from a_bkinfo.books 

和輸出

這裏充滿downloadable csv file

+1

真正的新社區和一般的編程語言,對不起張貼時打破了共同規則。 – PMa

+0

我用下面的代碼來獲取所有表格的源表格,它是a_bkinfo.books > select * from a_bkinfo.books and the output is here [link](https://www.dropbox.com/s /c3g0ybruvo9dq54/a_bkinfo.books.csv) 再次,我很抱歉,因爲我不知道如何直接嵌入到我的帖子StackOverflow這裏我的帖子,我無法找到答案:(:(。 – PMa

+0

請張貼它在你的問題不在評論 – peterm

回答

0

有一致的結果,你必須在子查詢中的WHERE子句中具有與在外部選擇中相同的條件。嘗試這種方式

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演示

+0

謝謝彼得!子克里的額外過濾器做到了! :) – PMa

+0

Omg,我很抱歉。我對此很新。剛接受。 – PMa