MySQL条件查询和范围查询

使用select对数据表进行查询时,可以添加where+条件对数据进行筛选。

一、 比较运算符
运算符功能示例
>大于select * from student where age > 14;
<小于select * from student where age < 18;
>=大于等于select * from student where age >= 15;
<=小于等于select * from student where age <= 16;
!=/<>不等于select * from student where age != 14;

二、 逻辑运算符
运算符功能示例
and与,多个条件同时成立select * from student where age > 14 and age < 18;
or或,多个条件任意一个成立select * from student where age > 14 and gender = ‘男’;
not非,取反select * from student where not age > 14;

三、 模糊查询

模糊查询有两种方法,like或者rlike。

——like:用%代替一个或多个字符;用_代替一个字符。

示例1:查询名字中以“李”开头的学生:

select * from student  where name like "李%";

示例2:查询名字中含有“林”的学生:

select * from student where name like "%林%";

示例3:查询有三个字的名字的学生:

select * from student where name like "___";

示例4:查询有三个字以上的名字的学生:

select * from student where name like "___%";

——rlike:以正则表达式对字符进行匹配。

示例1:查询名字中以“李”开头,以“宁”结尾的学生:

select * from student  where name like "^李.*宁$";

四、 范围查询

——in:非连续的范围查询。
示例1:选择年龄是14、16、18岁的学生:

select * from student where name in (14,16,18);

——between …… and ……:表示连续范围内的信息。

示例1:查询年龄在12到18岁的学生:

select * from student where name between 12 and 18;

示例2:查询年龄不在12到18岁的学生:

select * from student where name age not  between 12 and 18;

五、 判断是否为空

——is null / is not null

示例1:查询数据表中姓名为空的记录:

select * from student where name is null;

示例2:查询数据表中年龄不为空的记录:

select * from student where age is not null;
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值