正则表达式
我们知道,在SQL之中,可以用 like 这个谓词(表达式) 来进行模糊检索,并支持 %,?,_等占位符.但是,这个模糊检索的功能有很多限制,简单来说就是太模糊了。
在MySQL中提供了 REGEXP 关键字来支持正则表达式,当然,只是一些很简单的正则啦。
首先,我们构造一些测试数据。
-- 建表USE test;DROP TABLE IF EXISTS t_regcustomer;CREATE TABLE t_regcustomer ( id INT(10) AUTO_INCREMENT ,name VARCHAR(256) ,age INT(10) , PRIMARY KEY(id)) COLLATE='utf8_general_ci' ENGINE=InnoDB;增加一些测试数据:
-- 插入一些测试数据:TRUNCATE TABLE t_regcustomer;INSERT INTO t_regcustomer(name, age) VALUES ('王明',20);INSERT INTO t_regcustomer(name, age) VALUES ('王大',21);INSERT INTO t_regcustomer(name, age) VALUES ('小王',22);INSERT INTO t_regcustomer(name, age) VALUES ('小王2',22);INSERT INTO t_regcustomer(name, age) VALUES ('敲不死',23);INSERT INTO t_regcustomer(name, age) VALUES ('憨憨',24);INSERT INTO t_regcustomer(name, age) VALUES ('憨憨2',24);INSERT INTO t_regcustomer(name, age) VALUES ('郭靖名',25);INSERT INTO t_regcustomer(name, age) VALUES ('郭靖2',25);INSERT INTO t_regcustomer(name, age) VALUES ('郭靖3',25);INSERT INTO t_regcustomer(name, age) VALUES ('郭得缸',25),('大鹏',20),('大鹏2',20),('大鹏3',20),('二鹏',19),('鹏鹏',18),('鹏鹏1',18),('小鹏',17),('AAA',17),('aaa',17),('SS',17),('s2',17),('ss',17);
1. 最简单的查询:
SELECT *FROM t_regcustomer;2. 指定列名查询
SELECT c.id, c.name, c.ageFROM t_regcustomer c;3. 对查询结果排序
SELECT c.id, c.name, c.ageFROM t_regcustomer cORDER BY c.age ASC;4. like 模糊检索
%匹配任意数量(0~n)的任意字符
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name LIKE '%鹏%'ORDER BY c.age ASC;5. regexp 关键字
.匹配任意一个字符
注意此处因为没有起始(^)和结束($)限定符,所以只要列中出现的行都会被检索出来.
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP '.鹏.'ORDER BY c.age ASC;6. 正则起始限定符
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP '^王'ORDER BY c.age ASC;7. 大小写敏感
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP BINARY '^s'ORDER BY c.age ASC;8. 正则或运算
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP BINARY 'a|s'ORDER BY c.name ASC;9. 组运算正则
[123] 表示 1、2、3这3个数字之一出现即可
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP BINARY '鹏[123]'ORDER BY c.name ASC;[1-9] 匹配 1、2、3、.... 8、9
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP BINARY '鹏[1-9]'ORDER BY c.name ASC;10. 转义
使用 //
可以转义 /.[]()?-| 以及分页,换行符号等
11.更多内容
请查阅 《MySQL必知必会》 68页 正则表达式

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

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

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

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

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'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

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

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


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
