Home > Database > Mysql Tutorial > How to query specified conditions in mysql

How to query specified conditions in mysql

WBOY
Release: 2022-01-20 14:27:55
Original
8219 people have browsed it

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".

How to query specified conditions in mysql

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=&#39;张三&#39;    ## 查询name等于张三的数据
select name from student where name!=&#39;张三&#39;  ## 查询name不等于张三的数据
select name from student where age>20        ## 查询age大于20的数据
Copy after login

2. Logical operators

and

or

not

Examples are as follows

select * from student where age>10 and name=&#39;张三&#39;  ##查询age大于10且name等于"张三"的数据。
select * from student where not name=&#39;张三&#39;        ##查询name不等于"张三"的数据。
Copy after login

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之间的数据,包含边界值。
Copy after login

4. Empty judgment

select * from student where address is null       ##查询address为null的数据
select * from student where address is not null   ##查询address不为null的数据
Copy after login

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 &#39;王%&#39;   ##查询name中姓张的数据。
select * from student where name like &#39;张_    ##查询name中两个字姓张的数据。
select * from student where name like &#39;%A%%&#39; escape &#39;A&#39;  ##查询name中含有"%"的数据
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template