基本查询
查询多个字段
方法一
代码: select 字段1, 字段2, ... from 表名 ;
例子: select id from text_0.text1 ; (从数据库text_0中的表text1之内查询学号字段内的所有值)
方法二
代码: select * from 表名 ;
例子: select * from text_0.text1 ; (查询text_0数据库中text1表的各字段的所有值)
设置别名
代码: select 字段1 as 别名1 from 表名 ;
例子: select id as '学号' from text1 ; (查询text1表中的字段id内的值,同时给id字段附上别名'学号')
去除重复记录查询
代码: select distinct 字段列表 from 表名 ;
例子: select distinct id from text1 ; (查询表text1中的所有的id字段,并且不输出重复的值)
条件查询
=: 等于
代码: select 字段列表 from 表名 where 字段 = 值 ;
例子: select * from text1 where id = 2021032656; (在text1表中查询id为2021032656的学生信息)
<>: 不等于 (!=)
代码:select 字段列表 from 表名 where 字段 <> 值; (注:用!=效果相同)
例子: select * from text1 where id <> 2021032656; (在表text1中查询学号不为2021032656的学生信息)
between ... and ... : 在某个范围之内(含最小, 最大值)
代码: select 字段列表 from 表名 where 字段 between 值1 and 值2 ;
例子: select * from text1 where id between 2021032655 and 2021032660 ; (在表text1中查询学号在2021032655到2021032660之间的学生信息)
in(...):在in之后的列表中的值,多选一
代码: select 字段列表 from 表名 where 字段 in(值1, 值2, ...) ; (注:in相当于或(即 && 或者 or ), 但是in只能用来判断等于)
例子: select * from text1 where id in(2021032656, 2021032657) ; (在表text1中查询字段id等于2021032656和2021032657的学生信息)
like 占位符:模糊匹配(_匹配单个字符, %匹配任意个字符)
代码1: select 字段列表 from 表名 where 字段 like '__' ; (注:_个数依需求而定)
例子1: select * from text1 where name like '__' ; (查询text1表中的姓名为两个字的学生信息)
代码2:select 字段列表 from 表名 where 字段 like '%值' ; (注:%的位置决定查询的字段的位置, 即%2为查询末尾为2的字段值, 2%为查询开头为2的字段值, %2%为查询中间部分含有2这个值的字段值,)
例子2: select * from text1 where name like '%李%' ; (在表text1中查询姓名中部(或首位)含有'李'的学生信息)
is null: 是null
代码: select 字段列表 from 表名 where 字段 is null ;
例子: select * from text1 where id is null ; (在表text1中查询字段id为空的学生信息)
and 或 &&: 并且
代码: select 字段列表 from 表名 where 条件 && 条件 ; (注:用and效果相同)
例子: select * from text1 where id < 2021032661 && id >= 2021032655 ; (在表text1中查询字段在id大于等于2021032655,小于2021032661范围 内 的学生信息)
or 或 ||: 或者
代码: select 字段列表 from 表名 where 条件 || 条件 ; (注:用or效果相同)
例子: select * from text1 where id < 2021032661 || id >= 2021032655 ; (在表text1中查询字段在id大于等于2021032655,小于2021032661范围 外 的学生信息)
not 或 !: 非
代码: select 字段列表 from 表名 where not 条件 ; (注:用!效果相同)
例子: select * from text1 where not (id < 2021032661 and id >= 2021032655) ; (在表text1中查询字段学号 不 在id < 2021032661 和 id >= 2021032655 范围内的学生信息)
聚合函数
count: 统计数量
代码: select count(字段列表) from 表名 ;
例子: select count(*) from text1 ; (在表text1中查询元组的(行)总数)
max: 最大值
代码: select max(字段) from 表名 ;
例子: select max(id) from text1 ; (查询表text1中的id字段的最大的值)
min: 最小值
代码: select min(字段) from 表名 ;
例子: select min(id) from text1 ; (查询表text1中的id字段的最小的值)
avg: 平均值
代码: select avg(字段) from 表名 ;
例子: select avg(id) from text1 ; (查询表text1中的字段id的平均值)
sum: 求和
代码: select sum(字段) from 表名 ;
例子: select sum(id) from text1 ; (查询表text1中的字段id的总和)
分组查询
代码: select 字段, count(字段列表) from 表名 where 条件 group by 字段 having 条件;
例子: select ad, count(*) from text000 where age < 30 group by ad having count(*) >= 2; (查询年龄小于30的人,并根据地址进行分组,获取人数数量大于等于2的地址及对应人数)
where和having的区别
执行时机不同: where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤。
判断条件不同: where不能戳聚合函数进行判断,而having可以
注意
执行顺序: where > 聚合函数 > having 。
分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段毫无意义
排序查询
代码: select 字段列表 from 表名 order by 字段1 排序方式1, 字段2 排序方式2 ;
例子: select * from text000 order by id desc, age asc ; (在表text000中,对id和age字段进行排序,先对id进行降序排序,若有相同的id,则对具有的相同的元组(行)进行对age(年龄)的升序排序)
分页查询
语法
代码: select 字段列表 from 表名 limit 起始索引, 查询记录数 ;
例子: select * from text000 order by id asc limit 4, 2 ; (在text000表内,对第二页进行查询并且返回两个记录(数据),同时对数据进行升序排序)
注意事项
注: 起始索引 = ( 页码 - 1 ) * 每页显示记录数
分页查询是数据库方言,不同数据库实现方式不同
(页码:相当于对表内数据进行分组,每一组就是一页,页码从1开始,但是下标从0开始(数组); 记录: 显示的数据数,也就是分组的长度、大小,或者说数组的长度)
语法顺序
编写顺序: select > from > where > group by > having > order by > limit
执行顺序: from > where > group by > having > select > order by > limit
验证方法: 根据编写顺序写出含有以上全部语法的语句后,在from后面对表取别名,之后再逐步对执行顺序后的相关内容进行别名修改,执行,以验证执行顺序是否正确