本帖最后由 武汉18期-胡樟 于 2022-8-6 20:53 编辑
单表查询
select * from +表名 查询表中所有数据
select name,class from 表名 取多个字段符
select * from 表名 where name='daliu'; 查询等于daliu的所有数据
select * from 表名 where name !=‘daliu’;查询所有不等于daliu的数据
select * from 表名 where name <>=‘daliu’;查询所有不等于daliu的数据
select *from 表名 where class=1001 and phone=13311111111; 查询
class为1001且phone为13311111111的所有数据
select *from 表名 where class=1001 or phone=13311111111; 查询class为1001或者phone为13311111111的所有数据
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 class in(1001,1002); 查询class 为1001或1002的数据
select * from 表名 where id not in(1,2);查询id不为1或者2的数据
select * from 表名 where class is null; 查询class为空数据,不能直接用字段
select * from 表名 where class is not null;查询class不为空数据
select * from 表名 where name like ”%li%“; 查询name字段所有包含li的数据
select * from 表名 where name like ”%li“; 查询name字段以li结尾的数据
select * from 表名 where name like ”li%“;查询name字段以li开头的数据
select * from表名 字段 limit 1,4; 查询2到5行的数据
select * from 表名 字段 limit 0,5;查询前5行数据
limit m,n (m为下标值,n为查询行数)下标值从0开始
排序
从小到大--升序 asc
select * from 表名 order by 字段 asc;
select * from user order by phone asc;
从大到小--降序 desc
select * from 表名 order by 字段 desc;
分组:where 不能放在group by后面
select * from 表名 group by class; 通过class字段进行分组
select class, count(*) from 表名 group by class; 通过class字段进行分组然后求出每组对应的人数
selct class,count(*) from 表名 group by class having class is not null;
通过class字段进行分组然后加条件class不为null
注意:group by分组之后,只能使用having进行条件筛选
使用group by分组后,仅有分组字段和函数可以放到from前面
聚合函数
count() 统计
sum 求和
avg 求平均值
max 求最大值
min 求最小值
distinct 去重
select count(*) from 表名 ; 统计表中数据
select sum(id) from 表名; 计算id字段数值之和
select avg(id) from 表名 where class=1001;求id的平均值
select max(id) from 表名 where class=1001;求id最大值
select minin(id) from 表名 where class=1001;求id最小值
select distinct(字段) from 表名;对字段去掉重复数据
|
|