目录
- 一、建表并插入数据
- 1、创建一个people表
- 2、向people表添加数据
- 二、查询语句
一、建表并插入数据
1、创建一个people表
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表添加数据
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、模糊查询(单个条件)
select * from people where name like \’%测%\’;
— 2、模糊查询(多个条件)
select * from people where name like \’%测%\’ and name like \’%人%\’;
select * from people where name like \’%测%\’ or name like \’%人%\’;
——有趣的查询(网页端前端传参%测试)——
— 3、模糊查询(顺序执行)
select * from people where name like \’%测%人%\’;
select * from people where name like \’%人%测%\’;
— 4、_: 表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句
select * from people where name like \’测试人员_\’;
select * from people where name like \’__人员\’;
注:mysql 通配符查询必须用 rlike
— 5、[ ]:表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
select * from people where name rlike \'[试]人员\’;
— 6、[^ ] :表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符 ^ 非。
select * from people where name rlike \'[^试]人员\’;
— 7、^:以xx开头的记录
select * from people where name rlike \’^测\’;
— 8、$:以xx结尾的记录
select * from people where name rlike \’员$\’;
— 9、.:任意单个的
select * from people where name rlike \’.人员\’;
到此这篇关于MySQL 多个%等模糊查询的文章就介绍到这了,更多相关mysql模糊查询内容请搜索悠久资源网以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源网!