In mysql, the where statement is often used in conjunction with the SELECT statement to specify the conditions of the query. The syntax is "SELECT * FROM data table name WHERE query conditions;"; the where statement can be based on the fields in the mysql table value to read the specified data.
The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.
We know to use the SQL SELECT statement to read data from the MySQL table.
To conditionally select data from a table, add a WHERE clause to the SELECT statement.
Grammar
The following is the general syntax for the SQL SELECT statement to read data from the 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 in the query statement Tables are separated by commas, and the WHERE statement is used to set query conditions.
You can specify any condition in the WHERE clause.
You can use AND or OR to specify one or more conditions.
The WHERE clause can also be applied to SQL DELETE or UPDATE commands.
The WHERE clause is similar to the if condition in programming language, and reads the specified data based on the field value in the MySQL table.
Examples are as follows:
1. Filter by relational operators
is equal to =
Greater than>
Greater than or equal>=
Less than<
Less than or equal<=
Not equal to!=
Examples are as follows
select name from student where name='张三' ## 查询name等于张三的数据 select name from student where name!='张三' ## 查询name不等于张三的数据 select name from student where age>20 ## 查询age大于20的数据
2. Logical operators
and
or
not
Examples are as follows
select * from student where age>10 and name='张三' ##查询age大于10且name等于"张三"的数据。 select * from student where not name='张三' ##查询name不等于"张三"的数据。
3. Range query
in
between large value and decimal value
The example is as follows
select * from student where age in (10,11) ##查询age等于10或者11的数据。 select * from student where age=10 or age=11 ## 与上面语句等效。 select * from student where age between 10 and 24 ##查询age在10到24之间的数据,包含边界值。
4. Empty judgment
select * from student where address is null ##查询address为null的数据 select * from student where address is not null ##查询address不为null的数据
5. Fuzzy query
like
% means any number of characters (including 0 )
_ represents any character
escape: Cancel the wildcard feature of % or _ characters
Examples are as follows
select * from student where name like '王%' ##查询name中姓张的数据。 select * from student where name like '张_ ##查询name中两个字姓张的数据。 select * from student where name like '%A%%' escape 'A' ##查询name中含有"%"的数据
Recommended learning: mysql Video tutorial
The above is the detailed content of How to use where query statement in mysql. For more information, please follow other related articles on the PHP Chinese website!