4

我有表视图,我想在其中选择查询中添加的最后 10 条记录。我正在使用以下查询,它返回所有记录,但我想添加最后 10 个。

这是查询

NSString* select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster where ContentAddedByUserID='%@' AND HiveletCode='%@'",appDelegate.userID,appDelegate.organizationCode];

我已经搜索了一些我们可以通过这样的顶级记录获得的地方,我知道我们可以使用两个日期之间的日期来获取任何想法如何在没有日期的情况下获取最后 10 条记录。

我想获取最后 10 个添加的所有数据。

4

2 回答 2

8

表中的每一行都有一个唯一的 ID,您可以将其用于查询:

SELECT
    *
FROM
    ContentMaster
WHERE
    ContentAddedByUserID='%@'
AND
    HiveletCode='%@'
ORDER BY
    rowid DESC
LIMIT 10
于 2013-11-15T10:55:03.120 回答
2

According to documentation, each row in the Sqlite contains a 64 Bit auto incremented id associated with it.
So you can use query as

SELECT * FROM ContentMaster WHERE ContentAddedByUserID='%@' AND HiveletCode='%@'
ORDER BY ROWID DESC
LIMIT 10

Remember,

  1. If you declare an ordinary table column to use one of the defined special names ROWID, _ROWID_, or OID, then the use of that name will refer to the declared column not to the internal ROWID.
  2. If a table contains a column of type INTEGER PRIMARY KEY, then that column becomes an alias for the ROWID.
于 2013-11-15T11:01:40.433 回答