Mysqli-where-clause

提供:Dev Guides
移動先:案内検索

MySQLi-WHERE句

MySQLiテーブルからデータを取得するSQL SELECT コマンドを見てきました。 WHERE 句と呼ばれる条件句を使用して、結果を除外できます。 WHERE句を使用して、選択条件を指定して、テーブルから必要なレコードを選択できます。

構文

MySQLiテーブルからデータを取得するためのWHERE句を含むSELECTコマンドの一般的なSQL構文は次のとおりです-

SELECT field1, field2,...fieldN table_name1, table_name2...
[WHERE condition1 [AND [OR]] condition2.....
  • コンマで区切られた1つ以上のテーブルを使用して、WHERE句を使用してさまざまな条件を含めることができますが、WHERE句はSELECTコマンドのオプション部分です。

  • WHERE句を使用して、任意の条件を指定できます。

  • AND または OR 演算子を使用して、複数の条件を指定できます。

  • WHERE句をDELETEまたはUPDATE SQLコマンドとともに使用して、条件を指定することもできます。

    *WHERE* 句は、プログラミング言語のif条件のように機能します。 この句は、指定された値をMySQLiテーブルで使用可能なフィールド値と比較するために使用されます。 外部から与えられた値がMySQLiテーブルで利用可能なフィールド値と等しい場合、その行を返します。

以下は、 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 を使用する場合に、テーブルから選択した行をフェッチする場合に非常に便利です。 結合については、別の章で説明します。

*Primary Key* を使用してレコードを検索し、検索を高速化するのが一般的な方法です。

指定された条件がテーブル内のどのレコードとも一致しない場合、クエリは行を返しません。

コマンドプロンプトからのデータの取得

これは、SQL SELECTコマンドとWHERE句を使用して、選択したデータをMySQLiテーブルtutorials_infからフェッチします。

次の例は、名前が sai である tutorials_inf テーブルからすべてのレコードを返します-

root@host# mysql -u root -p password;
Enter password:*******

mysql> use TUTORIALS;
Database changed

mysql>SELECT * from tutorials_inf where name = 'sai';
+----+------+
| id | name |
+----+------+
|  1 | SAI  |
+----+------+
1 row in set (0.00 sec)

mysql>

文字列で LIKE 比較を実行しない限り、比較では大文字と小文字が区別されません。 次のように BINARY キーワードを使用して、検索で大文字と小文字を区別できます-

root@host# mysql -u root -p password;
Enter password:*******

mysql> use TUTORIALS;
Database changed

mysql> SELECT * from tutorials_inf \WHERE BINARY name = 'sai';
Empty set (0.02 sec)

mysql>

PHPスクリプトを使用したデータの取得:

PHP関数* mysqli_query()*のWHERE CLAUSEで同じSQL SELECTコマンドを使用できます。

次の例は、名前が sai である tutorials_inf テーブルからすべてのレコードを返します-

<?php
   $dbhost = 'localhost:3306';
   $dbuser = 'root';
   $dbpass = '';
   $dbname = 'TUTORIALS';
   $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

   if(! $conn ) {
      die('Could not connect: ' . mysqli_error());
   }
   echo 'Connected successfully<br>';
   $sql = 'SELECT * from tutorials_inf where name="sai"';
   $result = mysqli_query($conn, $sql);

   if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {
         echo "Name: " . $row["name"]. "<br>";
      }
   } else {
      echo "0 results";
   }
   mysqli_close($conn);
?>

サンプル出力は次のようになります-

Connected successfully
Name: SAI