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 WHERE
be 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 WHERE
condition is SELECT * FROM students WHERE score >= 80
. How about linear motors
Which, WHERE
after the keyword score >= 80
is the condition. score
Is the name of the column, the column stores student achievement, therefore, score >= 80
to filter out the records of the specified conditions:
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'
notegender
column stores the string need be enclosed in single quotes.
You can write WHERE
conditions:score >= 80 AND gender = 'M'
:
The second condition <条件1> OR <条件2>
indicates the condition 1 or condition 2 is satisfied. For example, the above-described AND
two 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:
Obviously OR
conditions than the AND
loose 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 = 2
coupled with NOT
: NOT class_id = 2
:
The above-mentioned NOT
conditions are NOT class_id = 2
in fact equivalent to class_id <> 2
, therefore, NOT
the 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:
If you do not brackets, in accordance with the operational condition NOT
, AND
, OR
priorities, namely NOT
the 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' |