Mysql-where-clause

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

MySQL-WHERE句

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

構文

次のコードブロックには、MySQLテーブルからデータを取得するための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条件*のように機能します。 この句は、指定された値をMySQLテーブルで使用可能なフィールド値と比較するために使用されます。 外部から与えられた値がMySQLテーブルで利用可能なフィールド値と等しい場合、その行を返します。

以下に、 WHERE 句で使用できる演算子のリストを示します。

フィールドAが10を保持し、フィールドBが20を保持すると仮定します-

Operator Description Example
= Checks if the values of the two operands are equal or not, if yes, then the condition becomes true. (A = B) is not true.
!= Checks if the values of the two operands are equal or not, if the values are not equal then the condition becomes true. (A != B) is true.
> Checks if the value of the left operand is greater than the value of the right operand, if yes, then the condition becomes true. (A > B) is not true.
< Checks if the value of the left operand is less than the value of the right operand, if yes then the condition becomes true. (A < B) is true.
>= Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes, then the condition becomes true. (A >= B) is not true.
Checks if the value of the left operand is less than or equal to the value of the right operand, if yes, then the condition becomes true. (A ⇐ B) is true.

WHERE句は、特に MySQL Join を使用する場合に、テーブルから選択した行をフェッチする場合に非常に役立ちます。 結合については、別の章で説明します。

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

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

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

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

次の例では、作成者名が Sanjay である tutorials_tbl テーブルからすべてのレコードを返します。

root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> SELECT * from tutorials_tbl WHERE tutorial_author = 'Sanjay';
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
|      3      | JAVA Tutorial  |      Sanjay     |    2007-05-21   |
+-------------+----------------+-----------------+-----------------+
1 rows in set (0.01 sec)

mysql>

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

root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> SELECT * from tutorials_tbl \
   WHERE BINARY tutorial_author = 'sanjay';
Empty set (0.02 sec)

mysql>

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

PHP関数* mysql_query()のWHERE CLAUSEで同じSQL SELECTコマンドを使用できます。 この関数は、SQLコマンドの実行に使用され、後で別のPHP関数 mysql_fetch_array()*を使用して、選択したすべてのデータをフェッチできます。 この関数は、連想配列、数値配列、またはその両方として行を返します。 行がもうない場合、この関数はFALSEを返します。

次の例は、著者名が Sanjay である tutorials_tbl テーブルからすべてのレコードを返します-

<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }

   $sql = 'SELECT tutorial_id, tutorial_title,
      tutorial_author, submission_date
      FROM tutorials_tbl
      WHERE tutorial_author = "Sanjay"';

   mysql_select_db('TUTORIALS');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
      echo "Tutorial ID :{$row['tutorial_id']}  <br> ".
      "Title: {$row['tutorial_title']} <br> ".
      "Author: {$row['tutorial_author']} <br> ".
      "Submission Date : {$row['submission_date']} <br> ".
      "--------------------------------<br>";
   }

   echo "Fetched data successfully\n";
   mysql_close($conn);
?>