2012-05-05 9 views
-1

テーブルの最後のN要素を取得するにはどうすればよいですか? このクエリを使用しましたが、動作していないようです。Sqliteのテーブルの最後の要素

Cursor c = database.rawQuery("select top 7 * from dailystats order by daily_ID desc;", null); 
+0

使用 'limit'、'「daily_IDの降順LIMITによってdailystats順から選択*を7;」「 – neevek

+0

ありがとうございました。私はそれを正しいと確認するために答えてください。 – callback

答えて

1

OPからの要求に応じて、ポイントを獲得するために、私は答えとして私のコメントを作る;)

使用limitselect * from dailystats order by daily_ID desc LIMIT 7

1

だけlimitを使用します。

select * from dailystats order by daily_ID desc limit 7; 
1

トップは、あなたがいる場合

select * from dailystats where daily_ID = (select max(daily_ID) from dailystats); 
+0

これらのクエリのどちらもトップ 'N'を返さない - トップ1のみ。 –

+0

@MattBall、ああ、私の悪い、ちょうど最後の行を返すことを考えて、今すぐ編集しよう – Habib

0

を制限

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の列を降順で保持します。

関連する問題