Data Application Master's SQL Basic Tutorial Sharing 7-Arithmetic Negative Join Operation

3. Arithmetic operations

Revisit the childhood classroom

1. Yes, it is addition, subtraction, multiplication and division

【Introduction to knowledge points】

The arithmetic operator + - & / is used to perform arithmetic functions in SQL, and its definition is no different from addition, subtraction, multiplication and division in mathematics.
Assuming that in the student table, as long as students with credits less than or equal to 20 need to take retakes, we can use:

WHERE Credit <= 20;

 

But try combining arithmetic operators to operate and query it.

【Example】

Use arithmetic operators to query the student information that needs to be retaken.
If the result of the credit minus 20 is less than 0, it means that the student needs to take retakes.

SELECT * FROM Student
WHERE (Credit - 20) < 0;

 

2. Combinatorial operations - summary

【Introduction to knowledge points】

Arithmetic operators can be freely combined to perform operations, they follow the most basic arithmetic precedence in arithmetic, and the precedence can be controlled by using parentheses ().
This is the same as the scientific operation in mathematics that everyone usually comes into contact with.

4. Negative operation

Use NOT to negate

1、NOT

【Introduction to knowledge points】

The negation operation is actually based on its original logical operation. By adding the NOT keyword, the returned result is negated, the original logical judgment result returns TRUE, and the NOT negated result returns FALSE.
The NOT keyword can be added to many SQL statements, for example:

NOT BETWEEN value 1 AND value 2
IS NOT NULL
NOT LIKE
NOT IN
......

 

【Example】

Query the information of students whose credits are not between 20 and 28 (closed range) in the student table.
Use NOT BETWEEN AND.

SELECT * FROM Student
WHERE Credit NOT BETWEEN 20 AND 28;

 

5. Connection operation

and and or

1、AND

【Introduction to knowledge points】

As the name implies, AND means and. In SQL, you can use AND to connect two or more conditions, and only return TRUE when the conditions of the connection are satisfied.
Usually we enclose each condition in parentheses, which can increase the readability of the program and effectively avoid some logical errors.

【Example】

Query the student information of the student whose name ends in y and is a female in the student table.

SELECT * FROM Student
WHERE (Sex = 'Female') AND (SName LIKE '%y');

 

2、OR

【Introduction to knowledge points】

OR means or. Similarly, OR can be used to connect two or more conditional queries in SQL. As long as one of the connected conditions can be matched and satisfied, it will return TRUE.

【Example】

Use OR to query the information of students whose names start with M or K in the student table.

SELECT * FROM Student
WHERE (SName Like 'M%') OR (SName LIKE 'K%');

 

3. Boys Ranking of Academy Oscar - Practice

【Introduction to knowledge points】

Through the above chapters, we have learned most of the operators in SQL. Whether it is a comparison judgment, an arithmetic operation or a logical judgment, the WHERE clause plays a crucial role. Of course, we cannot ignore the meaning of these operators themselves. and effect.

As the study progresses, the School of Physics and the School of Botany (with limited data and few people, please forgive me) have combined to conduct a competition for boys' credit ranking. Now, please help them complete this competition.

【Example】

List the credit rankings for boys in physics or botany based on what they have learned before.
In fact, this query needs to meet the following conditions:
1. It is a student of physics or botany;
2. It is a boy;
3. It is sorted according to the credits from high to low, which is reversed in SQL.

SELECT * FROM Student
WHERE (Major IN ('Physics','Botany')) AND (Sex = 'Male')
ORDER BY Credit DESC;

 

Note the use of parentheses in the condition.

 

To be continued below. . . . . .

 

Welcome to visit our official website:

http://www.datanew.com/datanew/homepage

http://www.lechuangzhe.com/homepage

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326216324&siteId=291194637