MySQL where clause

We know to use SQL SELECT statement to read data from MySQL table.

To conditionally select data from a table, add a WHERE clause to the SELECT statement.

The following is the general syntax of a SQL SELECT statement to read data from a data table using the WHERE clause:

SELECT field1, field2,...fieldN FROM table_name1, table_name2...[WHERE condition1 [AND [OR]] condition2.....
  • You can use one or more tables in the query statement, separate tables with commas (,), and use the WHERE statement to set query conditions.
  • Any condition can be specified in the WHERE clause.
  • One or more conditions can be specified using AND or OR.
  • The WHERE clause can also be used with the SQL DELETE or UPDATE command.
  • The WHERE clause is similar to the if condition in the programming language, reading the specified data according to the field value in the MySQL table.

The following is a list of operators that can be used in the WHERE clause.

The examples in the table below assume that A is 10 and B is 20

operator description instance
= Equals sign, checks whether two values ​​are equal, and returns true if they are equal (A = B) returns false.
<>, != Not equal, check if two values ​​are equal, return true if they are not equal (A != B) returns true.
> Greater than sign, check whether the value on the left is greater than the value on the right, if the value on the left is greater than the value on the right, return true (A > B) returns false.
< Less than sign, check whether the value on the left is less than the value on the right, if the value on the left is less than the value on the right, return true (A < B) returns true.
>= Greater than or equal sign, check whether the value on the left is greater than or equal to the value on the right, if the value on the left is greater than or equal to the value on the right, return true (A >= B) returns false.
<= Less than or equal sign, check whether the value on the left is less than or equal to the value on the right, if the value on the left is less than or equal to the value on the right, return true (A <= B) returns true.

The WHERE clause is very useful if you want to read specified data in a MySQL data table.

Using the primary key as a conditional query in the WHERE clause is very fast.

If the given criteria do not have any matching records in the table, the query returns no data.


read data from command prompt

We will use the WHERE clause in the SQL SELECT statement to read data from the MySQL data table runoob_tbl:

The following example will read all records in the runoob_tbl table whose runoob_author field value is Sanjay:

MariaDB [(none)]> use RUNOOB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [RUNOOB]> SELECT * from runoob_tbl WHERE runoob_author='Sanjay';

+-----------+---------------+---------------+-----------------+
| runoob_id | runoob_title  | runoob_author | submission_date |
+-----------+---------------+---------------+-----------------+
|         3 | JAVA Tutorial | Sanjay        | 2007-05-06      |
+-----------+---------------+---------------+-----------------+
1 row in set (0.00 sec)

MySQL的WHERE子句的字符串比较是不区分大小写的。 你可以使用 BINARY 关键字来设定WHERE子句的字符串比较是区分大小写的。

如下实例

MariaDB [RUNOOB]> SELECT * from runoob_tbl \
    ->           WHERE BINARY runoob_author='sanjay';
Empty set (0.00 sec)

使用PHP脚本读取数据

可以使用PHP函数的mysql_query()及SQL SELECT 带上 WHERE 子句的命令来获取数据。

该函数用于执行SQL命令,然后通过 PHP 函数 mysql_fetch_array() 来输出所有查询的数据。

以下实例将从 runoob_tbl 表中返回使用 runoob_author 字段值为 Sanjay 的记录:

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT runoob_id, runoob_title, 
               runoob_author, submission_date
        FROM runoob_tbl
        WHERE runoob_author="Sanjay"';

mysql_select_db('RUNOOB');
$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['runoob_id']}  <br> ".
         "Title: {$row['runoob_title']} <br> ".
         "Author: {$row['runoob_author']} <br> ".
         "Submission Date : {$row['submission_date']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>
运行结果如下:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326489208&siteId=291194637