Home >Database >Mysql Tutorial >How to use where query statement in mysql

How to use where query statement in mysql

WBOY
WBOYOriginal
2022-05-19 11:12:518798browse

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.

How to use where query statement in mysql

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

How to use the where query statement in mysql

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=&#39;张三&#39;    ## 查询name等于张三的数据
select name from student where name!=&#39;张三&#39;  ## 查询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=&#39;张三&#39;  ##查询age大于10且name等于"张三"的数据。
select * from student where not name=&#39;张三&#39;        ##查询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 &#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中含有"%"的数据

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!

Statement:
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