

MySQL正则表达式初步 我们知道,在SQL之中,可以用 like 这个谓词(表达式) 来进行模糊检索,并支持 %,?,_等占位符. 但是,这个模糊检索的功能有很多限制,简单来说就是太模糊了。 在MySQL中提供了 REGEXP 关键字来支持正则表达式,当然,只是一些很简单的正则啦。 首
MySQL正则表达式初步-- 建表 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)
;SELECT * FROM t_regcustomer;
SELECT c.id, c.name, c.age FROM t_regcustomer c ;
SELECT c.id, c.name, c.age FROM t_regcustomer c ORDER BY c.age ASC ;
SELECT c.id, c.name, c.age FROM t_regcustomer c WHERE c.name LIKE '%鹏%' ORDER BY c.age ASC ;
SELECT c.id, c.name, c.age FROM t_regcustomer c WHERE c.name REGEXP '.鹏.' ORDER BY c.age ASC ;
SELECT c.id, c.name, c.age FROM t_regcustomer c WHERE c.name REGEXP '^王' ORDER BY c.age ASC ;
SELECT c.id, c.name, c.age FROM t_regcustomer c WHERE c.name REGEXP BINARY '^s' ORDER BY c.age ASC ;
SELECT c.id, c.name, c.age FROM t_regcustomer c WHERE c.name REGEXP BINARY 'a|s' ORDER BY c.name ASC ;
SELECT c.id, c.name, c.age FROM t_regcustomer c WHERE c.name REGEXP BINARY '鹏[123]' ORDER BY c.name ASC ;
SELECT c.id, c.name, c.age FROM t_regcustomer c WHERE c.name REGEXP BINARY '鹏[1-9]' ORDER BY c.name ASC ;
11.更多内容
请查阅 《MySQL必知必会》 68页 正则表达式
