[英]How can I count the number of rows before a certain row in sqlite?
I have a database that stores the rank of an Item. 我有一个存储项目等级的数据库。 The rank is an absolute value that will be correct if all the items are taken into account. 如果考虑所有项目,则等级是绝对正确的绝对值。
If I only need a subset say four of this items it will give me something like: 如果我只需要其中一个子集,请说出其中四个选项,它将给我一些类似的信息:
Rank RowId in the whole Table
---------------
4 114
8 71
70 16
83 7
I now need an int specifying the rank only in the subset where the max rank is the number of items in the subset in my example 1,2,3,4. 现在,我需要一个int仅在子集中指定等级,其中最大等级是我的示例1,2,3,4中子集中的项目数。
Is there a way to achieve this in my sqlite query? 有没有办法在我的sqlite查询中实现这一点? I only need one of the ranks in my Activity. 我只需要“活动”中的一个等级。 I thought of ordering the results of the query by rank and then somehow get the position of item I want to rank at that moment. 我想到了按排名对查询结果进行排序,然后以某种方式获得当时我要排名的项目的位置。 But how would I achieve this with sqlite? 但是我如何用sqlite实现呢?
I tried to create a temporary table and insert the subset into it like this: 我试图创建一个临时表并将子集插入其中,如下所示:
CREATE TABLE rank(ID); CREATE TABLE rank(ID); INSERT INTO position SELECT ID from items WHERE ITEM_ID = 3 ORDER BY POSITION; 从ITEM_ID = 3的位置插入项目中的位置SELECT ID; SELECT RowID from rank WHERE ID = 9; 从等级WHERE ID = 9中选择SELECT RowID; DROP TABLE rank; DROP TABLE等级;
This is working in SQLite Manager and will return the correct number. 这在SQLite Manager中正常工作,将返回正确的数字。 But if I do this in Android in fails saying that there is no table rank while compiling query 但是,如果我在Android中执行此操作,则无法说编译查询时没有表排名
07-07 13:35:46.150: ERROR/AndroidRuntime(2047): Caused by: android.database.sqlite.SQLiteException: no such table: rank: , while compiling: SELECT RowID from rank WHERE ID = 9
EDIT: have to agree with @Matt the only way I've been able to do this is to use the temp table approach. 编辑:必须与@Matt达成一致,我能够做到这一点的唯一方法是使用临时表方法。
For what it's worth here's what it looks like... 对于它的价值,这就是它的样子……
create temp table if not exists <temptable>(Id integer primary key, rank);
insert into temptable(rank) select <column> from <table>;
select * from temptable;
EDIT: Actually that returns the ID associated with the row which isn't sequential so you won't always get 1,2,3,4... I'll have to think of something else. 编辑:实际上,它返回与不连续的行关联的ID,因此您将不会总是得到1,2,3,4 ...我将不得不考虑其他事情。 Sorry. 抱歉。
Not sure if I've understood your question. 不知道我是否理解您的问题。 You basically want this? 您基本上想要这个吗?
Id Value
---------------
1 4
2 8
3 70
4 83
So you want to add a pseudo-column as the id no matter what your subset contains? 因此,无论子集包含什么内容,您都想添加一个伪列作为id。
If that's correct then this should do it... 如果那是正确的,那么应该这样做...
SELECT RowId, <other columns>.... FROM <table> WHERE <where>
Apologies if I've misunderstood. 抱歉,如果我误会了。
您可以将查询(按等级排序)输出到具有自动增量ID的临时表中。
If you need to read only one row from a subquery you can always execute a limit on it, by providing the offset of how many records to be skipped first, and how much to be returned 如果您只需要从子查询中读取一行,则可以通过提供首先要跳过多少条记录以及要返回多少条记录的偏移量来始终对其执行限制
so if you want to get 25th row you tell to skip 24, and return 1 因此,如果您想获得第25行,请告诉您跳过24,然后返回1
select * from (SELECT * FROM table order by rank) limit 24,1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.