系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 数据库 > Mysql > 详细页面

MySQL 多个%等模糊查询功能实现

时间:2023-10-29来源:系统城装机大师作者:佚名

一、建表并插入数据

1、创建一个people表

1
2
3
4
5
6
7
8
9
DROP TABLE IF EXISTS `people`;
CREATE TABLE `people`  (
  `id` int NOT NULL COMMENT '主键',
  `name` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '姓名',
  `sex` tinyint NOT NULL COMMENT '性别',
  `age` int NOT NULL COMMENT '年龄',
  `phone` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '联系方式',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

2、向people表添加数据

1
2
3
4
5
insert into people values(1,'测试人员',18,1,'13812345678');
insert into people values(2,'测测人员',19,0,'13812345679');
insert into people values(3,'人员测试',20,1,'13812345680');
insert into people values(4,'测试人员1',21,0,'13812345681');
insert into people values(5,'员人试测',22,1,'13812345682');

二、查询语句

1
2
-- 1、模糊查询(单个条件)
select * from people where name like '%测%';

1
2
-- 2、模糊查询(多个条件)
select * from people where name like '%测%' and name like '%人%';

1 select * from people where name like '%测%' or name like '%人%';

 ------有趣的查询(网页端前端传参%测试)------

1
2
-- 3、模糊查询(顺序执行)
select * from people where name like '%测%人%';

1 select * from people where name like '%人%测%';

1
2
-- 4、_: 表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句
select * from people where name like '测试人员_';

1 select * from people where name like '__人员';

注:mysql 通配符查询必须用 rlike

1
2
-- 5、[ ]:表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
select * from people where name rlike '[试]人员';

1
2
-- 6、[^ ] :表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符 ^ 非。
select * from people where name rlike '[^试]人员';

1
2
-- 7、^:以xx开头的记录
select * from people where name rlike '^测';

1
2
-- 8、$:以xx结尾的记录
select * from people where name rlike '员$';

1
2
-- 9、.:任意单个的
select * from people where name rlike '.人员';

到此这篇关于MySQL 多个%等模糊查询的文章就介绍到这了

分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载