版权声明:本文章刊载的内容,多数为读者作者笔记,笔记内容来自于潭州教育提供的学习资源以及读者本人阅读的文章,特此声明! https://blog.csdn.net/qq_41453285/article/details/85320060
一、查询指定字段
1. 查询所有字段
select * from 表名;
2.查询指定字段
select 字段1,字段2... from 表名;
二、条件查询
概念:条件查询可通过where或者in关键字查询
- =:等于等于
1.单条件查询
select 字段 from 表名 where 条件;
例:select * from student where name="江南、董少";
2.多条件查询
in操作符:查询在括号内的数据
select 字段 from 表名 where 字段 in(....);
例:select * from student where id in(1,2,3);
3.查询空值
空:is null 非空:is not null
select * from 表名 where 字段 is null;
三、筛选、区间查询
1.区间查询
between...and:查询某个范围内的数据,闭区间。如果在between前加not(代表查询区间外的)
select 字段 from 表名 where 字段 between 条件1 and 条件2;
例:select nam from student where id between 2 and 4;
2.like通配符查询
like与%或者_配合使用
- %:代表任意多字符 _:代表单个字符
- %与_的位置可以自己随意设置,以代表不同的信息格式,其中_可以写多个
与%配合使用
select 字符 from 表名 where 字段 like '字符%';
例:select * from student where name like '董%';
(代表查询所有name以'董'为开头任意长度的信息)
与_配合使用
select 字符 from 表名 where 字段 like '字符_';
例:select * from student where address like '长_';
(代表查询所有以‘’长‘’开头的两个字符长度的信息)
四、限制查询次数:limit
格式:limit 起始行,行数
select * from 表名 limit 起始行,行数;
例:select * from student limit 2,3;(从第2行开始查询3行数据)
五、去重查询:distinct
概念:只打印重复的字段,如果有重复的,只打印第一次查询到的
select distinct 字段 from 表名;
例:select distinct id from student;