SQLite Limit

Summary: in this tutorial, you will learn how to use SQLite LIMIT clause to constrain the number of rows returned by a query.

概要:このチュートリアルでは、SQLiteのLIMIT句を使って、クエリが返す行数を制限する方法を学びます

Introduction to SQLite LIMIT clause

The LIMIT clause is an optional part of the SELECT statement. You use the LIMIT clause to constrain the number of rows returned by the query.

For example, a SELECT statement may return one million rows. However, if you just need the first 10 rows in the result set, you can add the LIMIT clause to the SELECT statement to retrieve 10 rows.

The following illustrates the syntax of the LIMIT clause.

LIMIT句は、SELECT文のオプション部分です。LIMIT句は、クエリが返す行の数を制限するために使用します。例えば、SELECT文は100万行を返すかもしれません。しかし、結果セットの最初の10行だけが必要な場合は、SELECT文にLIMIT句を追加して10行を取り出すことができます。次の図は、LIMIT句の構文を示しています。
SELECT
	column_list
FROM
	table
LIMIT row_count;

The row_count is a positive integer that specifies the number of rows returned.

row_countは正の整数で、返される行の数を指定します。

For example, to get the first 10 rows in the tracks table, you use the following statement:

例えば、Tracksテーブルの最初の10行を取得するには、次のようなステートメントを使用します。
SELECT
	trackId,
	name
FROM
	tracks
LIMIT 10;

If you want to get the first 10 rows starting from the 10th row of the result set, you use OFFSET keyword as the following:

結果セットの10行目から最初の10行を取得したい場合は、以下のようにOFFSETキーワードを使用します。
SELECT
	column_list
FROM
	table
LIMIT row_count OFFSET offset;

Or you can use the following shorthand syntax of the LIMIT OFFSET clause:

SELECT
	column_list
FROM
	table
LIMIT offset, row_count;

For example, to get 10 rows starting from the 11th row in the tracks table, you use the following statement:

例えば、Tracksテーブルの11行目から10行を取得するには、次のようなステートメントを使用します。

You often find the uses of OFFSET in web applications for paginating result sets.

ウェブアプリケーションでは、結果セットをページングする際にOFFSETがよく使われます。

この記事が気に入ったらサポートをしてみませんか?