search
HomeDatabaseMysql Tutorialmysql查询操作及正则表达式小结

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

Jun 07, 2016 pm 03:09 PM
limysqloperateInquireregularexpressionEnter

欢迎进入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
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
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft