我的MYSQL学习心得(五)
我的MYSQL学习心得(一)
我的MYSQL学习心得(二)
我的MYSQL学习心得(三)
我的MYSQL学习心得(四)
MYSQL中的运算符很多,这一节主要讲MYSQL中有的,而SQLSERVER没有的运算符
安全等于运算符()
这个操作符和=操作符执行相同的比较操作,不过可以用来判断NULL值。
在两个操作数均为NULL时,其返回值为1而不为NULL;
而当一个操作数为NULL时,其返回值为0而不为NULL。
下面分别是 SELECTNULL1 SELECT10 SELECTNULLNULL 的执行结果
在两个操作数均为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;
TIPS:正则表达式是一个可以进行复杂查询的强大工具,相对于LIKE字符串匹配,他可以使用更多的通配符类型
查询结果更加灵活。
逻辑运算符
逻辑与运算符:AND或者&&
逻辑或运算符:OR或者||
上面两个运算符就不说了,只是比SQLSERVER多了&&和||的写法
异或运算符: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。
TIPS: a XOR b的计算等同于(a AND (NOT b))或者(NOT a AND ( b))
位运算符
由于比较少用到,这里只做简单介绍,同样位运算符在SQLSERVER里是没有的
位运算符是用来对二进制字节中的位进行测试、移位或者测试处理
MYSQL中提供的位运算有
按位或(|)
按位与(&)
按位异或(^)
按位左移(
按位右移(>>)
按位取反(~):反转所有比特
TIPS:可以使用BIN()=binary函数查看一个十进制数的二进制表示
例如20这个数字 SELECTBIN(20)
二进制表示为:10100
特别提示
某一些MYSQL中的特殊字符需要用转义字符才能插入数据库,否则产生意料之外的结果。
下面的特殊字符需要在输入时加反斜线符号开头
输入单引号需要:/'
输入双引号需要:/''
输入反斜杠://
输入回车符:/r
输入换行符:/n
输入制表符:/tab
输入退格符:/b
在插入这些特殊字符到数据库之前一定要进行转义处理
在SQLSERVER里,这些特殊字符不是在前面加反斜杠/,而是加单引号'
例如插入一个单引号,加了反斜杠,插入成功
INSERT INTO table_1(NAME) VALUES('/'')SELECT * FROM table_1
总结
这一节简单介绍了MYSQL里的一些运算符和特殊字符,并且比较了与SQLSERVER的区别
如有不对的地方,欢迎大家拍砖o(∩_∩)o

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.

Scenarios where PostgreSQL is chosen instead of MySQL include: 1) complex queries and advanced SQL functions, 2) strict data integrity and ACID compliance, 3) advanced spatial functions are required, and 4) high performance is required when processing large data sets. PostgreSQL performs well in these aspects and is suitable for projects that require complex data processing and high data integrity.

The security of MySQL database can be achieved through the following measures: 1. User permission management: Strictly control access rights through CREATEUSER and GRANT commands. 2. Encrypted transmission: Configure SSL/TLS to ensure data transmission security. 3. Database backup and recovery: Use mysqldump or mysqlpump to regularly backup data. 4. Advanced security policy: Use a firewall to restrict access and enable audit logging operations. 5. Performance optimization and best practices: Take into account both safety and performance through indexing and query optimization and regular maintenance.

How to effectively monitor MySQL performance? Use tools such as mysqladmin, SHOWGLOBALSTATUS, PerconaMonitoring and Management (PMM), and MySQL EnterpriseMonitor. 1. Use mysqladmin to view the number of connections. 2. Use SHOWGLOBALSTATUS to view the query number. 3.PMM provides detailed performance data and graphical interface. 4.MySQLEnterpriseMonitor provides rich monitoring functions and alarm mechanisms.

The difference between MySQL and SQLServer is: 1) MySQL is open source and suitable for web and embedded systems, 2) SQLServer is a commercial product of Microsoft and is suitable for enterprise-level applications. There are significant differences between the two in storage engine, performance optimization and application scenarios. When choosing, you need to consider project size and future scalability.

In enterprise-level application scenarios that require high availability, advanced security and good integration, SQLServer should be chosen instead of MySQL. 1) SQLServer provides enterprise-level features such as high availability and advanced security. 2) It is closely integrated with Microsoft ecosystems such as VisualStudio and PowerBI. 3) SQLServer performs excellent in performance optimization and supports memory-optimized tables and column storage indexes.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
