(8):mysql查询操作及正则表达式小结_MySQL
正则表达式
bitsCN.com怎么说呢,用markdown编辑好的文本,无法用在博客园中,不知道怎么处理。
一、排序
1、按多个列排序
使用逗号隔开,如果指定方向则紧挨着要排序的列名
对于多个列的排序,先按照前一个排序然后在前一个的基础上按照后面的排序。
如:
SELECT * FROM a2 ORDER BY a_id DESC,t_id desc
数据结果如下:
2、order by
与limit
SELECT * FROM a2 ORDER BY a_id DESC,t_id desc LIMIT 1
注意:order by
的位置,from
之后,limit
之前。
二、数据过滤操作符(and/or/in/not)
注意:优先级
当含有and
和or
时,and
的优先级高于or
,所以先执行and
,解决的办法就是使用()
括号的优先级高于and
,能够消除歧义。
如:
SELECT * FROM a2 WHERE (a_id = 2 or t_id>4) AND id>3
in
操作符的特点in
操作符完成了与or
相同的功能,如:
SELECT * FROM a2 WHERE t_id in(1,2,3)SELECT * FROM a2 WHERE t_id =1 OR t_id=2 OR t_id=3
上面两个功能相同。
既然如此,那么为什么还用in
呢,下面就说说in
的优点:
in
的优点:
1.在使用长的合法选项清单时,IN
操作符的更清楚直观
2.计算次序容易管理
3.比OR
执行快
4.IN
最大的优点可以包含其他select语句,能够动态的建立Where子句。
SELECT * FROM a2 WHERE t_id in( SELECT t_id FROM a2 WHERE id=5);
NOT 取反NOT
对IN
、BETWEEN
、exists
子句取反。
三、数据过滤通配符
%通配符
%:表示任何字符出现任意次数
LIKE '惠普%' :以'惠普'开头 LIKE '%惠普' :以'惠普'结尾 LIKE '%惠普%':包含'惠普' LIKE 's%e':以s开头,e结尾
注意:
1.除了一个或多个字符外,%还能匹配0个字符。
2.尾空格可能会干扰通配符匹配,如:保存great时,如果它后面有一个或多个空格,则%great
将不会匹配它们,因为后面有多余的字符(空格),解决办法就是使用双%,%great%
,一个更好的办法是使用函数去掉首尾空格。
3.%不能匹配NULL
.
_通配符
下划线通配符与%相同,但是它只匹配单个字符而非多个。
SELECT * FROM a WHERE name LIKE '_ood' #goodSELECT * FROM a WHERE name LIKE 'goo_' #goodSELECT * FROM a WHERE name LIKE 'go_d' #good
优化:
通配符在搜索处理上比其他的要慢些,所以要记住以下技巧:
1.不要过度使用通配符
2.不要把它们用在搜索模式的开始处,如若不然,搜索起来会很慢的。
3.仔细注意通配符的位置,切勿放错。
四、使用正则表达式
本小节学习如何在Where
子句中使用正则表达式来更好的控制数据过滤。
更多详细知识参考《正则表达式必知必会》
1.基本字符的匹配
SELECT * FROM a1 WHERE name regexp '1000' #匹配名称含有1000的所有行SELECT * FROM a1 WHERE name regexp '.000' #匹配以000结尾的所有行,(.正则中表示:匹配任意一个字符)
从中可以看到正则表达式能够模拟LIKE使用通配符,注意:在通配符能完成的时候就不用正则,因为正则可能慢点,当然正则也能简化通配符,完成更大的作用。所以要有所取舍。
LIKE
与REGEXP
的区别:
SELECT * FROM a1 WHERE name LIKE 'a' SELECT * FROM a1 WHERE name regexp 'a'
下面两条语句第一条不返回数据,第二条返回数据。
原因如下:
LIKE
匹配整个列值时,不会找到它,相应的行也不会被返回(除非使用通配符)
REGEXP
匹配时,会自动查找并返回结果。
那么REGEXP
也能匹配整个列值,使用^
和$
定位符即可!
匹配不区分大小写
Mysql正则大小写都会匹配,为区分大小写可使用binary
关键字,如:
SELECT * FROM a1 WHERE name LIKE binary '%J%' #使用LIKE+通配符匹配大写JSELECT * FROM a1 WHERE name regexp binary 'j' #使用正则匹配小写j
2.进行OR
匹配
|
为正则表达式的OR
操作符,表示匹配其中之一
SELECT * FROM a1 WHERE name regexp binary 'a|j|G'
3.匹配特定字符
使用[]
括起来的字符,将会匹配其中任意单个字符。
SELECT * FROM a1 WHERE name regexp '[12]st'
以上'[12]st'正则表达式,[12]定义一组字符,它的意思是匹配1或2,因此结果如下:
正如所见,
[]
是另一种OR
语句,[123]st可以是[1|2|3]st的缩写,也可以使用后者,注意:1|2|3 st
这样不推荐,因为mysql会假定你的意思是匹配'1'或'2'或'3st'除非你把字符|
括在一个集合中,如:[1|2|3]st
字符也可以否定,加^
则意味着除此之外,如[^123]st
意思是匹配除了1st、2st、3st之外的数据。
4.匹配范围
正则表达式可以使用
-
匹配一个范围,如[0-9]
匹配任意数字,无论是1还是11还是10111,[a-z]
匹配任意小写字母。
5.匹配特殊字符
如上,.
/-
/[]
等是正则表达式的特殊字符,如果要匹配含有这些字符的数据,就需要使用转义(escaping),//
。如//.
表示查找'.'。//
也用来引用元字符(具有特殊含义的字符),如://f
:表示换页//n
:表示换行//r
:表示回车//t
:表示制表//v
:表示纵向制表
Notes:
如果匹配反斜杠本身()则需要使用
///
为什么Mysql使用两个反斜杠(/),而很多语言使用一个反斜杠转义呢,因为mysql自己解释一个,正则表达式库解释一个。
6.匹配字符串

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

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











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.
