[The Road to Advanced MySQL丨Part 17 (Complete)] This article will help you become proficient in MySQL operators

introduction
Insert image description here

In the previous article, we introduced MySQL functions; in development, the use of MySQL operators is very important. In this article, we use the command line to help readers master the operations of operators in MySQL.

Previous article link:[The Road to Advanced MySQL丨Part 16] This article will help you master MySQL functions

MySQL operators

Operators in MySQL can be divided into mathematical operators, comparison operators, logical operators and bitwise operators.

1. Mathematical operators:

MySQL supports common mathematical operators, such as plus sign (+), minus sign (-), multiplication sign (*), division sign (/) and modulo operator (%), which can be used to add numbers. Basic mathematical operations such as subtraction, multiplication, and division.

2. Comparison operators:

MySQL supports a series of comparison operators, such as equal sign (=), not equal sign (<> or !=), greater than sign (>), less than sign (<), greater than or equal sign (>=) and the less than or equal sign (<=), etc. These operators can be used to compare two values ​​for size or equality.

3. Logical operators:

Logical operators in MySQL include logical AND (AND), logical OR (OR), and logical NOT (NOT). They are usually used to combine multiple conditional expressions to form more complex query conditions.

4. Bit operators:

MySQL also supports bitwise operators, such as bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise left shift (<<) and bitwise right shift (>>) etc., they can be used to perform bit operations on binary numbers.

In addition, there are other special operators, such as the connection operator (||), the null union operator (<=>), and BETWEEN…AND… etc. These operators can be used in specific queries or operations to achieve the corresponding functionality.

Their priority is as follows:

Insert image description here

For example, there is an employee table with the following data:

Insert image description here
Now, we want to query employee information that meets the following conditions:

Age less than 35; name length greater than 3

So we can useWHERE, AND to write the following query statement:

SELECT * 
FROM company
WHERE age<35
AND LENGTH(name)>3;

The results are as follows. You can see that we successfully queried the employee information of 年龄小于35;名字长度大于3

Insert image description here

Another example:

If you have one nameorders table, including the following columns: order_id, order_date, . customer_id, total_amount

Insert image description here

Now, we want to query for orders that meet the following conditions:

The order date is in April 2022 and later; the total order amount is greater than 500;

The order ID is an odd number; the order date is Sunday.

The query statement is as follows:

SELECT *
FROM orders
WHERE order_date >= '2022-04-01'
  AND total_amount > 500
  AND MOD(order_id, 2) = 1
  AND DAYOFWEEK(order_date) = 1; -- 周日对应DAYOFWEEK为1

The result looks like this:

Insert image description here
Successfully queried the order of订单日期在2022年4月及以后;订单总金额大于500;订单ID是奇数;订单日期是周日

Summarize

The above is [MySQL Advanced Road | Part 17], which leads readers to master MySQL operators and achieve in-depth understanding of the MySQL database through specific practical operations.

At this point, the MySQL series has ended.

Insert image description here

Guess you like

Origin blog.csdn.net/2301_77485708/article/details/133964844