
本文最后更新于 779 天前,其中的信息可能已经有所发展或是发生改变。
Access denied for user 'root'@'%' to database 'xxx'grant all on xxx.* to 'root'@'%' identified by 'password' with grant option;Student(SId,Sname,Sage,Ssex) --SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-12-20' , '男');
insert into Student values('04' , '李云' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-01-01' , '女');
insert into Student values('07' , '郑竹' , '1989-01-01' , '女');
insert into Student values('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2012-06-06' , '女');
insert into Student values('12' , '赵六' , '2013-06-13' , '女');
insert into Student values('13' , '孙七' , '2014-06-01' , '女');Course(CId,Cname,TId) --CId 课程编号,Cname 课程名称,TId 教师编号
create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');Teacher(TId,Tname) --TId 教师编号,Tname 教师姓名
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');SC(SId,CId,score) --SId 学生编号,CId 课程编号,score 分数
create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);select Student.*,01Score,02Score
from Student,
(select tb1.SId,01Score,02Score
from
(select SId,score as 01Score
from sc
where CId='01')as tb1,
(select SId,score as 02Score
from sc
where CId='02')as tb2
where tb1.SId=tb2.SId and 01Score>02Score)as tb3
where tb3.SId=Student.SIdSELECT Student.*
FROM Student JOIN
(SELECT tb1.SId
FROM
(SELECT SId
FROM sc
WHERE CId='01') as tb1 join
(SELECT SId
FROM sc
WHERE CId='01') as tb2 on tb1.SId=tb2.SId) AS tb3
ON tb3.SId=Student.SId可能不存在???“可能”可以作为条件吗?
SELECT *
FROM sc
where SId not in(SELECT SId
from sc
where CId='01')
and CId='02' select Student.Sname,tb1.*
from Student join
(select avg(score) as avg_score,SId
from sc
group by SId
HAVING avg(score)>60) as tb1
on Student.SId=tb1.SId select distinct Student.*
from Student JOIN
sc on Student.SId=sc.SIdSELECT Student.Sname,Student.SId,ifnull(tb1.class_num,0),tb1.class_score
from Student left join
(select SId,count(1) as class_num,sum(score)as class_score
from sc
group by SId)as tb1
on Student.SId=tb1.SIdselect Student.*
from Student
where Student.SId in
(select SId
from sc)select count(1)
from Teacher
where Tname like'李%'select Student.*
from Student join
(select SId
from sc
where CId=
(select CId
from Course
where TId=
(select TId
from Teacher
where Tname like '张三')))as tb1
on Student.SId=tb1.SId select student.* from student,teacher,course,sc
where
student.sid = sc.sid
and course.cid=sc.cid
and course.tid = teacher.tid
and tname = '张三'; select sid
from sc
group by sid
HAVING count(1)<
(select count(1)as class_num
from Course)select * from student
where student.sid not in (
select sc.sid from sc
group by sc.sid
having count(sc.cid)= (select count(cid) from course)
); select * from student
where student.sid in (
select sc.sid from sc
where sc.cid in(
select sc.cid from sc
where sc.sid = '01'
)
); select distinct student.*
from sc,student
where cid in(
select cid
from sc
where sid='01')
and sc.sid=student.sidSELECT sid
from sc
where cid in(
SELECT cid
from sc
where sid='01')
and sid <> '01'
group by sid
having count(1)=(
select count(1)
from sc
where sid='01')select Student.*
from Student
where SId not in(
select sc.sid
from sc,teacher,course
where tname = '张三'
and teacher.tid=course.tid
and course.cid=sc.cid)select student.sid,avg(score),sname
from sc,student
where score<60
and student.SId=sc.sid
group by sid
having count(1)>1select student.*,score
from sc,student
where cid='01'
and student.sid=sc.sid
and score<60
order by score desc
select student.sid,student.sname,tb1.cname,tb1.score,tb2.cname,tb2.score,tb3.cname,tb3.score,tb4.avg_score
from student
left join
(select sc.sid,cname,score
from sc,course
where sc.cid=course.cid
and course.cname='语文')as tb1
on student.sid=tb1.sid
left join
(select sc.sid,cname,score
from sc,course
where sc.cid=course.cid
and course.cname='数学')as tb2
on student.sid=tb2.sid
left join
(select sc.sid,cname,score
from sc,course
where sc.cid=course.cid
and course.cname='英语')as tb3
on student.sid=tb3.sid
left JOIN
(select sid,avg(score) as avg_score
from sc
group by sid) as tb4
on student.sid=tb4.sid
order by tb4.avg_score descselect cname,max(score),min(score),avg(score)
from sc,course
where sc.cid=course.cid
group by sc.cidsum,case when求及格率,中等率,优良率,优秀率select cname,max(score),min(score),avg(score),
sum(case when sc.score>=60 then 1 else 0 end)/count(1) as 及格率,
sum(case when sc.score>=70 and sc.score<80 then 1 else 0 end)/count(1) as 中等率,
sum(case when sc.score>=80 and sc.score<90 then 1 else 0 end)/count(1) as 优良率,
sum(case when sc.score>=90 then 1 else 0 end)/count(1) as 优秀率
from sc,course
where sc.cid=course.cid
group by sc.cidselect cid,count(1)
from sc
group by cid
order by COUNT(1) desc,cidselect tb1.cid,tb1.sid,tb1.score,count(tb2.cid) as rank
from sc as tb1 left join sc as tb2 on tb1.score<tb2.score and tb1.cid=tb2.cid
group by tb1.cid,tb1.sid
order by cid,rank asc;15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次
set @r=0;
set @old=-1;
select Sname,tb1.total,tb1.rank
from(
select tb.*,if(@old<>tb.total,@r:=@r+1,@r)as rank,@old:=tb.total
from (
select sc.sid,sum(sc.score) as total
from sc
group by sc.sid
order by total desc)as tb)as tb1,student
where student.SId=tb1.sid
ORDER BY tb1.rank16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
sum,case when求各分数段的人数以及百分比select cname,tb.*
from course,(
select sc.cid,
sum(case when score<60 then 1 else 0 end)as '0-60',
sum(case when score<60 then 1 else 0 end)/count(1)*100 as '0-60百分比',
sum(case when score<70 and score>=60 then 1 else 0 end)as '60-70',
sum(case when score<70 and score>=60 then 1 else 0 end)/count(1)*100 as '60-70百分比',
sum(case when score<85 and score>=70 then 1 else 0 end)as '70-85',
sum(case when score<85 and score>=70 then 1 else 0 end)/count(1)*100 as '70-85百分比',
sum(case when score<=100 and score>=85 then 1 else 0 end)as '85-100',
sum(case when score<=100 and score>=85 then 1 else 0 end)/count(1)*100 as '85-100百分比'
from sc
group by sc.cid)as tb
where tb.cid=course.cidwhere,count找前三名select sc.*
from sc
where (
select count(1)
from sc as tb
where sc.cid=tb.cid and sc.score<tb.score
)<3
order by sc.cid,sc.score descselect a.sid,a.cid,a.score from sc a
left join sc b on a.cid = b.cid and a.score<b.score
group by a.cid, a.sid
having count(b.cid)<3
order by a.cid;select cid, count(sid)
from sc
group by cid;select sname, count(cid)
from sc ,student
where sc.sid=student.sid
group by sc.sid
having count(cid)=2;count(表达式)select count(Ssex='男')as '男',count(Ssex='女') as '女'
from studentselect *
from student
where sname like '%风%'group byselect student.*,tb.count
from student,(
select sname,count(1)as count
from student
group by sname
having count(1)>1)as tb
where student.sname =tb.snameselect *
from student
where Sage between '1990-0-0 00:00:00' and '1991-0-0 00:00:00'select cid,avg(score)
from sc
group by cid
order by avg(score) desc,cidselect sname,student.sid,avg(score)
from sc,student
where sc.sid=student.sid
group by sc.sidselect sname,score
from student,sc,course
where cname='数学'
and student.sid=sc.sid
and course.cid=sc.cid
and score<60joinselect sname,cname,score
from student left join sc on student.sid=sc.sid left join course on course.cid=sc.cidselect sname,cname,score
from course left join sc on course.cid=sc.cid left join student on student.sid=sc.sid
where score>70select sname,cname,score
from course left join sc on course.cid=sc.cid left join student on student.sid=sc.sid
where score<60select sc.sid,sname,score
from sc,student
where sc.sid=student.sid
and sc.cid = '01'
and score >80select cname,count(sid)
from sc,course
where sc.cid=course.cid
group by sc.cidlimitselect sname,score
from teacher,sc,student,course
where tname='张三'
and teacher.tid=course.tid
and course.cid=sc.cid
and student.sid=sc.sid
order by score desc
limit 1select sc.*
from sc,(
select score,sid
from sc
group by score,sid
having count(score)>1 ) as tb
where sc.score=tb.score and sc.sid=tb.sidselect sc.cid,count(1)
from course left join sc on course.cid=sc.cid
group by sc.cid
having count(1)>5select sid
from sc
group by sid
having count(1)>=2select sid
from sc
group by sid
having count(1)=(select count(1) from course)select *,TIMESTAMPDIFF(year,Sage,current_date) as age
from studentWEEKOFYEARselect *
from student
where WEEKOFYEAR(student.Sage)=WEEKOFYEAR(CURDATE());select *
from student
where WEEKOFYEAR(student.Sage)=WEEKOFYEAR(CURDATE())+1;MONTHselect *
from student
where MONTH(student.Sage)=MONTH(CURDATE());select *
from student
where MONTH(student.Sage)=MONTH(CURDATE())+1;Post Views: 429