Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
The structure of the data table used in this article is as follows:
The contents of the data table are as follows:
1. Arithmetic
1. Arithmetic
1.1 Operation of addition, subtraction, multiplication and division
SELECT
id,
(sales_a + sales_b)as all_sales,
(sales_a - sales_b)as sales_a_b,
(sales_a * price_a + sales_b * price_b)as gmv,
(price_a / price_b)as price_a_b,
sales_a * 2 as s_sales_a
FROM
chapter7
Copy the code
result:
1.2 Divisible and mod operations
SELECT 7 DIV 2
Copy the code
result:
SELECT 7 % 2
Copy the code
result:
SELECT 7 MOD 2
Copy the code
result:
1.3 Null-related operations
Null Evaluates to any number, resulting in null
SELECT
1 + NULL,
1-NULL,
1 * NULL,
1 / NULL
Copy the code
result:
2. Comparison operation
Common comparison operators
The operator | meaning |
---|---|
> | Is greater than |
< | Less than |
= | Is equal to the |
> = | Greater than or equal to |
< = | Less than or equal to |
! = | Is not equal to |
<> | Is not equal to |
between A and B | [A, B] |
is null | A null value |
is not null | The null value |
2.1 Comparison between columns
SELECT id, sales_A, sales_B, sales_A > sales_B as "> ", sales_A < sales_B as" < ", sales_A = sales_B as "= ", sales_A! = sales_B as "not equal ", sales_A is null as" null ", sales_A is not null as "non-null" FROM chapter7Copy the code
result:
Here’s an illustration of the comparison operation. Returns 1 if the comparison is true, 0 otherwise
2.2 Condition Screening
SELECT
id,
sales_a
FROM
chapter7
WHERE sales_a BETWEEN 14 AND 18
Copy the code
result: