sql query conditions

Use SELECT * FROM <表名>can check all the records to a table. However, many times, we do not want to get all the records, but selectively obtain records according to the conditions specified criteria, such as student records in the query score 80 points or more. In the case of a table with millions of records, access to all records not only time but also costs memory and network bandwidth.

SELECT statements can WHEREbe set query conditions, the results are records that meet the query criteria. For example, to specify the condition "score 80 points or more students," written WHEREcondition is SELECT * FROM students WHERE score >= 80. How about linear motors

Which, WHEREafter the keyword score >= 80is the condition. scoreIs the name of the column, the column stores student achievement, therefore, score >= 80to filter out the records of the specified conditions:

- query by the conditions students:

Therefore, the condition of the query syntax is:

SELECT * FROM <表名> WHERE <条件表达式>

The conditional expression may be <条件1> AND <条件2>satisfied condition 1 and the condition 2 expression. For example, qualifying "score 80 points or more," and also meet the conditions, "the boys", write these two conditions:

  • Condition 1: The data determination score column: score >= 80;
  • Condition 2: The data judging gender column: gender = 'M'note gendercolumn stores the string need be enclosed in single quotes.

You can write WHEREconditions:score >= 80 AND gender = 'M' :

- AND condition query by students:

The second condition <条件1> OR <条件2>indicates the condition 1 or condition 2 is satisfied. For example, the above-described ANDtwo conditions to queries OR, the query result is the "score 80 points or more" or "boy", i.e., a condition satisfying any one of the selected record:

- Press OR condition query students:

Obviously ORconditions than the ANDloose condition, the matching records returned by a greater extent.

The third condition that NOT <条件>indicates record "does not meet the conditions". For example, write a "not two classes of students," this condition, you can write "is two classes of students": , class_id = 2coupled with NOT: NOT class_id = 2:

- NOT condition query by students:

The above-mentioned NOTconditions are NOT class_id = 2in fact equivalent to class_id <> 2, therefore, NOTthe query is not very common.

To combine three or more conditions, you need to use parentheses ()show how conditional operations. For example, a complex prepared by: score 80 or less 90 or more, and boys:

- according to multiple criteria queries students:

If you do not brackets, in accordance with the operational condition NOT, AND, ORpriorities, namely NOTthe highest priority, followed AND, finally OR. Parentheses can change the priority.

Common conditional expression

condition Expression example 1 Example 2 Expression Explanation
= Equivalence determination using score = 80 name = 'abc' String needs to be enclosed in single quotes
Use> Analyzing greater than score > 80 name > 'abc' String comparison database is set according to the ASCII code, Chinese character based on the comparison
Use> = greater than or equal to the determination score >= 80 name >= 'abc'  
Use <Analyzing less than score < 80 name <= 'abc'  
Use <= less than or equal to the determination score <= 80 name <= 'abc'  
Use <> unequal Analyzing score <> 80 name <> 'abc'  
Determined using a similar LIKE name LIKE 'ab%' name LIKE '%bc%' %表示任意字符,例如'ab%'将匹配'ab','abc','abcd'

Guess you like

Origin www.cnblogs.com/furuihua/p/11857517.html