mysql查询
模糊查询 like 与 not like
show tables like '%模糊查询%';
mysql模糊查询like的用法
查询user表中姓名中有“王”字的:
select * from user where name like '%王%'
mysql模糊查询not like的用法
查询user表中姓名中没有“王”字的:
select * from user where name not like '%王%'
查询user表中地址在上海姓名中没有“王”字和所有姓名为空的:
select * from user where adress =‘上海’ and name not like '%王%' or name is null
查询user表中地址在上海姓名中没有“王”字和地址在上海姓名为空的:
select * from user where adress =‘上海’ and (name not like '%王%' or name is null)
排序 order by:
order by 字段名 DESC; 按照字段名降序排序
order by 字段名 ASC; 按照字段名升序排序
截取数据limit:
select * from tablename limit 2,4
取出第3条至第6条,4条记录
and 且
select * from student where date>'1988-1-2' and date<'1988-12-1';
or 或
select * from student where date<'1988-11-2' or date>'1988-12-1';
between
select * from student where date between '1988-1-2' and '1988-12-1';
in 查询制定集合内的数据
select * from student where id in (1,3,5);
聚合函数
- - -也就是组函数
在一个行的集合(一组行)上进行操作,对每个组给一个结果
AVG([distinct] expr) | 求平均值 |
---|---|
COUNT({*[distinct] } expr) | 统计行的数量 |
MAX([distinct] expr) | 求最大值 |
MIN([distinct] expr) | 求最小值 |
SUM([distinct] expr) | 求累加和 |
select max(id),name,sex from student group by sex;
select min(date) from student;
select avg(id) as '求平均' from student;
select count(*) from student; #统计表中总数
select count(sex) from student; #统计表中性别总数 若有一条数据中sex为空的话,就不予以统计~
select sum(id) from student;