テーブルの最後のN要素を取得するにはどうすればよいですか? このクエリを使用しましたが、動作していないようです。Sqliteのテーブルの最後の要素
Cursor c = database.rawQuery("select top 7 * from dailystats order by daily_ID desc;", null);
テーブルの最後のN要素を取得するにはどうすればよいですか? このクエリを使用しましたが、動作していないようです。Sqliteのテーブルの最後の要素
Cursor c = database.rawQuery("select top 7 * from dailystats order by daily_ID desc;", null);
OPからの要求に応じて、ポイントを獲得するために、私は答えとして私のコメントを作る;)
使用limit
、select * from dailystats order by daily_ID desc LIMIT 7
だけlimit
を使用します。
select * from dailystats order by daily_ID desc limit 7;
トップは、あなたがいる場合
select * from dailystats where daily_ID = (select max(daily_ID) from dailystats);
これらのクエリのどちらもトップ 'N'を返さない - トップ1のみ。 –
@MattBall、ああ、私の悪い、ちょうど最後の行を返すことを考えて、今すぐ編集しよう – Habib
を制限
select * from dailystats order by daily_ID desc limit 7;
を使用するか、単に最後の行を選択する必要があるSQL Serverのためのものですあなたはそれらすべての答えを変換することができます以下のようなより多くのAndroidのコードに:あなたのから
SQLiteDatabase database = dbHelper.getReadableDatabase();
String tableName = "dailystats";
String orderBy = "daily_ID desc";
String limit = String.valueOf(5);
Cursor cursor = database.query(tableName,
null, //all columns
null, // no where clause
null, // no where arguments
null, // no grouping
null, // no having
orderBy,
limit);
アンドロイドの他のcursor
としてcursor
を使用しています。 orderBy
列に関して、結果が上位limit
の列を降順で保持します。
使用 'limit'、'「daily_IDの降順LIMITによってdailystats順から選択*を7;」「 – neevek
ありがとうございました。私はそれを正しいと確認するために答えてください。 – callback