4.4 DQL → 模糊查询
模糊查询:其实本质上 也是 比较运算符 的玩法。
运算符 | 语法 | 描述 |
---|---|---|
is null | a is null | 如果为空则结果为真 |
is not null | a is not null | 如果不为空则结果为真 |
bytween…and… | a between b and c | 如果 a 在 b 和 c 之间则结果为真 |
Like | a like b | 如果 a 字符串匹配 b 则结果为真 |
in | a in (a1,a2,a3) | 如果 a 在 a1,a2,a3 其中一个里面则结果为真 |
查询姓刘的同学
select `studentno`,`studentname` from `student`
where studentname like '刘%';
这是因为 like 里面 有 特殊的符号 % 可以 接收 任意的 0个 甚至多个 字符。
select `studentno`,`studentname` from `student`
where studentname like "刘__";
而且 _ 下划线符号,它也有特殊的功能。就是 能够接收 1 个 任意字符。
对于模糊搜索来说,其实 Like 才是正统!!!其它的都算不太上是 模糊搜索。
- 查询多个精准匹配的数据(本质上就是 = 和 OR 操作符的结合存在!)
select `studentno`,`studentname` from `student` where address in('北京朝阳','广东深圳');
- 证明 in 就是 = 和 || 的结合体:
select `studentno`,`studentname` from `student` where address='北京朝阳' || address="广东深圳";
3. is null 搜索没有填写地址的 学生
select `studentno`,`studentname` from `student` where `address` is null;
5. is not null 搜索有生日的 学生
select `studentno`,`studentname` from `student` where `borndate` is not null;