SQLite 版本 < 3.25.0 中 Window 函数的替代方案?
Alternative for Window function in SQLite version < 3.25.0?
我想检索 table 中每个 chat_id 匹配项的最后 10 行。
这很完美:
SELECT *
FROM
(SELECT a.*,
row_number()
OVER (PARTITION BY a.chat_id
ORDER BY a.timestamp DESC ) AS row
FROM messages a ) AS foo
WHERE row <= 10
但是当我将此代码放在我的 React Native 应用程序上时,它会抛出错误,这是因为它仅在 SQLite 3.25.0 及更高版本中受支持。
https://developer.android.com/reference/android/database/sqlite/package-summary.html
还有其他方法吗?我试图避免对 SQL.
进行多次查询
一种方案是使用相关子查询统计有多少条记录与当前行相同chat_id
且大于timestamp
,并使用结果进行过滤:
select m.*
from messages m
where (
select count(*)
from messages m1
where m1.chat_id = m.chat_id and m1.timestamp > m.timestamp
) < 10
我想检索 table 中每个 chat_id 匹配项的最后 10 行。
这很完美:
SELECT *
FROM
(SELECT a.*,
row_number()
OVER (PARTITION BY a.chat_id
ORDER BY a.timestamp DESC ) AS row
FROM messages a ) AS foo
WHERE row <= 10
但是当我将此代码放在我的 React Native 应用程序上时,它会抛出错误,这是因为它仅在 SQLite 3.25.0 及更高版本中受支持。
https://developer.android.com/reference/android/database/sqlite/package-summary.html
还有其他方法吗?我试图避免对 SQL.
进行多次查询一种方案是使用相关子查询统计有多少条记录与当前行相同chat_id
且大于timestamp
,并使用结果进行过滤:
select m.*
from messages m
where (
select count(*)
from messages m1
where m1.chat_id = m.chat_id and m1.timestamp > m.timestamp
) < 10