Home >Database >Mysql Tutorial >How to query specified conditions in mysql
In mysql, you can use the SELECT statement with the WHERE clause to query specified conditions. The SELECT statement is used to read data, and the WHERE clause is used to set conditions to read data. The syntax is "select field list from Table name where conditional statement".
The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.
How to query specified conditions in mysql
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.
1. Definition
Use the where clause to filter the data in the table and output the filtered results.
2. Syntax
select field list from table name where statement;
3. Category
1. Filter by relational operator
equal=
greater than>
greater than Equal to >=
Less than<
Less than or equal to<=
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
% represents any number of characters (including 0)
_ represents any one 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 query specified conditions in mysql. For more information, please follow other related articles on the PHP Chinese website!