select * from 表名 查询表中所有的数据
select * from 表名 where class=1001; 查询所有班级为1001的数据 查询单个字段
select name from tb_user where class=1001; ==》查询单个字段
select name,class,from 表名 where class=1001;查询多个字段 显示所有class为1001的name和class这两个字段
select * from 表名 where name !=‘daliu' ; 查询name不等于daliu的数据
select * from 表名 where name<>‘daliu' 查询name不等于daliu的数据
select * from 表名 where class=1001 and phone=13311111111; 查询class为1001且phone为1331111111的所有数据
select * from 表名 where class=1001 or phone=13311111111; 查询class为1001或者phone为1331111111的所有数据
select * from 表名 where class in(1001,1002); 查询class为1001或者1002的数据
select * from 表名 where id not in (1,2); 查询id不为1或者2的数据
select * from 表名 where id>4; 查询id>4的所有数据
select * from 表名 where id>=3 and id<=6; 查询id大于等于3并且小于等于6的数据
select * from 表名 where id between 3 and 6; 查询id在3到6之间的数据
select * from 表名 where 字段 is null; 查询默认为空的数据 默认显示null 查询null
select * from 表名 where class is null;
select * from 表名 where 字段 is not null; 查询不为空的数据
select * from 表名 where class is not null;
select*from xiaoxu where name like "%xu%"; 查询name字段所有包含xu的数据
select*from xiaoxu where name like "%xu"; 查询name字段以xu结尾的所有数据
select*from xiaoxu where name like "xu%"; 查询name字段以xu开头的所有数据
select*from xiaoxu sid limit 1,4; 查询2到5行的数据
select*from xiaoxu sid limit 0,5; 查询前5行的数据
limit m,n (m为下标值,n为查询行数),下标值从0开始
select*from xiaoxu group by class; 通过class字段进行分组
select class,count(*) from xiaoxu group by class; 通过class字段进行分组然后求出每组对应的人数
select class,count(*) from xiaoxu group by class having class is not null; 通过class字段进行分组然后加条件class不为null
注意:group by 分组之后只能使用having进行条件筛选
使用group by 分组之后,仅有分组字段和函数可以放到from前面
排序 order by
从小到大 select*from xiaoxu order by phone asc; 对phone进行从小到大的排序
从大到小 select*from xiaoxu order by phone desc; 对phone进行从大到小的排序
聚合函数
count() 统计 select count(*) from xiaoxu; 统计xiaoxu表中数据的条数
sum() 求和 select sum(sid) from xiaoxu; 对sid字段数值进行求和
avg() 求平均值 select avg(id) from xiaoxu; 求id的平均值
max() 求最大值 select max(id) from xiaoxu where class=1001; 对class为1001的数据取id最大值
min() 求最小值 select min(id) from xiaoxu where class=1001; 对class为1001的数据取id最小值
distinct 去重 select distinct(class) from xiaoxu; 去掉class里重复的数据
|
|