练习24.10.26-02

drop table if exists xsb1


create table xsb1 (
    id int(4) primary key,
    age int(8),
    sex int(4),
    name varchar(20),
    class int(4),
    math int(4)
);

show tables;

insert into xsb1 values
(1,25,1,'zhangsan',1833,90),
(2,25,1,'lisi',1833,67),
(3,28,0,'xiaowang',1835,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 xsb1

-- 1、查询1833班信息的2,4行的数据
select * from xsb1
where class = 1833 limit 1,3

-- 2、显示班级为空的id和姓名、和数学分数
select id,name,math from xsb1
where class is null 

-- 3、统计每个班级人数
select class 班级,count(name) 班级人数  from xsb1
group by class

-- 4、最1833班数学成绩最大的ID年龄和姓名
select  id,age,name  from xsb1
where class = 1833 
order by math desc  limit 1


-- 5、求数学分最小的班级 ID年龄和姓名
select  class,id,age,name  from xsb1
order by math asc  limit 1

-- 6、求1833班数学分总和
select sum(math) 1833班数学分总和 from xsb1
where class = 1833 

-- 7、求所有班级分数总和
select sum(math) 所有班数学分总和 from xsb1

-- 8、求年纪最大的班级并显示班级年龄和姓名分数

select class 年纪最大的班级,age 年龄,name 姓名,math 分数 from xsb1
where age = (select max(age) from xsb1)


select class 年纪最大的班级,age 年龄,name 姓名,math 分数 from xsb1
order by  age desc limit 1

-- 9、统计sex1和0个总数
select sex 性别,count(sex) 性别数量 from xsb1
group by  sex


-- 10、求出所有班级年纪平均数
select class 班级, round(avg(age),0) 班级平均年龄 from xsb1
group by class

-- 11、求出1835班年纪的平均数
select class 班级, round(avg(age),0) 班级平均年龄 from xsb1
where  class = 1835


-- 12、求出1833班年纪的平均数
select class 班级, round(avg(age),0) 班级平均年龄 from xsb1
where  class = 1833


-- 13、将所有数据按照年纪进行降序后显示年纪姓名和班级
select age 年纪,name 姓名,class 班级 from xsb1
order by age desc

-- 14、将所有数据按照年纪升序显示年纪姓名班级和数学分数
select age 年纪,name 姓名,class 班级 ,math 数学分数 from xsb1
order by age asc

-- 15、按照班级将进行分组
select class from xsb1 
group by class 

-- 16、根据age字段进行降序排序;

select * from xsb1
order by age desc 

-- 17、根据math字段进行升序排序,并显示前5行所有数据;
select * from xsb1
order by math asc  limit 5

-- 18、把lisi的数学成绩改为69分
update xsb1 set math = 69  where name = 'lisi'

-- 19、查找性别不为1的所有数据

select * from xsb1
where sex != 1
-- 20、只显示表中姓名,且将相同的姓名名称去重
select distinct name  from xsb1

-- 21、统计表中行数
select count(*) from xsb1
-- 22、统计年纪在27岁的有多少
select count(*) from xsb1
where age = 27
-- 23、统计年纪大于25小于35的有多少
select * from xsb1
where age > 25 and age < 35
-- 24、求数学分总和
select sum(math) from xsb1

-- 25、求分数最小
select min(math) 最小分 from xsb1
-- 26、求平均分
select avg(math) 平均分 from xsb1
-- 27、只显示3-8行的数据
select * from xsb1
limit 2,6
-- 28、查找姓名尾号为qi的所有数据
select * from xsb1
where name like "%qi"
-- 29、查询姓名开头为xiao的所有数据
select * from xsb1
where name like "xiao%"
-- 30、查询中间值为ao开头的所有数据
select * from xsb1
where name like "%ao%"

猜你喜欢

转载自blog.csdn.net/qq_42217078/article/details/143308477