MySQL複雜用法

來源:互聯網
上載者:User

標籤:標準   電子   min   esc   開發   公司   功能   val   mysq   

  今天學了幾個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複雜用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.