MySQL (14) --- WHERE clause

MySQL WHERE clause

We know that to read the data using SQL SELECT statement from the MySQL table.

To conditionally select data from the table, can be added to the WHERE clause of a SELECT statement.

grammar

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

SELECT field1, field2,...fieldN FROM table_name1, table_name2...
[WHERE condition1 [AND [OR]] condition2.....
  • You can query using one or more tables, use a comma between tables, segmentation, and use the WHERE clause to set the search criteria.
  • You can specify any condition in the WHERE clause.
  • You can use AND or OR specify one or more conditions.
  • WHERE clause can also be used in a SQL DELETE or UPDATE command.
  • Conditions similar to the WHERE clause if the programming language to read the specified data according to the field values ​​in the table MySQL.

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

Examples in the following table assuming that A is 10, B 20

Operators description Examples
= Equals detect two values ​​are equal Returns true if equal (A = B) returns false.
<>, != Is not equal to, detect two values ​​are equal, not equal Returns true if (A! = B) returns true.
> Is greater than the number, the value of the detected value is larger than the left to the right, and returns true if the value is greater than the value of the right side of the left (A> B) returns false.
< Less-than sign, the detection value is less than the value of the left side of the right to the left if the value returned is less than the true value of the right Returns true (A <B).
>= Not less than a number, the detected value is greater than or equal to the left of the right value, returns a true value if the value is greater than or equal to the left of the right (A> = B) returns false.
<= Or less number, whether the detected value is less than or equal to the value of the left side of the right returns true if the value is less than or equal to the right of the left (A <= B) returns true.

If we want to read the specified data in MySQL data tables, WHERE clause is very useful.

As a condition of using the primary key to the WHERE clause of the query is very fast.

If given the absence of any matching records in the table, then the query does not return any data.


Reading data from the command prompt

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

Examples

The following examples will read all records in the table runoob_author runoob_tbl Sanjay field values ​​of:

SQL SELECT WHERE clause

SELECT * from runoob_tbl WHERE runoob_author = 'novice Tutorial';

Output:

 

MySQL string comparison WHERE clause is not case sensitive. You can use the BINARY keyword to set the WHERE clause string comparisons are case-sensitive.

Following examples:

BINARY keyword

mysql> SELECT * from runoob_tbl WHERE BINARY runoob_author='runoob.com';
Empty set (0.01 sec)
 
mysql> SELECT * from runoob_tbl WHERE BINARY runoob_author='RUNOOB.COM';
+-----------+---------------+---------------+-----------------+
| runoob_id | runoob_title  | runoob_author | submission_date |
+-----------+---------------+---------------+-----------------+
| 3         | JAVA 教程   | RUNOOB.COM    | 2016-05-06      |
| 4         | 学习 Python | RUNOOB.COM    | 2016-03-06      |
+-----------+---------------+---------------+-----------------+
2 rows in set (0.01 sec)

Used in the examples of  BINARY  keywords, it is case-sensitive, so  runoob_author = 'runoob.com'  query criteria is no data.

Guess you like

Origin blog.csdn.net/zhangbijun1230/article/details/92381777