sqlite根据特定条件获取行号

Get Row Number according to a specific condition by sqlite

我有 3 个表:

所以我需要一个 SQLite 查询来显示 s_id=7 的行号(这里必须是 3)

我在网上找了很多攻略都没有找到!如果有人能帮助我就完美了。

您可以像这样使用 window 函数:

select *
from (
    select s_id, row_number() over(order by s_id) as rn
    from section
) s
where s_id = 7

或者,如果您的 SQLite 版本不支持 window 函数,您可以使用子查询:

select s_id,
    (select count(*) from section s1 where s1.s_id <= s.id) as rn
from section s
where s_id = 7