MySQL mainly has the following operators:
- arithmetic operators
- comparison operator
- Logical Operators
- bitwise operators
arithmetic operators
Arithmetic operators supported by MySQL include:
operator | effect | Example | result |
---|---|---|---|
+ | addition | select 1+2; | 3 |
- | subtraction | select 1-2; | -1 |
* | multiplication | select 2*3; | 6 |
/ or DIV | Divide / Divide | select 2/3; select 11 DIV 4; | 0.6667; 2 |
% or MOD | Remaining | select 11 MOD 4; | 3 |
In division and modulo operations, if the divisor is 0, it will be an illegal divisor and the result will be NULL.
comparison operator
Conditional statements in SELECT statements often use comparison operators. Through these comparison operators, you can determine which records in the table are eligible. Returns 1 if the comparison result is true, returns 0 if it is false, and returns NULL if the comparison result is uncertain.
symbol | describe | Example | result |
---|---|---|---|
= | equal | select 2=3; select NULL=NULL; | 0; NULL |
<>, != | not equal to | select 2<>3;select 2!=3; | 1;1 |
> | more than the | select 2>3; | 0 |
< | less than | select 2<3; | 1 |
<= | less than or equal to | select 2<=3; | 1 |
>= | greater or equal to | select 2>=3; | 0 |
BETWEEN | between two values | >=min&&<=max select 5 between 1 and 10; |
1 |
NOT BETWEEN | not between the two values | select 5 not between 1 and 10; | 0 |
IN | in the collection | select 5 in (1,2,3,4,5); | 1 |
NOT IN | not in the set | select 5 not in (1,2,3,4,5); | 0 |
<=> | Strictly compares two NULL values for equality | When both opcodes are NULL, the resulting value is 1; when one opcode is NULL, the resulting value is 0 | select null<=>null; return 1; |
LIKE | fuzzy matching | select '12345' like '12%'; | 1 |
REGEXP or RLIKE | regex match | select 'beijing' REGEXP 'jing'; | 1 |
IS NULL | Is empty | select null IS NULL; | 1 |
IS NOT NULL | not null | select 'a' IS NOT NULL; | 1 |
Logical Operators
Logical operators are used to determine the truth or falsehood of expressions. If the expression is true, the result returns 1. If the expression is false, the result returns 0.
calculating signs | effect | Example | result |
---|---|---|---|
NOT or ! | logical not | select NOT 1; select !0; | 0;1 |
AND or && | logical and | select 2 AND 0; select 2 && 3; | 0;1 |
OR or || | logical or | select 2 OR 0; select 2 || 3; | 1;1 |
XOR | logical XOR | select 1 xor 0; | 1 |
bitwise operators
Bitwise operators are operators that perform calculations on binary numbers . Bitwise operations first convert the operands to binary numbers and perform bitwise operations. Then convert the result of the calculation from binary back to decimal.
calculating signs | effect | Example | result |
---|---|---|---|
& | bitwise AND | select 3&5; | 1 |
| | bitwise OR | select 3|5; | 7 |
^ | Bitwise XOR | select 3^5; | 6 |
~ | Rebellion | select ~1; | 18446744073709551614 |
<< | shift left | select 3<<1; | 6 |
>> | move right | select 3>>1; | 1 |
operator precedence
The lowest priority is: :=; the highest priority is: !.