SQL模糊查询
运算符 | 语法 | 描述 |
---|---|---|
is null | a is null | a为null,返回true |
is not null | a is not null | a为null,返回false |
between…and… | a between b and c | a在b和c之间 |
like | a like b | sql匹配,a匹配b |
in | a in (a1,a2,a3…) | a在(a1,a2,a3…)之中 |
#===================================模糊查询===================================
#查询姓刘的同学
#使用like 搭配 % 或者 _ 来进行模糊查询
#通配符:%表示0或任意数量的字符,_ 表示一个字符。
SELECT studentno,studentname from student
WHERE studentname LIKE '刘%';
#查询姓刘且名字只有两个字的同学
SELECT studentno as 学号,studentname as 学生姓名 from student
WHERE studentname LIKE '刘_';
#查询姓刘且名字有三个字的
SELECT studentno as 学号,studentname as 学生姓名 from student
WHERE studentname LIKE '刘__';
#查询名字中有强字的同学
SELECT studentno as 学号,studentname as 学生姓名 from student
WHERE studentname LIKE '%强%';
#查询学号为1001,1002,1003的学生
#使用in关键字进行查询,在这里不能使用%之类的通配符,
#in后面括号内的值必须是具体值
SELECT studentno ,studentname FROM student
WHERE studentno in(1001,1002,1003);
#查询地址在上海或者北京的同学
SELECT studentno,studentname,address FROM student
WHERE address in ('北京','上海');
#查询生日信息不为空的同学
SELECT studentno,studentname,borndate FROM student
WHERE borndate is not NULL;
#查询生日信息不空的同学
SELECT studentno,studentname,borndate FROM student
WHERE borndate is NULL;
模糊查询中,like关键字通常和通配符%或者_一起使用,如果不使用,查询到就是精确值,%表示任意多个字符, _表示一个字符。