Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement conditional query statement in mysql

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you mysql how to achieve conditional query statements, I hope you have something to gain after reading this article, let's discuss it together!

In mysql, you can use the select statement and the WHERE keyword to implement the conditional query, the implementation statement is "SELECT field name FROM data table WHERE query condition;"; the SELECT statement is used to query the data, and the WHERE keyword is used to specify the query condition.

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

In mysql, you can use select statements and the WHERE keyword to implement conditional queries.

The SELECT statement can query data. Query data refers to the use of different query methods to obtain different data from the database according to the requirements, which is the most frequent and important operation.

The WHERE keyword is used to specify query criteria. If you need to conditionally query data from a data table, you can use the WHERE keyword.

The syntax format is as follows:

SELECT Field name FROM data Table WHERE query condition

The query criteria can be:

Query conditions with comparison operators and logical operators

Query conditions with BETWEEN AND keyword

Query conditions with IS NULL keyword

Query conditions with IN keyword

Query conditions with LIKE keyword

Query statement with single condition

A single condition means that there is only one query condition after the WHERE keyword.

Example 1

The name of the student whose height is 170cm is queried in the tb_students_info data table, the SQL statement and the running results are as follows.

Mysql > SELECT name,height FROM tb_students_info-> WHERE height=170;+-+-+ | name | height | +-+-+ | Susan | 170 | +-+-+ 1 row in set (0.17 sec)

As you can see, the value of the height field recorded in the query result is equal to 170. If there are no records in the data table that match the query criteria when querying according to the specified criteria, the system prompts "Empty set (0.00sec)".

Example 2

Query the names of students under 22 in the tb_students_info data table, the SQL statement and the running results are as follows.

Mysql > SELECT name,age FROM tb_students_info-> WHERE age SELECT name,age,height FROM tb_students_info-> WHERE age > 21 AND height > = 175 +-+ | name | age | height | +-+ | Henry | 23 | 185 | Jim | 24 | 175 | Thomas | 22 | 178 | +-+ 3 rows in set (0.00 sec)

As you can see, the age fields of all records in the query results are greater than 21 and the height fields are greater than or equal to 175.

Example 4

Query the tb_students_info table for student information with age greater than 21 or height greater than or equal to 175. the SQL statement and run results are as follows.

Mysql > SELECT name,age,height FROM tb_students_info-> WHERE age > 21 OR height > = 175 +-+ | name | age | height | +-+ | Dany | 25 | 160 | Green | 23 | 158 | Henry | 23 | 185 | Jane | 22 | 162 | Jim | 24 | 175 | Lily | 22 | 165 | Susan | 23 | 170 | | Thomas | | 22 | 178 | | Tom | 23 | 165 | +-+ 9 rows in set (0.00 sec)

As you can see, the age fields of all records in the query results are greater than 21 or the height fields are greater than or equal to 175.

Example 5

Query the tb_students_info table for student information with age greater than 21 and height less than 175and student information with age less than 21 and height greater than or equal to 175. the SQL statement and run results are as follows.

Mysql > SELECT name,age,height FROM tb_students_info-> WHERE age > 21 XOR height > = 175 +-+ | name | age | height | +-+ | Dany | 25 | 160 | Green | 23 | 158 | Jane | 22 | 162 | Lily | 22 | 65 | Susan | 23 | 170 | Tom | 23 | 165 | +- -+ 7 rows in set (0.00 sec)

As you can see, the age fields of all records in the query results are greater than 21 and the height fields are all less than 175. There are no records in the tb_students_info data table where the age field is less than 21 and the height field is greater than or equal to 175.

OR, AND, and XOR can be used together, but pay attention to the priority of operators when using them.

The more query conditions, the fewer records will be queried. Because the more conditions you set, the more restrictions on the query statement and the fewer records that can meet all the conditions. In order to make the query record exactly what you want, you can set the query condition more specifically in the WHERE statement.

After reading this article, I believe you have a certain understanding of "how to implement conditional query statements in mysql". If you want to know more about it, please follow the industry information channel. Thank you for your reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report