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

MySQL-从最小到最大的嵌套排名,mysql,sql,ranking,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 &

我有以下代码:

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
以及输出


这里是完整的

要获得一致的结果,子查询中WHERE子句的条件必须与外部select中的条件相同。这样试试看

选择publ\u id 标题 ,页数 , 选择1+COUNTpage_count-从1开始 从书本上 其中page\u count0-使用与外部选择中相同的条件 publ_id不为空-使用与外部选择中相同的条件 等级 摘自t 其中页面计数不为空 和页数>0 并且publ_id不为空 按公共id、等级排序
这是一个演示

对社区和编程语言来说都是全新的,很抱歉在发布时违反了常见规则。我使用以下代码从源表中获取了所有信息,源表是a_bkinfo.books>select*from a_bkinfo.books,输出再次出现在这里,很抱歉,我不知道如何将表格直接嵌入StackOverflow上的帖子,我找不到答案:。请在您的问题中发布,而不是在评论中。我在原始问题中添加了指向源表格的链接。抱歉搞混了。谢谢你的编辑,彼得,你到底是怎么做到的?我点击了SqlFriddle,但是你是如何生成代码并粘贴到这里的呢?对不起,如果这个问题听起来很愚蠢…谢谢彼得!子qry中的附加过滤器起到了作用!: +---------+-------------------------------------- . -+------------+------+ | 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 | ... +---------+-------------------------------------- . -+------------+------+
select * from a_bkinfo.books