In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article is about how to use the where keyword in mysql. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
In mysql, the where keyword needs to be used with the SELECT statement to specify the query condition, that is, to conditionally query and return data from the data table; the syntax "SELECT {* | field column name} FROM data table name WHERE query condition;"
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
MySQL WHERE: conditional query data
In MySQL, if you need to conditionally query data from a data table, you can use the WHERE keyword to specify the query criteria.
The syntax format for using the WHERE keyword is as follows:
SELECT {* | Field column name} FROM data Table name 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.
Thank you for reading! This is the end of this article on "how to use the where keyword in mysql". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.