1. 程式人生 > >PHP數據庫45道題整理~~~啦啦啦

PHP數據庫45道題整理~~~啦啦啦

order by 職稱 avg tab sel 課程 har sele 最大值

select * FROM student
-- 二:查詢student表中所有記錄
select * FROM score WHERE Degree>60 AND Degree<80
-- 四:查詢Score表中成績在60到80之間的所有記錄
SELECT * FROM score WHERE Degree in (‘85‘,‘86‘,‘88‘);
-- 五:查詢Score表中成績為85,86或88的記錄。
SELECT * FROM student WHERE Class = ‘95031‘ AND Ssex = ‘0‘;
-- 查詢Student表中“95031”班或性別為“女”的同學記錄。

SELECT * FROM student ORDER BY Class;
-- 以Class降序查詢Student表的所有記錄。
SELECT SUM(class = 95031) FROM student;
-- 查詢“95031”班的學生人數。、
SELECT avg(Degree) FROM score;
-- 查詢“95033”班學生的平均分。
SELECT Cno FROM score GROUP BY Cno
-- 先分組
SELECT avg(Degree) FROM score GROUP BY Cno
-- 然後用平均查詢每門課的平均成績。select
SELECT * FROM score where Cno = 3-105 AND Degree > 109
-- 19、 查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。
select distinct Depart from Teacher
-- 查詢教師所有的單位即不重復的Depart列。
select Cno,avg(degree) from Score where cno like ‘3%‘ group by Cno having count(Cno)>5;
-- 12、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
select Sno from score where Degree >70 and Degree<90
-- 13、查詢分數大於70,小於90的Sno列。


SELECT * FROM student QWE JOIN score ZXC ON QWE.Sno=ZXC.Sno;
-- 合並表格,將student和score進行合並 左右連接在join前面加上left or RIGHT;

create table grade(low int(3),upp int(3),rank char(1))
insert into grade values(90,100,‘A‘)
insert into grade values(80,89,‘B‘)
insert into grade values(70,79,‘C‘)
insert into grade values(60,69,‘D‘)
insert into grade values(0,59,‘E‘)
-- 簡歷grade表格

SELECT * FROM score WHERE Sno = 109 AND Cno = 3-105;


-- 以下
-- 41、查詢“男”教師及其所上的課程。

select Cname from course where Tno in (select Tno from teacher where Tsex=‘1‘);

select Tname,Cname from course,teacher where course.Tno = teacher.Tno and teacher Tsex=‘1‘;
select Tname,Cname from course,teacher where course.Tno=teacher.Tno and teacher.Tsex=‘1‘;

-- 44、查詢和“李軍”同性別並同班的同學Sname.
select sname from Student where Ssex=(select Ssex from Student where Sname=‘李軍‘)and class=(select Class from Student where Sname=‘李軍‘)

14、查詢所有學生的Sname、Cno和Degree列。

select Sname,Cno,degree from score join student on score.sno =student.sno ---連接查詢

15、查詢所有學生的Sno、Cname和Degree列。

select Sname,Sno,degree from Score join course on Score.Cno =Course.Cno

16、查詢所有學生的Sname、Cname和Degree列。

select cname,sname,Degree from score join student on student.sno=score.sno join course on score.cno=course.cno

select cname,sname,degree from score ,student,course where student.sno=score.sno and score.cno=course.cno



-- 19、現查詢所有同學的Sno、Cno和rank列。

select sno,cno,rank from score ,grade where score.degree between low and upp

-- 20、查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。

select * from score where cno=‘3-105‘ and degree>(select degree from score where sno=‘109‘ and cno=‘3-105‘)

select * from score where cno=‘3-105‘ and degree >(select max(degree) from score where sno=’109’)

-- 21、查詢score中選學多門課程的同學中分數為非最高分成績的記錄。

select * from score where sno in (select sno from score group by sno having count(*)>1) and degree <(select max(degree) from score )

select * from score a where sno in (select sno from score group by sno having count(*)>1) and degree <(select max(degree) from score b where b.cno=a.cno )

-- 22、查詢成績高於學號為“109”、課程號為“3-105”的成績的所有記錄。

select * from score where cno=‘3-105‘ and degree>(select degree from score where sno=‘109‘ and cno=‘3-105‘)

-- 23、查詢和學號為108的同學同年出生的所有學生的Sno、Sname和Sbirthday列。

select * from student where year(sbirthdy)=(select year(sbirthday) from student where sno=‘108‘)

-- 24、查詢“張旭“教師任課的學生成績。

select Degree from Score where Cno in (select Cno from Course where Tno in (select Tno from Teacher where Tname=‘張旭‘))

-- 25、查詢選修某課程的同學人數多於5人的教師姓名。

select tname from teacher where tno in (select tno from Course where Cno in (select Cno from Score group by Cno having COUNT(*)>5))

-- 26、查詢95033班和95031班全體學生的記錄。

select * from Student where Class=‘95033‘ or class=‘95031‘

select * from student where class in(‘95033‘,‘95031‘)



-- 27、查詢存在有85分以上成績的課程Cno.

select distinct cno from Score where Degree>85

-- 28、查詢出“計算機系“教師所教課程的成績表。

select * from Score where Cno in(select Cno from Course where Tno in(select tno from Teacher where Depart=‘計算機系‘))

-- 29、查詢“計算機系”與“電子工程系“不同職稱的教師的Tname和Prof。

select tname,prof from Teacher where Depart= ‘計算機系‘ and prof not in(select prof from Teacher where Depart=‘電子工程系‘)

union

select tname,prof from Teacher where Depart= ‘電子工程系‘ and prof not in(select prof from Teacher where Depart=‘計算機系‘)

select tname,prof from teacher a where prof not in (select prof from teacher b where b.depart!=a.depart)

-- 30、查詢選修編號為“3-105“課程且成績至少高於選修編號為“3-245”的同學的Cno、Sno和Degree,並按Degree從高到低次序排序。

select * from Score where cno=‘3-105‘ and degree>any(select degree from Score where cno=‘3-245‘)order by degree desc——any其中任何一個、all所有



-- 31、查詢選修編號為“3-105”且成績高於選修編號為“3-245”課程的同學的Cno、Sno和Degree.

select Cno,Sno,Degree from Score where cno=‘3-105‘ and degree>(select max(degree) from Score where cno=‘3-245‘)

-- 32、查詢所有教師和同學的name、sex和birthday.

select tname,tsex,tbirthday from Teacher


-- 二:
select Sname,Ssex,Sbirthday from Student

-- 33、查詢所有“女”教師和“女”同學的name、sex和birthday.
-- student

select Sname,Ssex,Sbirthday from Student where Ssex=‘0‘;


-- teacher
select tname,tsex,tbirthday from Teacher where Tsex=‘0‘;

-- 34、查詢成績比該課程平均成績低的同學的成績表。

Select * from score a where degree<(select avg(degree) from score b where a.cno=b.cno);

-- 35、 查詢所有任課教師的Tname和Depart.

Select tname,depart from teacher where tno in (select tno from course where cno in (select cno from score group by cno))

Select tname,depart from teacher where tno in (select tno from course where cno in (select distinct cno from score))
-- 36、查詢所有未講課的教師的Tname和Depart.

Select tname,depart from teacher where tno in (select tno from course where cno not in (select distinct cno from score))

-- 37、查詢至少有2名男生的班號。
select class from student where Ssex=‘1‘ group by Class having count(*) >1;

-- 38、查詢Student表中不姓“王”的同學記錄。

select sname from Student where Sname not like ‘王%‘;


-- 39題不會寫,重點在年,year;

-- 39、查詢Student表中最大和最小的Sbirthday日期值。

select MAX(sbirthday) as ‘最大值‘,MIN(sbirthday)‘最小值‘ from Student

-- 40、以班號和年齡從大到小的順序查詢Student表中的全部記錄。

select class,sbirthday from Student order by Class desc,Sbirthday asc

-- 41、查詢“男”教師及其所上的課程。

select cname from course where tno in(select tno from teacher where tsex=’男’)

select tname,cname from teacher ,course where teacher.tno=course.tno and tsex=‘男‘

-- 42、查詢最高分同學的Sno、Cno和Degree列。

(1)select * from score where Degree = (select max(Degree) from score)

(2)select top 1 * from score order by degree desc

-- 43、查詢和“李軍”同性別的所有同學的Sname.

select sname from Student where Ssex=(select Ssex from Student where Sname=‘李軍‘)

-- 44、查詢和“李軍”同性別並同班的同學Sname.

select sname from Student where Ssex=(select Ssex from Student where Sname=‘李軍‘)and class=(select Class from Student where Sname=‘李軍‘);

-- 45、查詢所有選修“計算機導論”課程的“男”同學的成績表。

select Degree from score where Sno in(select Sno from student where Ssex=‘1‘) and Cno in (select Cno from course where Cname = ‘計算機導論‘);

PHP數據庫45道題整理~~~啦啦啦