sql—如何在sqlite3中获取行索引?

xwbd5t1u  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(418)

我创建了一个表,其中有一列注册号作为主键。我已使用 row_number() 按主键排序。
如何搜索注册号并获取该行的行id以及该行的其他信息?

m1m5dgzv

m1m5dgzv1#

如果已创建列 row_id 比如:

ROW_NUMBER() OVER (ORDER BY registration_number)

然后使用 CTE :

WITH cte AS (
  SELECT *, ROW_NUMBER() OVER (ORDER BY registration_number) row_id
  FROM tablename
)
SELECT * 
FROM cte
WHERE registration_number = ?

替换 ? 您要搜索的注册号。
另一种获得 row_id 将与相关子查询:

SELECT t.*,
  (SELECT COUNT(*) FROM tablename WHERE registration_number <= t.registration_number) AS row_id 
FROM tablename AS t
WHERE t.registration_number = ?

相关问题