单表查询
select*from lili==>查询所有的表数据
select name,class from lili;==》取多个字段值
select*from lili where !="xiao";==》查询nema不等于xiao的所有数据
select*from lili where name <>"shao";==》查询nema不等于xiao的所有数据
select*from lili where class=187 and phone=13111111111;==》查询class为187和phone为13111111111的所有数据
select*from lili where class=189 or phone=13111111111;==》查询class为189或phone为13111111111的所有数据
select*from lili where id>4;==》查询id大于4的数据
select*from lili where id>=3 and id<=6;==》查询id大于等于3并且小于等于6的数据
select*from lili where id between 3 and 6;==》查询id大于等于3并且小于等于6的数据
select*from lili where class in(188,189);==》查询class为189或188的数据
select*from lili where class=189 or class=188;==》查询class为189或188的数据
select*from lili where id not in(1,5);==》查询id不是1或5的数据
select*from lili where class is null;==》查询class为空的数据,不能直接用字段=null
select*from lili where class is not null;==》查询class不为空的数据
select*from lili where name like "%ao%";==》查询name字段所有包含ao的数据
select*from lili where name like "x%";==》查询name字段以x开头的所有数据
select*from lili where name like "%o";==》查询name字段以o结尾的所有数据
select*from lili id limit 1,4;==》查询2-5行数据
select*from lili id limit 0,5;==》查询前5行数据
limir m,n(m为下标值n为查询行数)下标值从0开始
排序
从小到大==》升序,asc
selenct*from +表名 order by 字段 asc;
select*from lili order by phone asc;==》对phone字段从小到大排序
从大到小==》降序,desc
select*from +表名 order by 字段 desc
select*from lili ortder by phone desc:==>对phone字段从大到小排序
分组 (涉及到每个时都需要分组)
select*from lili group by class;==》对class字段进行分组
select class,count(*) from lili group by class;==》对class字段进行分组,求出每组的人数
select*from lili group by class having class is not null;==》对class字段进行分组,加条件class不为null的
注意:
1使用.group by分组之后,只能用having进行条件筛选
2.使用group by分组之后,只有分组字段和函数可以放到from前面
聚合函数
count()==》统计
sum()==》求和
avg()==》求平均值
max()==》求最大值
min()==》求最小值
distinct==》去重
select count(*) from lili;==》统计lili表中数据条数
select sum(id) from lili;==》计算id字段数值之和
select avg(id) from lili;==》求id字段的平均值
select max(id) from lili;==》求id字段的最大值
select min(id) from lili;==》求id字段的最小值
select distinct(phone) from lili;==》去掉phone字段重复的数据
|
|