8.4.3 Condition query

one, where

WHERE 列名 <比较运算符> 常量或列名

Commonly used comparison operators:

operator meaning
=, >,<,>=,<=, !=,> Comparison of size
AND, OR, NOT multiple conditions
IN Determine the collection
BETWEEN AND Determine the scope
IS NULL empty value
LIKE character match

1. Compare size

Priority: NOT, AND, OR
can use parentheses to change the priority

[Example 8-18] Query the student number, class number and grades of students whose scores are higher than 90.

SELECT*
FROM sC
WHERE Grade>90;

[Example 8-19] Query the student ID and grades of the students whose grades are in [70.80].

SELECT Sno,Grade
FROM sC
WHERE Grade>=70 and Grade<=80;

[Example] Query the student number, course number and grades of elective C1 or C2 and the score is greater than or equal to 80 points.

SELECT Sno,Cno,gradeFROM sc
WHERE (Cno='C1' OR Cno='C2') AND grade>=80;
#或者
WHERE Cno='C1' AND grade>=80
OR Cno='C2'AND grade>=80

2. Query with IN

IN and NOT IN
[Example 8-20] Query the student number and class number of the class C1, C2 or C3.

SELECT Sno,Cno
FROM sc
WHERE Cno IN('C1, 'C2', 'C3') ;

3. Determine the scope (between and)

[Example 8-21] Query (包括75和80)the student number, class number and grades of the students whose grades are between 75 and 80 points.

SELECT Sno,Cno,Grade
FROM Sc
WHERE Grade BETWEEN 75 AND 80;
或者不在此范围 NOT BETWEEN 75 AND 80

4. Null query (IS NULL or IS NOT NULL)

Use the predicate IS NULL or IS NOT NULL
注意“IS NULL”不能用“= NULL”代替。
[Example 8-24] to query the student number and program number of the students who did not take the exam after the course selection.

SELECT Sno,Cno
FROM sc
WHERE Grade IS NULL;

5. String matching

Format:
attribute name LIKE '<matching string>'
attribute name NOT LIKE '<matching string>'

<matching string> = character constant + wildcard two kinds of wildcards:
%: represents 任意长度a string (length can be 0)
_: represents any 单个character

[Example] Query the student information in the S table whose surname is Li and whose name is three characters.

SELECT *
FROM Stu
WHERE Sname LIKE '李__';

[Example 8-26] Query the information of all students surnamed Li in the student table Stu.

SELECT *
FROM Stu
WHERESname LIKE '李%';

[Supplementary] Query the student number and name of the student with "Mei" in the name.

SELECT Sno,Sname
FROM Stu
WHERE Sname LIKE '%梅%';

[Supplementary] Query the student number and name of the student whose second character is "Mei"

SELECT Sno,Sname
FROM Stu
WHERE Sname LIKE '_梅%';

6. Define the escape character

ESCAPE'<escape character>'
such as: ESCAPE '/'
[Supplementary] Query the course number, course name and credits whose course name starts with 'DB_'.

SELECT Cno, Cname, Ccredit
FROM C
WHERE Cname LIKE 'DB/_%'
ESCAPE '/';

Guess you like

Origin blog.csdn.net/qq_25887493/article/details/124081436