create table student(
id int(4) primary key ,
age int(8),
sex int(4),
name varchar(20),
class int(4),
math int(4)
)engine=innodb;
insert into student values
(1,25,1,'zhangsan',1833,90),
(2,25,1,'lisi',1833,67),
(3,28,0,'wangwu',1833,79),
(4,35,1,'xiaoliu',1835,96),
(5,27,0,'xiaoli',1833,86),
(6,32,1,'xiaochen',1835,48),
(7,22,1,'wangwu',1834,70),
(8,31,0,'xiaoqi',1825,88),
(9,27,0,'xiaoqi',1833,74),
(10,27,1,'niuqi',null,80);
select * from student ;
表格:
1、查询1833班信息的2-4行的数据
结果:* 所有数据
条件:class=1833 ,limit 1,3
方法1:select * from student where class="1833" limit 1,3
2、显示班级为空的id和姓名、和数学分数
结果:id ,name,math
条件:class is null
方法:select id,name,math from student where class is null;
3、统计每个班级人数
结果:count(name)
条件:count(name) ,group by class
方法:SELECT count(id),class from student group by class;
4、在1833班数学成绩最高的ID年龄和姓名
结果:id ,age、name
条件:class=1833 , max(math)
语句:有缺陷写法
select id,age,name,math from student where class="1833" order by math desc limit 0,1; ( 存在重复最高分数只显示一个)
方法2:
select id,age,name from student where class="1833" and math=(select max(math) from student where class="1833")
5、求数学分最小的班级 ID年龄和姓名
结果:id 、age、name、class
条件:min(math)
方法:select class,id,age,name,math from student where math=(select min(math) from student);
6、求1833班数学分总和
结果:sum(math)
条件:class=1833,sum(math)
方法:select class,sum(math) from student where class = 1833 ;
7、求所有班级分数总和
结果:sum(math)
条件:
方法:select sum(math) from student ;
理解成每个班级:分数总和
select class,sum(math) from student group by class ;
8、求年纪最大的班级并显示班级年龄和姓名分数
结果:class、age、name、math
条件:max(age)
方法:select class,age,name,math from student where age=(select max(age)from student );
9、统计sex1和0个总数
结果:sex,count(*)
条件:group by sex
方法:select sex,count(sex) from student group by sex;
10、求出所有班级年纪平均数
结果:avg(age)
方法:select avg(age) from student;
11、求出1835班年纪的平均数
结果:avg(age)
条件:class=1835
方法:select avg(age) from student where class=1835;
12、求出1833班年纪的平均数
结果:avg(age)
条件:class=1833
方法:select avg(age) from student where class=1833;
13、将所有数据按照年纪进行降序后显示年纪姓名和班级
结果: age、name、class
条件:order by age desc
方法:select age,name,class from student order by age desc;
14、将所有数据按照年纪升序显示年纪姓名班级和数学分数
结果:age、class、name、math
条件:order by asc
方法:select age,name,class,math from student order by age age;
15、按照班级将进行分组
结果:* 显示所有信息
条件:group by class
方法:select * from student group by class ;
16、根据age字段进行降序排序;
结果: 显示所有信息 *
条件:order by age desc
方法:select * from student order by age desc ;
17、根据math字段进行升序排序,并显示前5行所有数据;
结果:* 所有数据
条件:order by math asc limit 0,5
方法:select * from student order by math asc limit 0,5;
18、把lisi的数学成绩改为69分
条件:updata math=69
方法:update student set math=69 where name='lisi';
19、查找性别不为1的所有数据
结果:显示所有的数据 *
条件:sex<>1 ,sex!=1 ,not in(1)
方法:
20、只显示表中姓名,且将相同的姓名名称去重
结果: name
条件:distinct
方法:select distinct(name) from student;
21、统计表中行数
22、统计年纪在27岁的有多少
23、统计年纪大于25小于35的有多少
24、求数学分总和
25、求分数最小
26、求平均分
27、只显示3-8行的数据
28、查找姓名尾号为qi的所有数据
29、查询姓名开头为xiao的所有数据
30、查询中间值为ao开头的所有数据
标签:__,SIR,name,age,12.2,select,student,class,math From: https://www.cnblogs.com/xiaolehong/p/18054290