Mysqli-where-clause
MySQLi-WHERE句
MySQLiテーブルからデータを取得するSQL SELECT コマンドを見てきました。 WHERE 句と呼ばれる条件句を使用して、結果を除外できます。 WHERE句を使用して、選択条件を指定して、テーブルから必要なレコードを選択できます。
構文
MySQLiテーブルからデータを取得するためのWHERE句を含むSELECTコマンドの一般的なSQL構文は次のとおりです-
コンマで区切られた1つ以上のテーブルを使用して、WHERE句を使用してさまざまな条件を含めることができますが、WHERE句はSELECTコマンドのオプション部分です。
WHERE句を使用して、任意の条件を指定できます。
AND または OR 演算子を使用して、複数の条件を指定できます。
WHERE句をDELETEまたはUPDATE SQLコマンドとともに使用して、条件を指定することもできます。
以下は、 WHERE 句で使用できる演算子のリストです。
フィールドAが10を保持し、フィールドBが20を保持すると仮定します-
Operator | Description | Example |
---|---|---|
= | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A = B) is not true. |
!= | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
⇐ | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A ⇐ B) is true. |
WHERE句は、特に MySQLi Join を使用する場合に、テーブルから選択した行をフェッチする場合に非常に便利です。 結合については、別の章で説明します。
指定された条件がテーブル内のどのレコードとも一致しない場合、クエリは行を返しません。
コマンドプロンプトからのデータの取得
これは、SQL SELECTコマンドとWHERE句を使用して、選択したデータをMySQLiテーブルtutorials_infからフェッチします。
例
次の例は、名前が sai である tutorials_inf テーブルからすべてのレコードを返します-
文字列で LIKE 比較を実行しない限り、比較では大文字と小文字が区別されません。 次のように BINARY キーワードを使用して、検索で大文字と小文字を区別できます-
PHPスクリプトを使用したデータの取得:
PHP関数* mysqli_query()*のWHERE CLAUSEで同じSQL SELECTコマンドを使用できます。
例
次の例は、名前が sai である tutorials_inf テーブルからすべてのレコードを返します-
サンプル出力は次のようになります-