前言
基础查询、条件查询(where)、分组查询(group by)、排序查询(order by)、分页查询(limit)
“stu”为该数据库的表名
目录
一、查询语句
1.查询所有数据
select distinct 列名 form stu; ps:distinct用于消除重复查询结果
2.模糊查询
通配符:
1. -;代表当个任意字符
2. %;代表任意个数字符
举例子:
1.查询姓马的同学
select * from stu where name like ‘马%’;
2.查询第二个字是‘花’的同学
select * from stu where name like ‘-花%’;
3.查询名字中含有‘德’的同学
select * from stu where name ‘%德%’;
3.排序查询
asc:升序排序;desc:降序排序;
select * from 表名 order by 排序字段名 asc/desk;
4.分组查询
select 字段列表 from stu 【where 分段前条件限定】group by 分段字段名 【having 分组后条件过滤】
select sex,avg(math),count(*) from Stu where math >70 group by sex having count(*) >2
ps:avg指平均函数,count(*)指查询结果。
5.分页查询
select 字段列表 from 表名 limit 起始索引,查询条目数;
起始索引=(当前页码-1)*每页显示的条数
二、其他语句
1.删除数据语句
delete form stu 【where 条件】
2.添加数据语句
1.给指定的列添加数据
insert into stu(ID,name) values (1,’张三’);
2.给全部列添加数据
insert into stu values (值1,值2,….)
3.批量的添加数据
insert into stu(列名1,列名2,….)values (值1,值2,….)
3.修改数据语句
update stu set 列名=值1 where name = ‘张三’