MySQL's four operators
- One, arithmetic operators
- Second, the comparison operator
- Three, logical operators
- Four, bit operator
- Five, the precedence of operators
One, arithmetic operators
The arithmetic operators that MySQL supports.
Operator | Comment |
---|---|
+ | addition |
- | Subtraction |
* | multiplication |
/ | division |
% | Take the remainder |
Usage examples
mysql> select 1+2,5-3,3*4,4/3,7%2;
+-----+-----+-----+--------+------+
| 1+2 | 5-3 | 3*4 | 4/3 | 7%2 |
+-----+-----+-----+--------+------+
| 3 | 2 | 12 | 1.3333 | 1 |
+-----+-----+-----+--------+------+
1 row in set (0.00 sec)
1) In the division operation and the remainder operation, the divisor cannot be 0. If the divisor is O, the returned result is NULL. It should be noted that if there are multiple operators, the operations are performed according to the priority of multiplication and division followed by addition and subtraction. Operators with the same priority are in no order.
2) There is another case in the MySQL field value. Certain string-type fields store numeric strings. These fields will be automatically converted into numeric values during arithmetic operations. If the beginning of the string is a number, it will be converted to this number during conversion. If it is a mixed string that contains both characters and numbers, it will be converted to 0 if it cannot be converted to a number. These details require more attention when performing arithmetic operations.
Second, the comparison operator
Comparison operators are a type of operators that are often used when querying data records. By using the comparison operator, you can determine which records in the table are eligible. If the result of the comparison is true, it returns 1, if it is false, it returns 0, and if the result of the comparison is uncertain, it returns NULL. The strings are case-insensitive by default when comparing.
MySQL commonly used comparison operators
Operator | Comment |
---|---|
= | equal |
> | more than the |
< | Less than |
<= | Less than or equal to |
>= | greater or equal to |
!= or <> | not equal to |
IS NULL | Determine whether a value is NULL |
IS NOT NULL | Determine whether a value is not NULL |
BETWEEN AND | Between the two |
IN | In the collection |
LIKE | Wildcard matching, fuzzy query |
GREATEST | Return the maximum value for two or more parameters |
LEAST | Return the minimum value for two or more parameters |
1. Equal to operator
"=" is used to judge whether numbers, strings, and expressions are equal. If they are equal, it returns 1, and if they are not equal, it returns 0. If one of the two comparisons is NULL, the result of the comparison is NULL. Among them, the comparison of characters is judged according to the ASCII code. If the ASCII code is equal, it means that the two characters are the same; if the ASCII code is not equal, it means that the two characters are not the same.
- Commonly used ASCII value I
Usage examples
mysql> select 5=5,'e'='e',(2+6)=(3+5),6='6',4=null;
+-----+---------+-------------+-------+--------+
| 5=4 | 'a'='e' | (2+6)=(3+5) | 6='6' | 4=null |
+-----+---------+-------------+-------+--------+
| 0 | 0 | 1 | 1 | NULL |
+-----+---------+-a------------+-------+--------+
1 row in set (0.00 sec)
Note
1) If both are integers, the comparison is performed according to the integer value.
2) If an integer is a string, and the field value is of the same type, the string will be automatically converted to a number and then compared.
3) If both are strings, compare them according to the strings.
4) If at least one of the two values is NULL, the result of the comparison is NULL.
2. Not equal to operator
There are two ways to write the inequality sign, namely <> or =, which is used to compare numbers, strings, and expressions that are not equal.
If it is not equal, it returns 1, and if it is equal, it returns 0, which is exactly the opposite of the return value of equal. It should be noted that the inequality
operator cannot be used to determine NULL.
Usage examples
mysql> select 3<>3,4!='a','abc'!='cba',0<>null;
+------+--------+--------------+---------+
| 3<>3 | 4!='a' | 'abc'!='cba' | 0<>null |
+------+--------+--------------+---------+
| 0 | 1 | 1 | NULL |
+------+--------+--------------+---------+
1 row in set, 1 warning (0.00 sec)
3. Greater than, greater than or equal to, less than, less than or equal to operators
- The greater than (>) operator is used to determine whether the operand on the left is greater than the operand on the right. If it is greater, it returns 1, otherwise it returns 0. It cannot be used to determine NULL.
- The less than (<) operator is used to determine whether the operand on the left is less than the operand on the right. If it is less, it returns 1; otherwise, it returns 0 and cannot be used to judge NULL.
- Greater than or equal (>=) determines whether the operand on the left is greater than or equal to the operand on the right. If it is greater than or equal to the operand, it returns 1, otherwise it returns 0 and cannot be used to judge NULL.
- Less than or equal (<=) to determine whether the operand on the left is less than or equal to the operand on the right, if it is less than or equal to 1, it returns 1, otherwise it returns and cannot be used to determine NULL.
Usage examples
mysql> select 5>2,'a'>4,'b'<'a','abc'< 'baa',null<9,(5+3)<=(6+8);
+-----+-------+---------+--------------+--------+--------------+
| 5>2 | 'a'>4 | 'b'<'a' | 'abc'< 'baa' | null<9 | (5+3)<=(6+8) |
+-----+-------+---------+--------------+--------+--------------+
| 1 | 0 | 0 | 1 | NULL | 1 |
+-----+-------+---------+--------------+--------+--------------+
1 row in set, 1 warning (0.00 sec)
//字符串做比较时默认只会比较第一个字符的的大小。
//不同类型的字段类型无法比较,返回值为0
4. IS NULL, IS NOTNULL operators
IS NULL judges whether a value is NULL, returns 1 if it is NULL, otherwise returns 0
IS NOT NULL judges whether a value is not NULL, returns 1 if it is not NULL, otherwise returns 0
Usage examples
mysql> select 2 IS NULL,'' IS NOT NULL,NULL IS NULL,0 IS NULL;
+-----------+----------------+--------------+-----------+
| 2 IS NULL | '' IS NOT NULL | NULL IS NULL | 0 IS NULL |
+-----------+----------------+--------------+-----------+
| 0 | 1 | 1 | 0 |
+-----------+----------------+--------------+-----------+
1 row in set (0.00 sec)
//空值不等于NULL。空值虽然不占用空间,但是也表示一个值。
//NULL虽然占用空间,但是不表示一个值,只表示一个NULL对象。
5. BETWEEN AND operator
The BETWEENAND comparison operation is usually used to determine whether a value falls between two values. Can be used to determine whether an integer or English is between a certain number or letter.
Usage examples
mysql> select 5 between 2 and 8,'a' between 'a' and 'b',6 between 1 and 6,'a' between 'b' and 'z';
+-------------------+-------------------------+-------------------+-------------------------+
| 5 between 2 and 8 | 'a' between 'a' and 'b' | 6 between 1 and 6 | 'a' between 'b' and 'z' |
+-------------------+-------------------------+-------------------+-------------------------+
| 1 | 1 | 1 | 0 |
+-------------------+-------------------------+-------------------+-------------------------+
1 row in set (0.00 sec)
// between对于取值之间的头尾是包含的。类似与小于等于和大于等于。
6, LEAST, GREATEST operator
LEAST: When there are two or more parameters, the minimum value is returned. If one of the values is NULL, the
return result is NULL.
GREATEST: When there are two or more parameters, the maximum value is returned. If one of the values is NULL
, the return result is NULL.
Usage examples
mysql> select least(1,2,3,3.1),least('a','b','c'),least('a',5,'b'),least(1,4,8);
+------------------+--------------------+------------------+--------------+
| least(1,2,3,3.1) | least('a','b','c') | least('a',5,'b') | least(1,4,8) |
+------------------+--------------------+------------------+--------------+
| 1.0 | a | 0 | 1 |
+------------------+--------------------+------------------+--------------+
1 row in set, 2 warnings (0.00 sec)
//least :可以比较整数类型和浮点型的最小值,输出类型自动转换为浮点型的最小值。
//least:不能比较整数和字符串类型的最小值。
mysql> select greatest('a','c',5),greatest(4,5,7),greatest(4.5,5,7),greatest('a','y','b');
+---------------------+-----------------+-------------------+-----------------------+
| greatest('a','c',5) | greatest(4,5,7) | greatest(4.5,5,7) | greatest('a','y','b') |
+---------------------+-----------------+-------------------+-----------------------+
| 5 | 7 | 7.0 | y |
+---------------------+-----------------+-------------------+-----------------------+
1 row in set, 2 warnings (0.00 sec)
//greatest:可以比较整数类型和浮点型的最大值,输出类型自动转换为浮点型的最大值
//greatest:可以比较整数型和字符型的最大值,但是会将字符串类型的值忽略不参与比较
7, IN, NOTIN operator
IN: Judge whether a value is in the corresponding list, if it returns 1, otherwise return 0.
NOT IN: Judge whether a value is not in the corresponding list, if not return 1, otherwise return 0
Usage examples
mysql> select 'a' in ('a','b', 'c'),'c' not in ('a','b','c');
+-----------------------+--------------------------+
| 'a' in ('a','b', 'c') | 'c' not in ('a','b','c') |
+-----------------------+--------------------------+
| 1 | 0 |
+-----------------------+--------------------------+
1 row in set (0.00 sec)
8, LIKE, NOTLIKE operators
LIKE is used to match strings, if the match is successful, it returns 1, otherwise it returns 0. LIKE supports two wildcards:'%' is used to match any number of characters, and'_' can only match one character.
NOT LIKE is just the opposite of LIKE. If no match is successful, it returns 1, otherwise it returns 0
Usage examples
mysql> select 'abc' like 'a%',12 like '1_' ,35 not like '3_67','ac'not like '__';
+-----------------+--------------+--------------------+-------------------+
| 'abc' like 'a%' | 12 like '1_' | 35 not like '3_67' | 'ac'not like '__' |
+-----------------+--------------+--------------------+-------------------+
| 1 | 1 | 1 | 0 |
+-----------------+--------------+--------------------+-------------------+
1 row in set (0.00 sec)
Three, logical operators
Logical operators are also called Boolean operators. They are usually used to determine whether an expression is true or false. If it is true, it returns 1, otherwise it returns 0. True and false can also be represented by TRUE and FALSE.
There are four logical operators supported in MySQL
Operator | Comment |
---|---|
NOT or! | Logical negation |
AND或&& | Logical and |
OR 或 II | Logical OR |
XOR | Logical exclusive OR |
- Way of understanding
1表示为真TURE,0表示为假FALSE
逻辑非 1!= 0 0!= 1
真的为假,假的为真
逻辑与 0&&0=0 1&&0=0 0&&!=0 1&&1=1
两个条件都为真为真,两个为假为假
逻辑或 0||0=0 1||0=1 0||1=1 1||1=1
两个条件有一个为真则为真,两个为假为假
逻辑异或 0xor0=0 1xor0=1 0&&1=1 1xor1=0
两个条件一个为真即为真,两个为真或两个为假为假
1. Logical negation
The simplest operator in logical operators is logical negation, which is represented by NOT or !.
Example operation
mysql> select not 2,!3,not 0,!(4%2),!NULL;
+-------+----+-------+--------+-------+
| not 2 | !3 | not 0 | !(4%2) | !NULL |
+-------+----+-------+--------+-------+
| 0 | 0 | 1 | 1 | NULL |
+-------+----+-------+--------+-------+
1 row in set (0.00 sec)
- Logic negation negates the logic test that follows it, turning true to false and false to true.
1) If the operand following NOT is 0, the resulting value is 1;
2) If the operand is non-zero, the resulting value is 0;
3) If the operand is NULL, the resulting value is NULL.
2. Logical AND
Logical AND is usually used to judge the validity of two or more values, and logical AND is represented by AND or &&
Example operation
mysql> select 2 and 5,6 && 0,0&&NULL,1 and NULL;
+---------+--------+---------+------------+
| 2 and 5 | 6 && 0 | 0&&NULL | 1 and NULL |
+---------+--------+---------+------------+
| 1 | 0 | 0 | NULL |
+---------+--------+---------+------------+
1 row in set (0.00 sec)
- Logic AND will be judged by the logic test, all values returned are true return true, otherwise return false.
The meaning of NULL in logical judgment
NULL does not mean that 0 or 1 is true or false. When the judgment condition cannot be judged true or false because of the existence of NULL, it returns a NULL value
Analyzing conditions | && | II | xor |
---|---|---|---|
1 and NULL | NULL | 1 | 1 |
NULL and 1 | NULL | 1 | 1 |
0 and NULL | 0 | NULL | NULL |
NULL and 0 | 0 | NULL | NULL |
NULL and NULL | NULL | NULL | NULL |
3. Logical OR
Logical OR means two or more values, any one of which is non-zero, that is, returns true. Logical OR is represented by || or OR
Example operation
MySQL 5.6 识别 “||” 符号的或运算符
mysql> select 4||5,6 or 0,0||5;
+------+--------+------+
| 4||5 | 6 or 0 | 0||5 |
+------+--------+------+
| 45 | 1 | 05 |
+------+--------+------+
1 row in set (0.00 sec)
MySQL 5.7 不识别 “||” 符号的或运算符,5.7逻辑与判断的时候建议都使用 “or” 或运算符
mysql> select 4||5,6 or 0,0||5;
+------+--------+------+
| 4||5 | 6 or 0 | 0||5 |
+------+--------+------+
| 45 | 1 | 05 |
+------+--------+------+
1 row in set (0.00 sec)
4. Logical XOR
Operands with any two values, if both are 0 or both are non-zero, return 0; if one is 0 and the other is non-zero, the return result is 1; when either value is NULL, the return value Is NULL. Logical exclusive OR is represented by "xor"
Example operation
mysql> select 5 xor 6,0 xor 0,0 xor 5;
+---------+---------+---------+
| 5 xor 6 | 0 xor 0 | 0 xor 5 |
+---------+---------+---------+
| 0 | 0 | 1 |
+---------+---------+---------+
1 row in set (0.00 sec)
Four, bit operator
Bit operators are actually operators that perform calculations on binary numbers.
Bitwise operations in MySQL will first convert the operands into binary format, then perform bitwise operations, and finally convert the calculation result from binary back to decimal format for users to view.
MySQL supports 6 bitwise operators
Bit operator | Comment |
---|---|
& | Bitwise and |
I | Bitwise or |
~ | Bitwise negation |
^ | Bitwise XOR |
<< | Bit shift left |
>> | Bit shift right |
Example operation
- Bitwise AND, OR, XOR, negation
mysql> select 15&5,8|5,5^10,10&~1;
+------+-----+------+-------+
| 15&5 | 8|5 | 5^10 | 10&~1 |
+------+-----+------+-------+
| 5 | 13 | 15 | 10 |
+------+-----+------+-------+
1 row in set (0.00 sec)
运算过程 15&5 8|5 5^10 10&~1
第一步转换为二进制 15=1111 5=101 8=1000 5=101 5=101 10=1010 10=1010 1=1
第二步将二进制数一位一位对其进行运算符判断,对其方式为右对其向左补零。 ~1=1110
15=1111 8=1000 5=0101 10=1010
5=0101 5=0101 10=1010 ~1=1110
得到 0101=5 1101=13 1111=15 1010=10
-
Bitwise AND operation (&), the corresponding binary bits are all 1, and their operation result is 1, otherwise it is 0.
-
Bitwise OR operation (|) means that one or two of the corresponding binary bits are 1, and the result of the operation is 1, otherwise it is 0.
-
Bitwise XOR operation (^), when the corresponding binary bits are not the same, the result of the operation is 1, otherwise it is 0.
-
Bitwise inversion (~) means the corresponding binary number is inverted bit by bit, that is, 1 becomes 0 after the inversion, and becomes 1 after the inversion. It is usually used in combination with other bit operations, and the value is inverted first Complement by another value is performing other bitwise operator operations.
-
Bit shift left, right
mysql> select 5<<2,2<<5,15>>3,25>>5;
+------+------+-------+-------+
| 5<<2 | 2<<5 | 15>>3 | 25>>5 |
+------+------+-------+-------+
| 20 | 64 | 1 | 0 |
+------+------+-------+-------+
1 row in set (0.00 sec)
向左位移过程 5<<2
转换为二进制 5=101
将5的二进制数整体向左移动两个位置空出来的补零得到 10100
转换为十进制为25
向右位移的过程 15>>3
转换为二进制 15=1111
将15的二进制数整体向右移动三个位置,移出的数丢弃得到 1
转换为十进制为1
Note : The shift left or shift right operator is to convert the number to binary, and then shift the specified number of digits to the left or right, the excess digits will be removed and discarded, and the vacant position will be filled with 0 .
Five, the precedence of operators
Operators have priority issues in their use. The precedence of operators determines the order of different operators in the calculation process. Operators with higher levels will be calculated first. If the operators are of the same level, MySQL will perform the calculations in order from left to right. If you are not sure about the priority of the operator used, you can use () to change the priority.