SQLite Where

Summary: in this tutorial, you will learn how to use SQLite WHERE clause to specify the search condition for rows returned by the query.

概要:このチュートリアルでは、SQLiteのWHERE句を使って、クエリで返される行の検索条件を指定する方法を学びます。

Introduction to SQLite WHERE clause


The WHERE clause is an optional clause of the SELECT statement. It appears after the FROM clause as the following statement:

SQLiteのWHERE句の紹介
WHERE句は、SELECT文のオプション句です。FROM句の後に次のような文で表示されます。
SELECT
	column_list
FROM
	table
WHERE
	search_condition;

In this example, you add a WHERE clause to the SELECT statement to filter rows returned by the query. When evaluating a SELECT statement with a WHERE clause, SQLite uses the following steps:

この例では、SELECT ステートメントに WHERE 句を追加して、クエリが返す行をフィルタリングします。WHERE句を含むSELECT文を評価する際、SQLiteは以下のステップを使用します。

First, check the table in the FROM clause.
Second, evaluate the conditions in the WHERE clause to get the rows that met these conditions.
Third, make the final result set based on the rows in the previous step with columns in the SELECT clause.

まず、FROM句でテーブルを確認します。
第2に、WHERE句の条件を評価し、これらの条件を満たした行を取得します。
第3に、前のステップの行とSELECT句の列に基づいて、最終的な結果セットを作ります。


The search condition in the WHERE has the following form:

WHERE句の検索条件は次のような形になります。
left_expression COMPARISON_OPERATOR right_expression

For example, you can form a search condition as follows:

たとえば、次のように検索条件を形成することができます
WHERE column_1 = 100;
WHERE column_2 IN (1,2,3);
WHERE column_3 LIKE 'An%';
WHERE column_4 BETWEEN 10 AND 20;

Besides the SELECT statement, you can use the WHERE clause in the UPDATE and DELETE statements.

SELECT文のほかに、UPDATE文やDELETE文でもWHERE句を使うことができます。

SQLite comparison operators


A comparison operator tests if two expressions are the same. The following table illustrates the comparison operators that you can use to construct expressions:

SQLiteの比較演算子
比較演算子は、2つの式が同じであるかどうかをテストします。次の表は、式の構築に使用できる比較演算子を示しています。
Operator Meaning 
=                                Equal to
<> or !=                     Not equal to
<                                 Less than
>                                Greater than
<=                              Less than or equal to
>=                              Greater than or equal to
演算子の意味 
= 等しい
<> または != 等しくない
< より小さい
> より大きい
<= より小さいか等しい
>= より大きいか等しい


SQLite logical operators


Logical operators allow you to test the truth of some expressions. A logical operator returns 1, 0, or a NULL value.

Notice that SQLite does not provide Boolean data type therefore 1 means TRUE, and 0 means FALSE.

The following table illustrates the SQLite logical operators:

SQLiteの論理演算子
論理演算子を使うと、ある式の真偽を調べることができます。論理演算子は 1、0、または NULL 値を返します。
SQLiteはブールデータ型を提供していないので、1はTRUEを意味し、0はFALSEを意味することに注意してください。
次の表は、SQLiteの論理演算子を示しています。
Operator                                Meaning
ALL                  returns 1 if all expressions are 1.
AND                returns 1 if both expressions are 1, and 0 if one of the expressions is 0.
ANY                      returns 1 if any one of a set of comparisons is 1.
BETWEEN           returns 1 if a value is within a range.
EXISTS              returns 1 if a subquery contains any rows.
IN                     returns 1 if a value is in a list of values.
LIKE                   returns 1 if a value matches a pattern
NOT               reverses the value of other operators such as NOT EXISTS, NOT IN, NOT BETWEEN, etc.
OR                  returns true if either expression is 1
演算子の意味
ALL は、すべての式が 1 であれば 1 を返します。
ANDは、両方の式が1の場合は1を、一方の式が0の場合は0を返します。
ANYは、一連の比較対象のうちどれか1つが1であれば1を返します。
BETWEENは、ある値が範囲内にある場合に1を返します。
EXISTSは、副問い合わせに行が含まれていれば1を返します。
INは、値が値のリストにある場合、1を返します。
LIKEは、値がパターンに一致する場合に1を返します。
NOTは、NOT EXISTS、NOT IN、NOT BETWEENなどの他の演算子の値を反転させます。
ORは、どちらかの式が1の場合に真を返します


SQLite WHERE clause examples


We will use the tracks table in the sample database to illustrate how to use the WHERE clause.

SQLite の WHERE 句の例
サンプルデータベースのtrackテーブルを使って、WHERE句の使い方を説明します。

The equality operator (=) is the most commonly used operator. For example, the following query uses the WHERE clause the equality operator to find all the tracks in the album id 1:

等号演算子(=)は最もよく使われる演算子です。たとえば、次のクエリは、WHERE句に等号演算子を使用して、アルバムID 1に含まれるすべてのトラックを検索します
SELECT
  name,
  milliseconds,
  bytes,
  albumid
FROM
  tracks
WHERE
  albumid = 1;


SQLite compares the values stored in the AlbumId column with a literal value 1 to test if they are equal. Only the rows that satisfy the condition are returned.

SQLiteは、AlbumIdカラムに格納された値とリテラル値1を比較し、それらが等しいかどうかをテストします。条件を満たす行のみが返されます。

When you compare two values, you must ensure that they are the same data type. You should compare numbers with numbers, string with strings, etc.

2つの値を比較する際には、それらが同じデータ型であることを確認する必要があります。数字と数字、文字列と文字列などを比較する必要があります。

In case you compare values in different data types e.g., a string with a number, SQLite has to perform implicit data type conversions, but in general, you should avoid doing this.

文字列と数値のように異なるデータ型の値を比較する場合、SQLiteは暗黙のデータ型変換を行わなければなりませんが、一般的にはこのようなことは避けるべきでしょう。

You use the logical operator to combine expressions. For example, to get tracks of the album 1 that have the length greater than 200,000 milliseconds,

論理演算子を使って式を組み合わせることができます。例えば、アルバム「1」のトラックのうち、長さが200,000ミリ秒以上のものを取得するには、

you use the following statement:

次のような文を使用します。
SELECT
	name,
	milliseconds,
	bytes,
	albumid
FROM
	tracks
WHERE
	albumid = 1
AND milliseconds > 250000;

The statement used two expressions albumid = 1 and milliseconds > 250000. It uses the AND logical operator to combine these expressions.

このステートメントでは、albumid = 1 と milliseconds > 250000 という 2 つの式を使用しています。これらの式を結合するために、AND論理演算子を使用しています。


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