create table hw_studentinfo(
stu_no int(8) not null primary key auto_increment,
stu_name varchar(10) not null,
gender char(1),
age int,
birthday date,
class varchar(10),
course varchar(10),
score float
)
insert into hw_studentinfo
values(1,“王五”,“男”,10,“2010-05-05”,“JJA2001”,“Java”,70);
– 插入一条学生记录:学号2,姓名李四,性别女,年龄25,出生日期1997-01-05,班级名”JPH2001”,选修php
insert into hw_studentinfo
values(2,“李四”,“女”,25,“1997-01-05”,“JPH2001”,“php”,null);
insert into hw_studentinfo
values(3,“张三”,“男”,30,null,null,“php”,null);
insert into hw_studentinfo(stu_no,stu_name,gender,age,course)
values(4,“张三”,“男”,30,“php”);
delete from hw_studentinfo where stu_name = ‘王五’;
update hw_studentinfo set class = “JJA2002”;
update hw_studentinfo set class = “JPH2001” where stu_name = ‘张三’;
select * from hw_studentinfo;
select stu_name,age from hw_studentinfo;
select stu_no as studentNo,stu_name as studentName from hw_studentinfo;
select stu_name,age from hw_studentinfo where stu_name = ‘张三’;
select * from hw_studentinfo where class = ‘JJA2002’ or class = ‘JPH2001’;
select * from hw_studentinfo where class in(‘JJA2002’,‘JPH2001’);
select * from hw_studentinfo where class != ‘JJA2001’;
select * from hw_studentinfo where age between 20 and 23;
select * from hw_studentinfo where class is null;
select stu_name from hw_studentinfo where class = ‘JPH2001’ and gender = ‘女’;
select * from hw_studentinfo where stu_name like ‘王%’;
select * from hw_studentinfo where stu_name like ‘%强%’;
select * from hw_studentinfo order by age asc;
select * from hw_studentinfo where course = ‘Java’ order by score asc;
select * from hw_studentinfo where class = ‘JJA2001’ and course =‘java’
order by score desc,stu_no asc;
select SUM(age) from hw_studentinfo;
select AVG(age) from hw_studentinfo;
select MAX(age) from hw_studentinfo;
select MIN(age) from hw_studentinfo;
select class,count(*) from hw_studentinfo where class = ‘JJA2001’;
select DISTINCT class from hw_studentinfo;
select class from hw_studentinfo group by class;
select class,sum(score) from hw_studentinfo where class = ‘JJA2001’;
select class,avg(score) from hw_studentinfo where class = ‘JJA2001’;
select gender,count(*) from hw_studentinfo group by gender;
select course,count(*) from hw_studentinfo group by course;
select class,count(*) from hw_studentinfo group by class;
select class,count() from hw_studentinfo group by class having count()>3;
select course,avg(score) from hw_studentinfo group by course order by avg(score) desc;
select course,count() from hw_studentinfo
group by course
having count() > 3
order by count(*) desc;
select * from hw_studentinfo order by age desc limit 5;
按照Java分数从高到低显示前5个学生
select * from hw_studentinfo where course = ‘java’
order by score desc limit 5;
每一个课程的最高分
select course,max(score) from hw_studentinfo
group by course
having max(score)>=90
order by max(score) desc
limit 2;
select * from hw_studentinfo where course = ‘php’ order by score desc limit 2,1;
select * from hw_studentinfo where course = ‘JAVA’ order by score asc limit 1;
select * from hw_studentinfo where age = (select max(age) from hw_studentinfo);
select * from hw_studentinfo
where course = ‘java’
and score > (select avg(score) from hw_studentinfo where course = ‘java’)
select class,avg(score)
from hw_studentinfo
group by class
having avg(score) > (select avg(score) from hw_studentinfo)
select COUNT(*) from (
select class,avg(score)
from hw_studentinfo
group by class
having avg(score) > (select avg(score) from hw_studentinfo)
) as temp;