当前位置:网站首页>There are more than 20 categories of the four operators in MySQL. It took three days and three nights to sort them out. Don't collect them quickly
There are more than 20 categories of the four operators in MySQL. It took three days and three nights to sort them out. Don't collect them quickly
2022-07-24 15:42:00 【51CTO】
Operator connects the operands in the expression , Its function is to indicate the operation performed on the operands in the data table .
What are the benefits of using arithmetic ?
Using operators, you can use the data in the table more flexibly , We can solve many of our problems by using operators , Greatly improved efficiency .
What is an operator :
The operator is to tell mysql Symbols that perform specific arithmetic or logical operations .
mysql The internal operators of are very rich , There are four main categories , Namely :
- Arithmetic operator : For all kinds of numerical operations
- Add (+)
- reduce (-)
- ride (*)
- except (/)
- Seeking remainder ( Or modular operation )(%)
- Comparison operator : Used in comparison operations
- Greater than (>)
- Less than (<)
- be equal to (=)
- Greater than or equal to (>=)
- Less than or equal to (<=)
- It's not equal to (!=)
-
IN、BETWEEN AND、IS NULL、GREATEST、LEAST、LIKE、REGEXP etc.
- Logical operators : The evaluation results are 1(
TRUE)、0(FALSE), These operators are :
- Logic is not (NOT perhaps !)
- Logic and (AND perhaps &&)
- Logic or (OR perhaps ||)
- Logical XOR (XOR)
- An operator : The operands involved in the operation operate in binary bits
- Bit and (&)
- Bit or (|)
- Bit non (~)
- Bit exclusive or (^)
- Move left (<<)
- Move right (>>)
You can see up to 20 Multiple operators , We'll sort it out one by one according to this .
Arithmetic operator
The arithmetic operator is sql The most basic operator in ;

Old rules , Everything is in operation ;
for instance :
Create a table named yunweijia_1 Data sheet for ;
stay yunweijia_1 Insert data into the data table of ;
Addition and subtraction
You can see from above , We can num The value of the field is added and subtracted , and + and - Same priority for , The result of adding before subtracting or subtracting before adding is the same .
Multiplication 、 Division and remainder
You can see from above , Yes num Time for division ,66 By 2 After division , Decimal places are also reserved 4 The number of decimal places . When calculating the remainder , because 66 By 22 Divided by , So the remainder is 0, however 66 Can't be 30 to be divisible by , So the remainder is 6 了 .
Be careful :
In mathematical operations , Divisor is 0 The division of is meaningless , Therefore, the divisor in the division operation cannot be 0, If you are 0 In addition to , The result will return
NULL.as follows :
mysql> select num, num/0, num%0 from yunweijia_1; +------+-------+-------+ | num | num/0 | num%0 | +------+-------+-------+ | 66 | NULL | NULL | +------+-------+-------+ 1 row in set (0.00 sec) mysql>
Comparison operator
The result of a comparison operator is always 1、0 perhaps null. Comparison operators are often used in select Used in the query condition clause of , Used to query records that meet specified conditions .

Equals operator (=)
If equal , The return value is 1, Otherwise, the return value is 0;
From the results, we can see ha , In making a judgment ,2=2 and '2'=2 The return value of is the same , All are 1, Why is this , Because when making judgments ,mysql Automatic conversion , Alphabet character 2 It's converted into numbers 2;'a'='a' Is the same character comparison , So the return value is 1; Expression because of the equal sign (=) The calculation results of both sides are consistent , So the return value is 1, because (=) Cannot be used for null NULL The judgment of the , So the return value is NULL.
Therefore, there are the following rules for numerical comparison :
- If one or two parameters are
NULL, The result of the comparison operation is NULL; - If both parameters in a comparison operation are strings , Then compare by string ;
- If both parameters are integers , Then compare by integer ;
- If you use string and number to judge equality , be
mysql Automatically convert strings to numbers .
Security equals operator (<=>)
mysql The safe equals operator in is similar to the equals operator , But what? , The safety equals operator can be used to judge NULL value . In both operands are NULL when , Its return value is 1, Not directly back NULL; conversely , When an operand is NULL, Its return value is 0, And not NULL.
Not equal to the operator (<> perhaps !=)
'<>' perhaps '!=' Used to judge numbers 、 character string 、 The judgment that the expression is not equal ; If it's not equal , The return value is 1; Otherwise, the return value is 0;
Be careful :
These two operators cannot be used to determine null values
NULL.
Less than or equal to operator (<=)
It is used to judge whether the operand on the left is less than or equal to the operand on the right .
If less than or equal to , The return value is 1, Otherwise, the return value is 0. alike ,<= Can't be used to determine null values NULL.
Less than operator (<)
It is used to judge whether the operand on the left is less than the operand on the right ;
If it is less than , The return value is 1, Otherwise, the return value is 0.< Can't be used to determine null values NULL.
Greater than or equal to operator (>=)
Used to determine whether the operand on the left is greater than or equal to the operand on the right .
If greater than or equal to , The return value is 1; Otherwise, the return value is 0. Can't be used to determine null values NULL.
Greater than operator (>)
It is used to determine whether the operands on the left are greater than those on the right .
If it is greater than , The return value is 1; Otherwise, the return value is 0. Can't be used to determine null values NULL.
IS NULL(ISNULL) and (IS NOT NULL) Operator
is null and isnull Used to check whether a value is NULL;
If it is NULL, The return value is 1, Otherwise, the return value is 0.
is not null Used to check whether a value is non NULL;
If it is right or wrong NULL, The return value is 1, Otherwise, the return value is 0.
BETWEEN AND Operator
Grammar format :
If expr Greater than or equal to min And less than or equal to max, be between The return value of is 1, Otherwise, the return value is 0.
LEAST Operator
Grammar format :
In the case of two or more parameters , Return minimum . If any argument is NULL, be LEAST() The returned value is NULL;
GREATEST(VALUE1, VALUE2,......)
When there are two or more parameters , Return maximum . If any argument is NULL, be grestest() The returned value is NULL.
IN、NOT IN Operator
in Operator is used to determine whether the operands are in One of the values in the list ;
If it is , The return value is 1, Otherwise, the return value is 0.
not in Operator is used to determine whether the expression is in One of the values in the list ;
If not , The return value is 1, Otherwise, the return value is 0.
When in There is null value , If the operator does not hold , Go straight back to null;
like Arithmetic
Used to match strings ; Grammar format :
If expr Meet the matching conditions , The return value is 1, Otherwise, the return value is 0.
When expr Or any of the matching conditions is null, The result is null.
like Operator when matching , You can use wildcards , as follows :
-
% Matches any number of characters , Even zero characters ; -
_ Only one character can be matched ;
REGEXP Operator
mysql Regular expressions in , Used to match strings , The grammar format is :
If expr Meet the matching conditions , return 1, Otherwise return to 0, If expr Or any one of the matching conditions is NULL, The result is NULL.
regexp Operator when matching , The following wildcards are commonly used :
-
^ Match with this character Back String starting with the character of -
$ Match with this character front String at the end of the character -
. Match any single character -
[...] Match any character in square brackets , for example :[abc] matching 'a','b','c'. There are also [a-z] Match any letter , and [0-9] Match any number -
* Match another or more characters in front of him , for example :'x*' Match any number of 'x' character ,[0-9]* Match any number ,* Match any number of any characters .
Logical operators
stay mysql The result of evaluation of all logical operators in is TRUE、FALSE or NULL.
stay mysql in , They withdraw cash as 1(TRUE)、0(FALSE) and NULL.

NOT or !
Logical nonoperator not perhaps !, Indicates that when the operands are 0 when , The results are as follows 1;
When the operands are nonzero , The results are as follows 0;
When the operand is null when , The return value is null.
tip :
When calculating with operators , Be sure to note that different operators have different priorities . If the calculation order cannot be determined , It is best to use parentheses , To ensure the correctness of the calculation results .
ADD perhaps &&
Logic and operators add perhaps && Indicates that when all operands are nonzero values , And not for null when , The results are as follows 1,;
When one or more operands are 0 when , The result is 0;
In other cases, the return value is null.
or perhaps ||
Logical or operator or perhaps || Indicates that when both operands are not null Value and any operand is non-zero , The result is 1, Otherwise, the result is 0;
When one of the operands is null, And the other operand is nonzero , The result is 1, Otherwise, the result is null;
When both operands are null when , The results are as follows null.
XOR
Logical XOR operator xor Indicates that when any operand is null when , The return value is null;
For non null The number of operations , If both operands are not 0 Value or both 0 value , Then the returned result is 0;
If one is for 0 value , The other is not 0 value , The return result is 1.
An operator
Bit operators are operators that evaluate on binary numbers . Bit operators first convert operands to binary numbers , And then we do bit operations , Finally, the calculation result is changed from binary to decimal .

Bit or operator (||)
The essence of bit or operation is to perform logical or operation on several data participating in the operation according to the corresponding binary numbers . One or two of the corresponding binary bits are 1 Then the operation result of this bit is 1, Otherwise 0.
10 The binary value of is 1010,15 The binary value of is 1111, After bitwise or arithmetic , The result is 1111, An integer 15;
9 The binary value of is 1001,4 The binary value of is 0100,2 The binary value of is 0010, After bitwise or arithmetic , The result is 1111, An integer 15.
Bit and operator (&)
The essence of bit and operation is to perform logical and operation on several operands involved in the operation bit by bit according to the corresponding binary numbers . The corresponding binary bits are 1 or 1 Then the operation result of this bit is 1, Otherwise 0.
10 The binary value of is 1010,15 The binary value of is 1111, After bitwise and operation , The result is 1010, An integer 10;
9 The binary value of is 1001,4 The binary value of is 0100,2 The binary value of is 0010, After bitwise and operation , The result is 0000, An integer 0.
Bitwise exclusive or operator (^)
The essence of bit exclusive or operation is to perform logical exclusive or operation on the two data involved in the operation bit by bit according to the corresponding binary number .
When the binary numbers of corresponding bits are different , The result of the corresponding bit is 1. If both corresponding digits are 0 Or all for 1, Then the result of the corresponding bit is 0.
10 The binary value of is 1010,5 The binary value of is 101, After bitwise XOR , The result is 1111, An integer 15;
1 The binary value of is 1,2 The binary value of is 10, After bitwise XOR , The result is 11, That is, the integer is 3;
3 The binary value of is 11,6 The binary value of is 110, After bitwise XOR , The result is 101, An integer 5.
Bit shift left operator (<<)
Bit shift left operator << Moves all bits of the specified binary value to the left by the specified number of bits .
Move left by the specified number of digits , The high value on the left continues to move left , It will not be discarded , On the right side, the low position is left by 0 A filling .
Grammar format :
n Specify the value expr The number of bits to shift .
1 The binary value of is 1, After moving two bits to the left, it becomes 100, Decimal certificate 4;
15 Binary bit of 1111, Move left 6 Bit later became 1111000000, It's a decimal integer 960.
Bit shift right operator (>>)
Bit shift right operator >> Shift all bits of the specified binary value to the right by the specified number of bits .
After moving the specified number of digits to the right , The lower value on the right will be removed and discarded , The left high position is empty with 0 A filling .
The grammar format is :
n Specify the value expr The number of bits to shift .
100 The binary of is 11001000, Two bits to the right, followed by 110010, Decimal system 50;
10 The binary of is 1010, Move right 6 After bit is 0, Even binary is 0.
Bitwise negation operator (~)
The essence of bit inversion operation is to reverse the data involved in the operation bit by bit according to the corresponding binary number , namely 1 It changes to 0、0 It changes to 1.
In logical operation 100 & ~1 in , Because of the bitwise negation operator ~ Is higher than the bitwise and operator , So first of all 1 Reverse the operation , After inversion , Except that the lowest order is 0 Outside , The others are 1, namely 1110, Then with decimal values 100 Operation and operation , The result is 1100100, An integer 100.
Operator precedence
The priority of operators determines the order in which different operators are evaluated in the expression . We have arranged so many operators above , Which operators have the highest priority , Next, we will rank the priority from low to high .

The priority of each operator is different , In general , High level operators are calculated first , If the levels are the same ,mysql Calculate from left to right in the order of expressions . If priority cannot be determined , We can use parentheses () To change priorities . And this will make us sql More elegant , And the calculation process is clearer .

For more information, go to VX official account “ Operation and maintenance home ” , Get the latest article .
------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------
linux Interrupt exception ,linuxuml modeling ,linux Compile multiple source files ,linux How to update the time ,linux Paging video tutorial in ,
linux How to decompress gzip,linux Unzip a directory , How to close linux gateway ,linux Document structure diagram ,linux Improve the permission to execute ,
linux What mark is used in the document , Network security linux strengthening ,linux Check email in ,linux Change the background color ,linux Build a website under the environment .
边栏推荐
- Join parameter processing and @param
- Hard core innovation that database needs to care about in the future
- 微调LayoutLM v3进行票据数据的处理和内容识别
- 【SWT】自定义数据表格
- pip 安装报错 error in anyjson setup command: use_2to3 is invalid.
- MySQL learning notes (summary)
- AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘
- Nine key measures to maintain server security in Hong Kong
- 中信证券账户开通流程,手机上开户安全吗
- Windows10 installation free redis
猜你喜欢

降噪蓝牙耳机哪个好?性价比最高的降噪蓝牙耳机排行

MySQL学习笔记(总结)

Research on stability of time-delay systems based on Lambert function

2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第三题 跑团机器人 (已完结)

被攻击怎么解决?DDoS高防IP防护策略

Leetcode 220. 存在重复元素 III

AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘

Read the paper with me - multi model text recognition network

2022 RoboCom 世界机器人开发者大赛-本科组(省赛)---第一题 不要浪费金币 (已完结)
![[fluent -- layout] flow layout (flow and wrap)](/img/01/c588f75313580063cf32cc01677600.jpg)
[fluent -- layout] flow layout (flow and wrap)
随机推荐
From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
Experience summary of slow SQL problems
Join parameter processing and @param
Fine tune layoutlm V3 for bill data processing and content recognition
What is the ranking of good securities companies? Is online account opening safe
PyTorch的自动求导
Simplified understanding: publish and subscribe
[SWT] user defined data table
IP protocol - network segment division
【Flutter -- 布局】流式布局(Flow和Wrap)
Istio1.12:安装和快速入门
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第三题 跑团机器人 (已完结)
Hard core innovation that database needs to care about in the future
MySQL学习笔记(总结)
kubernetes GPU的困境和破局
2022 robocom world robot developer competition - undergraduate group (provincial competition) -- question 1: don't waste gold (finished)
上课作业(6)——#575. 单词的划分(word)
Memcache cache application (lnmp+memcache)
【tf.keras】:版本从1.x升级到2.x遇到的一个问题:InvalidArgumentError: Cannot assign a device for operation embedding_
Lsyncd搭建同步镜像-用Lsyncd实现本地和远程服务器之间实时同步