Home Database Mysql Tutorial mysql查询操作及正则表达式小结

mysql查询操作及正则表达式小结

Jun 07, 2016 pm 03:09 PM
li mysql operate Inquire regular expression Enter

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 怎么说呢,用markdown编辑好的文本,无法用在博客园中,不知道怎么处理。 一、排序 1、按多个列排序 使用逗号隔开,如果指定方向则紧挨着要排序的列名 对于多个列的排序,先按照前一个排序然后在前一

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  怎么说呢,用markdown编辑好的文本,无法用在博客园中,不知道怎么处理。

  一、排序

  1、按多个列排序

  使用逗号隔开,如果指定方向则紧挨着要排序的列名

  对于多个列的排序,先按照前一个排序然后在前一个的基础上按照后面的排序。

  如:

  SELECT * FROM a2 ORDER BY a_id DESC,t_id desc

  数据结果如下:

mysql查询操作及正则表达式小结

  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' #good

  SELECT * FROM a WHERE name LIKE 'goo_' #good

  SELECT * 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+通配符匹配大写J

  SELECT * FROM a1 WHERE name regexp binary 'j' #使用正则匹配小写j

[1] [2] 

mysql查询操作及正则表达式小结

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

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: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

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.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

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.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

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.

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities Explained MySQL: Key Features and Capabilities Explained Apr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

See all articles