1. 程式人生 > >MySQL復雜用法

MySQL復雜用法

建立 姓名 排序 create 針對 ont sql birt 至少

  今天學了幾個MySQL的復雜用法,具體叫什麽名字已經記不太清了=-= 但是語法還是記住了。感覺今天學的語法並不是最重要的 ,邏輯才是。只要邏輯理清了 像是一個問題 分為幾步 找到哪是第一步,哪是第二步,哪是第三步等 剩下的就是把每一步的語句拼湊起來。不過我感覺這是個有點笨辦法,不過個人感覺比較清晰,畢竟還沒有那麽強的邏輯能力 不像一些大神直接一步搞定了。

  明天要寫需求文檔了,感覺略顯蛋疼。身為一個標準宅男,溝通能力實在堪憂。不知道該問一些什麽問題,略顯蛋疼,還有有時候問的太細會不會被客戶按在地上摩擦摩擦啊 囧。

  說一下目前對需求文檔的理解吧:

  主要是針對客戶,確定客戶的具體需求。

  業務需求概述:為什麽公司開發一個什麽類型的能幹什麽的軟件。

  功能說明:這個軟件都能做些什麽(大體)。

  業務流程:這個軟件都能做什麽(具體)。

  目前就只能理解到這裏,也不知道有沒有誤入歧途=-=。

  剩下的拿習題頂了......

1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。

Select sname, ssex, class from student;

2、 查詢教師所有的單位即不重復的Depart列。

select distinct depart from teacher;

3、 查詢Student表的所有記錄。

Select * from student;

4、 查詢Score表中成績在60到80之間的所有記錄。

Select * from score where degree between 60 and 80;

5、 查詢Score表中成績為85,86或88的記錄。

Select * from score where degree = 85 or degree = 86 or degree = 88;

6、 查詢Student表中“95031”班或性別為“女”的同學記錄。

Select * from student where class = “95031” or ssex = “女”;

7、 Class降序查詢Student表的所有記錄。

select * from student order by class;

8、 Cno升序、Degree降序查詢Score表的所有記錄。

select * from score order by cno ,degree desc;

9、 查詢“95031”班的學生人數。

select count(*) from student where class = "95031";

10、 查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)

select sno, cno from score where degree = (select max(degree) from score);

11、 查詢每門課的平均成績。

select cno, avg(degree) from score group by cno;

12、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。

select avg(degree) from score where cno in (select cno from score group by cno having count(cno) > 5) and cno like "3%";

13、查詢分數大於70,小於90的Sno列。

select sno from score where degree > 70 and degree < 90;

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

select a.sname, b.cno, b.degree from student a, score b where a.sno = b.sno;

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

select a.sno, a.degree, b. cname from score a, course b where a.cno = b.cno;

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

select a.sname, b.cname, c.degree from student a, course b, score c where a.sno = c.sno and b.cno = c.cno;

17、 查詢“95033”班學生的平均分。

select avg(degree) from score where sno in (select sno from student where class = "95033");

18、 假設使用如下命令建立了一個grade表:

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’)

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

select a.sno, a.cno, b.rank from score a

-> join grade b

-> on a.degree >= b.low and a.degree <= b.upp;

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

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

select * from score where degree not in (select max(degree) from score where cno in (select cno from score group by cno having(cno) > 1));

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

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

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

select sno, sname, sbirthday from student where year(sbirthday) = (select year(sbirthday) from student where sno = 108);

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

select * from score where cno = (select cno from course where tno = (select tno from teacher where tname = "張旭"));

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

select tname from teacher where tno = (select tno from course where cno = (select cno from score group by cno having count(cno) > 5));

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

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

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

select cno from score where degree > 85;

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

select * from score where cno in (select cno from course where tno in (select tno from teacher where depart = "計算機系"));

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

select tname, prof from teacher where depart in ("計算機系","電子工程系") and prof in (select prof from teacher group by prof having count(prof) < 2);

29、查詢選修編號為“3-105“課程且成績至少高於選修編號為“3-245”的同學

select * from score where cno = "3-105" and degree >= (select max(degree) from score where cno = "3-245");

30、Cno、Sno和Degree,並按Degree從高到低次序排序。

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

select cno, sno, degree from score order by degree;

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

select tname, tsex, tbirthday from teacher;

select sname, ssex, sbirthday from student;

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

select tname, tsex, tbirthday from teacher where tsex = "女";

select sname, ssex, sbirthday from student where ssex = "女";

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

select * from score where degree < (select avg(degree) from score where cno in (select cno from score)) and cno in (select cno from score);

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

select tname, depart from teacher where tno in (select tno from course);

35 、 查詢所有未講課的教師的Tname和Depart.

select tname, depart from teacher where tno not in (select tno from course);

36、查詢至少有2名男生的班號。

select class from student where ssex >= 2;

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

select * from student where sname not like ("%王%");

38、查詢Student表中每個學生的姓名和年齡。

select sname, sbirthday from student;

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

select min(sbirthday) from student;

select max(sbirthday) from student;

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

select * from student order by class desc, sbirthday desc;

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

select a.*, b.cname from teacher a, course b where b.tno = a.tno and a.tno in (select tno from teacher where tsex = "男");

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

select * from score where degree in (select max(degree) from score);

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

select sname from student where ssex = (select ssex from student where sname = "李軍");

44、查詢和“李軍”同性別並同班的同學Sname.
select sname from student where class = (select class from student where sname = "李軍") and ssex = (select ssex from student where sname = "李軍");

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

select * from score where sno in (select sno from student where ssex = "男") and cno = (select cno from course where cname = "計算機導論");

MySQL復雜用法