Query records that meet the conditions in the table-WHERE clause

Source: Internet
Author: User

Query records that meet the conditions in a table-when the WHERE clause uses SQL statements for query operations, developers or users are not interested in all the records in the data table, however, if you want to obtain the data records you actually need, You need to restrict the query results. In SQL statements, you can use the WHERE clause to filter out non-conforming records. 1. Comparison query in the WHERE clause, you can use comparison operators to query information such as values and character values. Comparison operators are classified into three types: arithmetic comparison operators,... And in operators. This section describes the query methods of these three comparison operators. Finally, we will introduce the comparison of strings and time in the WHERE clause. Arithmetic comparison operators in SQL statements include = (equal to),> = (greater than or equal to), <= (less than or equal to),> (greater than), and <(less) ,! = (Not equal to), <> (not equal to),!> (Not greater ),! <(Not less ). In the WHERE clause of a SELECT statement, you can use arithmetic comparison operators to compare a specified column. The syntax format is as follows: Field 1 comparison operator value WHERE, field 1 indicates the name of the field column to be queried in the data table. Field 1 is followed by the arithmetic comparison operator, and the value indicates the value to be compared in the specified column. The result returned by the comparison operator is a logical value. If the logical value is TRUE, the queried records are returned. If the logical value is FALSE, the query results are not returned. Note: When comparing numeric data in the WHERE clause, you can use no single quotation marks. For other types of data (such as string and time) it must be enclosed in single quotes. In addition, the data types on the left and right of the comparison operators in the WHERE clause must be compatible. BETWEEN... The AND operator queries the records in the specified condition range... The AND operator can be used to query records within a specified condition range. Use... A value must be given after the BETWEEN operator AND the AND operator in the AND operator query. The syntax format is as follows: Field 1 BETWEEN value 1 AND value 2, where field 1 represents the field to be queried in the data table; value 1 is a small value in the given value; value 2 is a large value in a given value. The final query result also contains the values of value 1 and value 2. Tip: in SQL, you can use the NOT BETWEEN AND operator to exclude some records. For example, to query the instructor information table, the age is not 30 ~ The instructor information BETWEEN 50 can be achieved through not between and. The IN operator queries records that match the list. The IN operator queries records that match the list. Using the IN operator, you can query any value that meets the specified expression IN the list. The attribute values after the IN operator can be one or multiple. Separate multiple attribute values with commas. The syntax format is as follows: Field 1 IN (attribute value 1, attribute value 2, attribute value 3 ...) Field 1 indicates the fields to be queried in the data table. attribute value 1, attribute value 2, and attribute value 3 indicate the values to be queried. Attribute values can be numeric or character values. If the attribute value is of the character type, use single quotation marks to enclose it. String comparison often encounters string comparison problems when SQL statements are used for comparative queries. When comparing strings, common databases can use comparison operators to compare strings. In addition, you can use the keyword BINARY to compare strings in MySQL databases. When you use a comparison operator to compare strings, the character values on both sides of the comparison operator should be enclosed in single quotes.

SELECT 'mysql' = 'MySQL' 

 

If you want to compare strings to be case-sensitive, you can use the BINARY keyword to perform BINARY Comparison on strings. Using the BINARY keyword, a string (number) is converted into a BINARY object. The format is as follows:
SELECT string1 comparison operator BINARY string2 or select binary string1 comparison operator string2SELECT 'mysql' = BINARY 'mysql'

 

When comparing the date and time in the WHERE clause, the date and time to be compared must be a string format acceptable to the database server. For example, in the student info table (T_student), the student's birth date is set to a variable of the DATETIME date type. To compare the birth date value of a student in the WHERE clause, you can use single quotes to enclose the date value. 2. Logical query in SQL statements, logical operators include AND, OR, AND NOT. The AND operator is used to query records that meet multiple conditions at the same time. The OR operator is used to query records that meet any of the conditions. The NOT operator is used to query records that meet the opposite conditions. The AND operator is used to query records that meet multiple conditions at the same time in SQL Execution operations. In many cases, the WHERE clause does not only want to satisfy one condition, instead, it is expected that the final query results must satisfy multiple conditions (two or more conditions) at the same time ). In this case, the AND operator is used. The syntax format is as follows: Condition 1 AND condition 2 WHERE condition 1 AND condition 2 are the conditions that must be met during query in the WHERE clause. If you want to use the AND operator to join multiple conditions in the WHERE clause. You can use the following syntax format. Condition 1 AND condition 2 AND Condition 3... Here two AND operators are used to connect three conditions. When multiple AND operators are connected, the values on both sides of each AND operator must be TRUE, that is, if these conditions are met at the same time, the results will be displayed. Note: In the Microsoft SQL Server database, replace the AND operator with the "&" symbol to represent the logic AND operation. When you use the OR operator to query records that meet any of the conditions, you only need to meet any of the conditions in the query results. In this case, the OR operator is required. You can use the OR operator to query records that meet any of the conditions. The syntax format is as follows: Condition 1 OR condition 2 WHERE condition 1 and condition 2 are the conditions required for query in the WHERE clause. If either of these conditions is met, the records that meet the conditions will be retrieved. If you want to use the OR operator to join multiple conditions in the WHERE clause. You can use the following syntax format condition 1 OR condition 2 OR condition 3... Here, two OR operators are used to connect three conditions. When multiple OR operators are connected, if any of the conditions on both sides is TRUE, the records that meet the conditions will be displayed. Note: In the Microsoft SQL Server database, use the "|" symbol instead of the OR operator to represent the logic and operation. If you use the NOT operator to reverse the Boolean value of a query condition, you must use the NOT operator to query records that do NOT meet the specified condition. The NOT operator returns the Boolean value of the query condition. Note: "~" is used in the Microsoft SQL Server database. The symbol replaces the NOT operator to represent the logic and operation. These three logical operators can also be mixed in an SQL statement. Among the three logical operators, NOT has the highest priority. AND has a higher priority than OR. For ease of understanding, brackets can be used to enclose each part when these three logical operators are used together. 3. When you use an SQL statement to perform a query, the null query is also used. In a data table, if the row attribute of a table does not contain any value, that is, the row attribute of the table does not contain any data records. Then it is called a null value. In SQL queries, NULL can be used to indicate the meaning of NULL values. In SQL statements, the keyword IS NULL or IS NOT NULL can be used to judge NULL values. 4. when using the LIKE operator to perform fuzzy queries using SQL statements for queries, we often encounter such a situation that we cannot completely determine the complete conditions of the information to be queried, however, this information has some obvious characteristics. For example, if you want to learn the SQL language and want to find some relevant materials in the library, but you do not know what books are available for the SQL language, at this time, keyword SQL is usually entered in the library management system, so that all books related to SQL will be found. This is fuzzy query. The keyword LIKE for fuzzy query is provided in the SQL language. It must be used with the wildcard "%" and. In an SQL statement, the wildcard "_" indicates matching a single character. That is, in a query statement, a "_" can only match one character. In an SQL statement, the wildcard "%" indicates that it matches 0 or multiple characters. That is, a "%" can be 0 characters, a single character, or two or more characters. When SQL is used to perform fuzzy queries, sometimes the character values in a field in the data table contain the characters "%" or. When querying, developers want to query the characters with the character value "%" or "_" as part of the character, at this time, you need to use the ESCAPE keyword to ESCAPE it. Follow these steps to use the ESCAPE keyword for ESCAPE: (1) Add an ESCAPE character before the ESCAPE Character "%" or "_". The ESCAPE character can be any character. (2) name the ESCAPE character after the ESCAPE keyword. After these two steps, the wildcard ("%" or "_") located after the escape character will be escaped as a common character. 5. Use the REGEXP keyword for regular expression. In the MySQL database, a more flexible pattern matching method is provided, that is, using the REGEXP keyword for string pattern matching. Use the REGEXP keyword to perform pattern matching on the string. As long as the matched string contains a child string that matches the matching template, it is considered as pattern matching. The return value is TRUE. In MySQL databases, you can use some pattern matching modifiers to test the pattern matching of strings using the REGEXP keyword. ^: Used to match the start of a string. $: Used to match the end of a string. []: Any character in square brackets can match. For example, [abc] indicates matching characters a, B, or c in square brackets. -: A hyphen is used to indicate the range of character matching. For example, [a-z] indicates matching any character in square brackets from character a to character z. +: Indicates that the character to be matched appears at least once or multiple times in the matched string. *: Indicates that the character used for matching appears zero or multiple times in the matched string. (): The content in parentheses is regarded as a whole. For example, abc indicates that the string matching the style (abc) is abc. {M}: the integer m indicates the number of occurrences of the string before curly braces. For example, {abc} {2} indicates that the string matching the style {abc} {2} is abcabc. In addition, REGEXP can perform a pattern matching test on the string.
SELECT 'agf' REGEXP '[a-d]+','banana' REGEXP '(ana){2}' 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.