条件查询和模糊查询

本文详细介绍了SQL查询的基本语法,包括条件查询和模糊查询。条件查询涉及年龄、入学时间等条件,如年龄大于20岁、在特定日期范围内等。模糊查询则通过LIKE关键字实现,例如查询姓马的学员、姓名中包含特定字符等。这些基础知识对于数据库操作至关重要。
摘要由CSDN通过智能技术生成

一.条件查询:select * from + 表名 + where + 条件;

这里举例来说明:(均在stu表中)

1.查询年龄大于20岁的学员信息

select * from stu where age >20;

2.查询年龄大于等于20岁的学员信息

select * from stu where age >= 20;

3.查询年龄大于等于20岁并且年龄小于等于30岁的学员信息

select * from stu where age >= 20 && age <= 30;

select * from stu where age >= 20 and age >=30;

4.查询入学时间在‘1990-09-01’到‘1999-09-01’之间的学员信息(这里表中入学时间为hire_date)

select * from stu where hire_date between '1990-09-01' and '1999-09-01';

5.查询年龄等于18岁的学员信息

select * from stu where age = 18;

6.查询年龄不等于18岁的学员信息

select * from stu where age != 18;

select * from stu where age <> 18;

7.查询年龄等于18岁 或者 年龄等于20岁 或者 年龄等于22岁的学员信息

select * from stu where age = 18 or age = 20 or age = 22;

select * from stu where age in(18,20,22);

8.查询英语成绩为null的学员信息(这里表中定义的英语成绩为english)

(注意:null值比较不能用 = !=,需要使用is is not)

select * from stu where english is null;

select * from stu where english is not null;

9.select * from 试题表 where 使用场景 in('课堂测试','升级考试','阶段考试');

二.模糊查询:(1)_:代表单个任意字符(2)%:代表任意个数字符

eg1.查询姓‘马’的学员信息

select * from stu where name like '马%';

2.查询第二个字是‘花’的学员信息

select * from stu where name like '_花%';

3.查询姓名中包含‘德’的学员信息

select * from stu where name like '% 德%';

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值