Chapter VII basis MySql MySQL Operators

Operators MySql

 

After establishing a MySQL database table structure, the significance of the data represented in the table have been identified. MySQL operation performed by the operator, you can get to another data other than the table structure.

1) Arithmetic Operators

Performing arithmetic operations, such as: addition, subtraction, multiplication and division.

2) Comparison operators

It comprises greater than, less than, equal to or not equal, and the like. Mainly used for comparison values, and so on string matching. For example: LIKE, IN, BETWEEN AND IS NULL and all other comparison operators, further comprising REGEXP regular expression is a comparison operator.

3) Logical Operators

Including AND, OR, XOR, and other non-logical operators. Whose return value is a Boolean value of true (1 or true) and a false value (0 or false).

4) Bitwise Operators

It comprises a bitwise AND, bitwise OR, bitwise, bitwise XOR, bitwise left shift and right shift bit allelic operators. Bitwise must first be converted to binary data, and then operates in a binary format, after the completion of the operation, the binary values ​​back into the original type, returned to the user.

 

Arithmetic Operators

Arithmetic Operators Explanation
+ Adding
- Subtraction
* Multiplication
/ Division, returns the quotient
% Modulo operation returns the remainder

 

Comparison Operators

Comparison Operators Explanation
= equal
< Less than
<= Less than or equal
> more than the
>= greater or equal to
<=> Security equals, does not return UNKNOWN
<> Or! = not equal to
IS NULL 或 ISNULL Determining whether a value is NULL
IS NOT NULL Determining whether a value is not NULL
LEAST When there are two or more arguments, returns the minimum value
GREATEST When there are two or more parameters, the maximum value of return
BETWEEN AND Determining a value falls between two values
IN A judgment value is any value IN list
NOT IN Analyzing an arbitrary value other than a value of IN list
LIKE Wildcard matching
REGEXP Regular expression match

 

The following describes the use of different methods of comparison operators.

1) is equal to the operator "="

The equals sign "=" used to determine numbers, strings and expressions are equal. If equal, the return value is 1, otherwise it returns a value of 0.

When the data are compared with the following rules:

  • If the results of one or two parameters is NULL, then the comparison operation is NULL.
  • If a comparison operation with the two parameters are a string, the string according to the comparison.
  • If both arguments are positive, then in accordance with the integer comparison.
  • If the numbers are equal a string and determining, MySQL can automatically convert the string into a number.

2) Safety equals operator "<=>"

Value for comparison of the two expressions. When the value of the two expressions have a null value or values are null, it will return to UNKNOWN.

For the operator "<=>", when the two expressions are equal to each other or equal to null value, the comparison result is TRUE; wherein if a value is null or non-null value are not equal, but, compared to FALSE, not UNKNOWN situation occurs.

3) is not equal to the operator "<>" or "! ="

"<>" Or "! =" For numbers, strings, unequal expression determination. If not equal, the return value is 1; otherwise it returns a value of 0. The two operators can not be used is determined null (NULL).

4) less than or equal to the operator "<="

"<=" Is used to determine whether the left-hand operand is less than or equal to right operand. Is less than or equal to the return value is 1; otherwise it returns a value of 0. "<=" Can not be used for determining a null value.

5) less than the operator "<"

"<" For determining the left operand is less than right operand. If less than, the return value is 1; otherwise it returns a value of 0. "<" For determining not null.

6) is greater than or equal to the operator "> ="

"> =" To determine whether the left operand is greater than or equal to right operand. If greater than or equal to the return value is 1; otherwise it returns a value of 0. "> =" Can not be used for determining a null value.

7) is greater than the operator ">"

">" For determining the left operand is greater than right operand. If so, the return value is 1; otherwise it returns a value of 0. ">" Can not be used for determining a null value.

8) IS NULL (or the ISNULL)

IS NULL ISNULL and for checking whether a value is NULL, if NULL, the return value is 1; otherwise it returns a value of 0.

9) IS NOT NULL

IS NOT NULL test whether a value is non-NULL, if non-NULL, the return value is 1; otherwise it returns a value of 0.

10) BETWWEN AND

The syntax is:

<Expression> the BETWEEN <Min> the AND <maximum value>

 

If <表达式>greater than or equal to <最小值>and less than or equal to <最大值>, the value of the return BETWEEN 1; a value of 0 otherwise.

11) LEAST

The syntax is:

The LEAST (<value 1>, <value2>, ..., <value n>)

 

Wherein, n represents the value of the parameter list has the value of n. Where two or more arguments, returns the minimum value. If any argument is NULL, The LEAST () returns a value of NULL.

12) GREATEST

The syntax is:

GREATEST (<value 1>, <value2>, ..., <value n>)

 

Wherein, n represents the value of the parameter list has the value of n. Where two or more arguments, returns the maximum value. If any argument is NULL, GREATEST () returns a value of NULL.

13) IN

IN operator is used to determine whether the operand is a value IN list. If so, the return value is 1; otherwise it returns a value of 0.

14) NOT IN

NOT IN operator expression is used to determine a value for the IN list. If not, the return value is 1; otherwise it returns 0.

Logical Operators

In the SQL language, the resulting all logical operator evaluates TRUE, FALSE, or NULL. In MySQL are embodied as 1 (TRUE), 0 (FALSE ) , and NULL.

MySQL logical operators in the following table.

Logical Operators Explanation
Or NOT! Logical NOT
AND or && Logic and
OR or || Logical or
XOR Logical XOR


The following describes the use of different logical operators.

1) NOT or!

! Logical negation operator NOT or indicating when the operand is 0, the return value is 1; and when the operand is non-zero, the return value is 0; when the operand is NULL, the return value is NULL.

2) AND or &&

Logical operators AND and or && indicates when all operands are non-zero value and is not NULL, the return value is 1; when one or more operands is zero, the return value is 0; return value other cases NULL.

3) OR or ||

Or a logical operator OR or ||, represents when both operands are non-NULL value and either operand is non-zero, the result is 1, otherwise the result is 0; when one operand and the other is NULL when the operand is non-zero, the result is 1, otherwise the result is NULL; when both operands are NULL, the result obtained is NULL.

4) XOR

An exclusive OR operator XOR. When either operand is NULL, the return value is NULL; operands for non-NULL, if neither operand is a value of 0 or is 0, the result is returned to 0; when a is 0, the other is not non-0, the result returned is 1.

Bitwise Operators

Bit binary operator is used to shift bits in a byte or test process.

MySQL bitwise operators provided in the following table.

Bitwise Operators Explanation
| Bitwise or
& Bitwise AND
^ Bitwise XOR
<< Bitwise left shift
>> Bitwise Right Shift
~ Bitwise inversion of all bits


The following describes the use of different bit operators.

1) bitwise OR operator "|"

Essence bitwise OR of the two data involved in computing OR'd by a corresponding binary bit by bit. If the corresponding bit is a one or two, then the 1-bit operation result is 0 otherwise.

2) position the operator "&"

Essence of the two bits of data operation involved in computing a logical AND operation performed by the corresponding binary bit by bit. If the corresponding bit is 1, then the 1-bit operation result is 0 otherwise.

3) bitwise exclusive OR operator "^"

Substantial bit XOR operation is involved in computing two data logical exclusive OR operation by a corresponding binary bit by bit. The corresponding bits are different, the result is 1 when the corresponding bits. If the corresponding two bits are both 0 or 1, the corresponding bit of the result is zero.

4) bitwise left shift operator "<<"

位左移运算符“<<”使指定的二进制值的所有位都左移指定的位数。左移指定位数之后,左边高位的数值将被移出并丢弃,右边低位空出的位置用 0 补齐。

语法格式为表达式<<n,这里 n 指定值要移位的位数。

5) 位右移运算符“>>”

位右移运算符“>>”使指定的二进制值的所有位都右移指定的位数。右移指定位数之后,右边高位的数值将被移出并丢弃,左边低位空出的位置用 0 补齐。

语法格式为表达式>>n,这里 n 指定值要移位的位数。

6) 位取反运算符“~”

位取反运算符的实质是将参与运算的数据按对应的二进制数逐位反转,即 1 取反后变 0,0 取反后变为 1。

运算符的优先级决定了不同的运算符在表达式中计算的先后顺序,下表列出了 MySQL 中的各类运算符及其优先级。

优先级由低到高排列 运算符
1 =(赋值运算)、:=
2 II、OR
3 XOR
4 &&、AND
5 NOT
6 BETWEEN、CASE、WHEN、THEN、ELSE
7 =(比较运算)、<=>、>=、>、<=、<、<>、!=、 IS、LIKE、REGEXP、IN
8 |
9 &
10 <<、>>
11 -(减号)、+
12 *、/、%
13 ^
14 -(负号)、〜(位反转)
15 !


可以看出,不同运算符的优先级是不同的。一般情况下,级别高的运算符优先进行计算,如果级别相同,MySQL 按表达式的顺序从左到右依次计算。

另外,在无法确定优先级的情况下,可以使用圆括号“()”来改变优先级,并且这样会使计算过程更加清晰。

MySQL 数据库中的表结构确立后,表中的数据代表的意义就已经确定。而通过 MySQL 运算符进行运算,就可以获取到表结构以外的另一种数据。

1) 算术运算符

执行算术运算,例如:加、减、乘、除等。

2) 比较运算符

包括大于、小于、等于或者不等于,等等。主要用于数值的比较、字符串的匹配等方面。例如:LIKE、IN、BETWEEN AND 和 IS NULL 等都是比较运算符,还包括正则表达式的 REGEXP 也是比较运算符。

3) 逻辑运算符

包括与、或、非和异或等逻辑运算符。其返回值为布尔型,真值(1 或 true)和假值(0 或 false)。

4) 位运算符

包括按位与、按位或、按位取反、按位异或、按位左移和按位右移等位运算符。位运算必须先将数据转换为二进制,然后在二进制格式下进行操作,运算完成后,将二进制的值转换为原来的类型,返回给用户。

 

算术运算符

算术运算符 说明
+ 加法运算
- 减法运算
* 乘法运算
/ 除法运算,返回商
% 求余运算,返回余数

 

比较运算符

比较运算符 说明
= 等于
< 小于
<= 小于等于
> 大于
>= 大于等于
<=> 安全的等于,不会返回 UNKNOWN
<> 或!= 不等于
IS NULL 或 ISNULL 判断一个值是否为 NULL
IS NOT NULL 判断一个值是否不为 NULL
LEAST 当有两个或多个参数时,返回最小值
GREATEST 当有两个或多个参数时,返回最大值
BETWEEN AND 判断一个值是否落在两个值之间
IN 判断一个值是IN列表中的任意一个值
NOT IN 判断一个值不是IN列表中的任意一个值
LIKE 通配符匹配
REGEXP 正则表达式匹配

 

下面分别介绍不同的比较运算符的使用方法。

1) 等于运算符“=”

等号“=”用来判断数字、字符串和表达式是否相等。如果相等,返回值为 1,否则返回值为 0。

数据进行比较时,有如下规则:

  • 若有一个或两个参数为 NULL,则比较运算的结果为 NULL。
  • 若同一个比较运算中的两个参数都是字符串,则按照字符串进行比较。
  • 若两个参数均为正数,则按照整数进行比较。
  • 若一个字符串和数字进行相等判断,则 MySQL 可以自动将字符串转换成数字。

2) 安全等于运算符“<=>”

用于比较两个表达式的值。当两个表达式的值中有一个为空值或者都为空值时,将返回 UNKNOWN。

对于运算符“<=>”,当两个表达式彼此相等或都等于空值时,比较结果为 TRUE;若其中一个是空值或者都是非空值但不相等时,则为 FALSE,不会出现 UNKNOWN 的情况。

3) 不等于运算符“<>”或者“!=”

“<>”或者“!=”用于数字、字符串、表达式不相等的判断。如果不相等,返回值为 1;否则返回值为 0。这两个运算符不能用于判断空值(NULL)。

4) 小于或等于运算符“<=”

“<=”用来判断左边的操作数是否小于或等于右边的操作数。如果小于或等于,返回值为 1;否则返回值为 0。“<=”不能用于判断空值。

5) 小于运算符“<”

“<”用来判断左边的操作数是否小于右边的操作数。如果小于,返回值为 1;否则返回值为 0。“<”不能用于判断空值。

6) 大于或等于运算符“>=”

“>=”用来判断左边的操作数是否大于或等于右边的操作数。如果大于或等于,返回值为 1;否则返回值为 0。“>=”不能用于判断空值。

7) 大于运算符“>”

“>”用来判断左边的操作数是否大于右边的操作数。如果大于,返回值为 1;否则返回值为 0。“>”不能用于判断空值。

8) IS NULL(或者 ISNULL)

IS NULL 和 ISNULL 用于检验一个值是否为 NULL,如果为 NULL,返回值为 1;否则返回值为 0。

9) IS NOT NULL

IS NOT NULL 用于检验一个值是否为非 NULL,如果为非 NULL,返回值为 1;否则返回值为 0。

10) BETWWEN AND

语法格式为:

<表达式> BETWEEN <最小值> AND <最大值>

 

<表达式>大于或等于<最小值>,且小于或等于<最大值>,则 BETWEEN 的返回值为 1;否则返回值为 0。

11) LEAST

语法格式为:

LEAST(<值1>,<值2>,…,<值n>)

 

其中,值 n 表示参数列表中有 n 个值。存在两个或多个参数的情况下,返回最小值。若任意一个自变量为 NULL,则 LEAST() 的返回值为 NULL。

12) GREATEST

语法格式为:

GREATEST (<值1>,<值2>,…,<值n>)

 

其中,值 n 表示参数列表中有 n 个值。存在两个或多个参数的情况下,返回最大值。若任意一个自变量为 NULL,则 GREATEST() 的返回值为 NULL。

13) IN

IN 运算符用来判断操作数是否为 IN 列表中的一个值。如果是,返回值为 1;否则返回值为 0。

14) NOT IN

NOT IN 运算符用来判断表达式是否为 IN 列表中的一个值。如果不是,返回值为 1;否则返回值为 0。

逻辑运算符

在 SQL 语言中,所有逻辑运算符求值所得的结果均为 TRUE、FALSE 或 NULL。在 MySQL 中分别体现为 1(TRUE)、0(FALSE)和 NULL。

MySQL 中的逻辑运算符如下表所示。

逻辑运算符 说明
NOT 或者 ! 逻辑非
AND 或者 && 逻辑与
OR 或者 || 逻辑或
XOR 逻辑异或


下面分别介绍不同的逻辑运算符的使用方法。

1) NOT 或者 !

逻辑非运算符 NOT 或者 !,表示当操作数为 0 时,返回值为 1;当操作数为非零值时,返回值为 0;当操作数为 NULL 时,返回值为 NULL。

2) AND 或者 &&

逻辑与运算符 AND 或者 &&,表示当所有操作数均为非零值并且不为 NULL 时,返回值为 1;当一个或多个操作数为 0 时,返回值为 0;其余情况返回值为 NULL。

3) OR 或者 ||

逻辑或运算符 OR 或者 ||,表示当两个操作数均为非 NULL 值且任意一个操作数为非零值时,结果为 1,否则结果为 0;当有一个操作数为 NULL 且另一个操作数为非零值时,结果为 1,否则结果为 NULL;当两个操作数均为 NULL 时,所得结果为 NULL。

4) XOR

逻辑异或运算符 XOR。当任意一个操作数为 NULL 时,返回值为 NULL;对于非 NULL 的操作数,若两个操作数都不是 0 或者都是 0 值,则返回结果为 0;若一个为 0,另一个不为非 0,则返回结果为 1。

位运算符

位运算符是用来对二进制字节中的位进行移位或者测试处理的。

MySQL 中提供的位运算符如下表所示。

位运算符 说明
| 按位或
& 按位与
^ 按位异或
<< 按位左移
>> 按位右移
~ 按位取反,反转所有比特


下面分别介绍不同的位运算符的使用方法。

1) 位或运算符“|”

位或运算的实质是将参与运算的两个数据按对应的二进制数逐位进行逻辑或运算。若对应的二进制位有一个或两个为 1,则该位的运算结果为 1,否则为 0。

2) 位与运算符“&”

位与运算的实质是将参与运算的两个数据按对应的二进制数逐位进行逻辑与运算。若对应的二进制位都为 1,则该位的运算结果为 1,否则为 0。

3) 位异或运算符“^”

位异或运算的实质是将参与运算的两个数据按对应的二进制数逐位进行逻辑异或运算。对应的二进制位不同时,对应位的结果才为 1。如果两个对应位都为 0 或者都为 1,则对应位的结果为 0。

4) 位左移运算符“<<”

位左移运算符“<<”使指定的二进制值的所有位都左移指定的位数。左移指定位数之后,左边高位的数值将被移出并丢弃,右边低位空出的位置用 0 补齐。

语法格式为表达式<<n,这里 n 指定值要移位的位数。

5) 位右移运算符“>>”

位右移运算符“>>”使指定的二进制值的所有位都右移指定的位数。右移指定位数之后,右边高位的数值将被移出并丢弃,左边低位空出的位置用 0 补齐。

语法格式为表达式>>n,这里 n 指定值要移位的位数。

6) 位取反运算符“~”

位取反运算符的实质是将参与运算的数据按对应的二进制数逐位反转,即 1 取反后变 0,0 取反后变为 1。

运算符的优先级决定了不同的运算符在表达式中计算的先后顺序,下表列出了 MySQL 中的各类运算符及其优先级。

优先级由低到高排列 运算符
1 =(赋值运算)、:=
2 II、OR
3 XOR
4 &&、AND
5 NOT
6 BETWEEN、CASE、WHEN、THEN、ELSE
7 =(比较运算)、<=>、>=、>、<=、<、<>、!=、 IS、LIKE、REGEXP、IN
8 |
9 &
10 <<、>>
11 -(减号)、+
12 *、/、%
13 ^
14 -(负号)、〜(位反转)
15 !


可以看出,不同运算符的优先级是不同的。一般情况下,级别高的运算符优先进行计算,如果级别相同,MySQL 按表达式的顺序从左到右依次计算。

另外,在无法确定优先级的情况下,可以使用圆括号“()”来改变优先级,并且这样会使计算过程更加清晰。

Guess you like

Origin www.cnblogs.com/cute9406/p/11683314.html