我有以下代碼: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 | ... +---------+-------------------------------------- . -+------------+------+
我希望第一個排名從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
和輸出
真正的新社區和一般的編程語言,對不起張貼時打破了共同規則。 – PMa
我用下面的代碼來獲取所有表格的源表格,它是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
請張貼它在你的問題不在評論 – peterm