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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

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.

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.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft