MySQL优化之运算符_MySQL
<strong style="font-family: Arial, Verdana, sans-serif;">安全等于运算符(<=>)</strong>
这个操作符和=操作符执行相同的比较操作,不过<=>可以用来判断NULL值。
在两个操作数均为NULL时,其返回值为1而不为NULL;
而当一个操作数为NULL时,其返回值为0而不为NULL。
下面分别是 SELECT NULL <=>1 SELECT 1<=>0 SELECTNULL <=>NULL 的执行结果
在两个操作数均为NULL时,其返回值为1而不为NULL;
LEAST运算符
语法格式为:LEAST(值1,值2,...值n),其中值n表示参数列表中有n个值。在有两个或多个参数的情况下,返回最小值。
假如任意一个自变量为NULL,则LEAST()的返回值为NULL
使用LEAST运算符进行大小判断,SQL语句如下:
SELECT LEAST(2,0),LEAST('a','b','c'),LEAST(10,NULL)

由结果可以看到,当参数中是整数或者浮点数时,LEAST将返回其中最小的值;
当参数为字符串时,返回字母中顺序最靠前的字符;
当比较值列表中有NULL时,不能判断大小,返回值为NULL
GREATEST运算符
语法格式为: GREATEST(值1,值2,...值n),其中n表示参数列表中有n个值。
在有两个或多个参数的情况下,返回最大值。
假如任意一个自变量为NULL,则GREATEST()的返回值为NULL
使用GREATEST运算符进行大小判断,SQL语句如下:
SELECT GREATEST(2,0),GREATEST('a','b','c'),GREATEST(10,NULL)

由结果可以看到,当参数中是整数或者浮点数时,GREATEST将返回其中最大的值;
当参数为字符串时,返回字母中顺序最靠后的字符;
当比较值列表中有NULL时,不能判断大小,返回值为NULL
REGEXP 运算符
在SQLSERVER里是没有正则函数或者运算符的,MYSQL在这方面的确比较完善
用来匹配字符串,语法格式为:expr REGEXP 匹配条件,如果expr满足匹配条件,返回1;
如果不满足,则返回0;
若expr或匹配条件任意一个为NULL,则结果为NULL
常用的几种通配符:
(1)'^'匹配以该字符后面的字符开头的字符串
(2)'$'匹配以该字符后面的字符结尾的字符串
(3)'.'匹配任何一个单字符
(4)'[...]'匹配在方括号内的任何字符。例如,“[abc]" 匹配a、b或c。
字符的范围可以使用一个'-',“[a-z]”匹配任何字母,而“[0-9]”匹配任何数字
(5)'*' 匹配零个或多个在他前面的字符。例如,“x*”匹配任何数量的'*'字符,“[0-9]*”匹配任何数量的数字,
而“.*”匹配任何数量的任何字符。
使用REGEXP 运算符进行字符串匹配运算,SQL语句如下:
SELECT 'ssky' REGEXP '^s','ssky' REGEXP 'y$' ,'ssky' REGEXP '.sky','ssky' REGEXP '[ab]';

由结果可以看到,指定匹配字符串为ssky。
'^s'表示匹配任何以字母s开头的字符串,因此满足匹配条件,返回1;
'y$'表示匹配任何以字母y结尾的字符串,因此满足匹配条件,返回1;
'.sky'表示匹配任何以sky结尾,字符长度为4的字符串,因此满足匹配条件,返回1;
'^s'表示匹配任何以字母s开头的字符串,因此满足匹配条件,返回1;
'[ab]'表示匹配任何包含字母a或者b的字符串,指定字符串中没有字母a也没有字母b,因此不满足匹配条件,返回0;
注意:正则表达式是一个可以进行复杂查询的强大工具,相对于LIKE字符串匹配,他可以使用更多的通配符类型,查询结果更加灵活
逻辑运算符
逻辑与运算符:AND或者&&
逻辑或运算符:OR或者||
异或运算符:XOR
当任意一个操作数为NULL时,返回值为NULL;对于非NULL的操作数,如果两个操作数都是非0值或者都是0值,则返回结果为0;
如果一个为0值,另一个为非0值,返回结果为1
使用异或运算符XOR进行逻辑判断,SQL语句如下
SELECT 1 XOR 1, 0 XOR 0,1 XOR 0,1 XOR NULL,1 XOR 1 XOR 1

由结果可以看到‘1 XOR 1’和‘0 XOR 0’中运算符两边的操作数都为非零值,或者都是零值,因此返回0;
'1 XOR 0'中两边的操作数,一个为0值,另一个为非0值,返回结果为1;
'1 XOR NULL'中有一个操作数为NULL,返回结果为NULL;
'1 XOR 1 XOR 1'中有多个操作数,运算符相同,因此运算符从左到右依次计算,'1 XOR 1'的结果为0,再与1进行异或运算,因此结果为1。
注意: a XOR b的计算等同于(a AND (NOT b))或者(NOT a AND ( b))
位运算符
由于比较少用到,这里只做简单介绍
位运算符是用来对二进制字节中的位进行测试、移位或者测试处理
MYSQL中提供的位运算有
按位或(|)
按位与(&)
按位异或(^)
按位左移(<<)
按位右移(>>)
按位取反(~):反转所有比特
TIPS:可以使用BIN()=binary函数查看一个十进制数的二进制表示
例如20这个数字 SELECT BIN(20)
二进制表示为:10100

特别提示
某一些MYSQL中的特殊字符需要用转义字符才能插入数据库,否则产生意料之外的结果。
下面的特殊字符需要在输入时加反斜线符号开头
输入单引号需要:\'
输入双引号需要:\''
输入反斜杠:\\
输入回车符:\r
输入换行符:\n
输入制表符:\tab
输入退格符:\b
在插入这些特殊字符到数据库之前一定要进行转义处理
例如插入一个单引号,加了反斜杠,插入成功
INSERT INTO table_1(NAME) VALUES('\'') SELECT * FROM table_1


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In the C language, there is no root operator. The built-in function "sqrt()" is used to open the root, and the syntax "sqrt(value x)" is used; for example, "sqrt(4)" is to perform the square root operation on 4. , the result is 2. sqrt() is a built-in root operation function in C language. Its operation result is the arithmetic square root of the function variable; this function can neither operate negative values nor output imaginary results.

For Golang developers, "invaliduseof...operator" is a common error. This error usually occurs when using variable-length parameter functions. It will be detected at compile time and indicate which parts have problems. This article will introduce how to solve this error. 1. What is a variable-length parameter function? A variable-length parameter function is also called a variable-parameter function. It is a function type in the Golang language. Using variable-length parameter functions, you can define multiple ones as follows

In Java, "%" means remainder. It is a binary arithmetic operator that can perform division operations and obtain the remainder. The syntax is "operand 1 % operand 2". The operand of the remainder operator "%" is usually a positive integer or a negative number or even a floating point number. If a negative number participates in this operation, the result depends on whether the previous number is positive or negative.

The += operator is used to add the value of the left operand to the value of the right operand and assign the result to the left operand. It is suitable for numeric types and the left operand must be writable.

In PHP, the "==" symbol is a comparison operator that can compare whether two operands are equal. The syntax is "operand 1 == operand 2". The "==" operator compares and tests whether the variable on the left (expression or constant) has the same value as the variable on the right (expression or constant); it only compares the values of the variables, not the data types. If the two values are the same, it returns a true value; if the two values are not the same, it returns a false value.

In PHP, you can use the "%" and "==" operators to determine whether two numbers are divisible; you only need to use the "%" operator to divide the two numbers to get the remainder, and then use the "==" operator Just judge whether the obtained remainder is 0. The syntax is "Number 1 % Number 2 == 0". If it is 0, it can be divisible. If it is not 0, it cannot be divisible.

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Magic methods in Python are special methods that allow you to add "magic" to a class. They are often named surrounded by two underscores. Python's magic method, also known as the dunder (double underline) method. Most of the time, we use them for simple things like constructors (init), string representations (str, repr) or arithmetic operators (add/mul). In fact, there are many methods that you may not have heard of but are very useful. In this article, we will sort out these magic methods! We all know the size of the iterator __len__ method, which can be used in container classes Implement the len() function on. However, if you want to get the length of a class object that implements iterator
