今天复习了数据库原理与应用教程这本书,重新系统性阅读了一下sql语句,其中着重看了看有关子查询和模糊查询的部分。
模糊查询是针对字符串操作的,类似正则表达式,没有正则表达式强大。
一、一般模糊查询
-
单条件查询
//查询所有姓名包含“张”的记录
select * from student where name like '张' -
多条件查询
//查询所有姓名包含“张”,地址包含四川的记录
select * from student where name like '张' and address like '四川'
//查询所有姓名包含“张”,或者地址包含四川的记录
select * from student where name like '张' or address like '四川'
- 查询包含通配符的字符串
//查询姓名包含通配符%的记录
select * from student where name like '%[%]%' //通过[]转义
//查询姓名包含[的记录
select * from student where name like '%/[%' escape '/' //通过指定'/'转义
//查询姓名包含通配符[]的记录
select * from student where name like '%/[/]%' escape '/' //通过指定'/'转义