1.where条件
where A and|or|in|not in B
select * from test.order where age<=18; #一个筛选条件
where A and B #【and】 表示且
select * from test.order where age<=18 and name='李明'; #多个条件筛选
where A or B #【or】 表示或
select * from test.order where age<=18 or name='李明'; #多个条件筛选
where A in B #【or】 表示或
select * from test.order where age<=18 or name in('李明','张三'); #多个条件筛选
2.排序
**order by 字段 ==order by 字段 asc** #排序,默认为增序
select * from stu order by age;
select * from stu order by age asc;
**order by 字段 desc** #排序,默认为递减
select * from stu order by age desc;
3.模糊查询
**where 字段 like 'j%';** #字母为j开头的数据行
select * from stu where name like 'j%';
**where 字段 like '%j';** #字母为j结尾的数据行
select * from stu where name like 'j%';
**where 字段 like '%j%';** #字母含有j的数据行,不关心位置
select * from stu where name like '%j%';
4.占位查询
**where 字段 like '_j%';** #第二个字母为j的数据行 【_】表示占位符
select * from stu like '_j%';
**where 字段 like '__j%';** #第三个字母为j的数据行 【__】表示占位符
select * from stu like '__j%';
5.合并表
**union all** #合并表
如:
select * from A
union all
select * from B;;
**union** #合并且去重
select * from A
union
select * from B;
6.null
**select * from A where 字段 is null;**
select * from stu where name is null;
**select * from A where 字段 is not null;**
select * from stu where name is not null;
7.分组语句(聚合)
应用场景:求和,均值等
select
sum(字段) as 别名 #求和,且重新命名
avg(字段)
max(字段)
min(字段)
count()
from 表名
group by (字段) #分组 这个分组字段必须和select后面出现的字段一直,因为是在前面的分组语句之后进行的分组,所以分组字段可以是多个。
having ifnull(); #子查询,在前面的结果集中进行查询
> group by 字段 #这个分组是根据字段分组的,如:group by age 那如果在字段age中有男,女,保密,那出现的就只有三行,它只会出现男,女,保密,各出现一个,所有这个就是分组,所通俗一点,其实就是根据age进行去重,多余的都不会显示
8.子查询
where #这个查询只是限制查询的条件
having #这个是在分组之后又进行限制条件进行查询。相当于所以了范围
如:
select
name,age,
sum(age)
from stu
where age>18
group by age
having ifnull(sum(age),0)>20;