mysql帶條件查詢,聯表查詢

---恢復內容開始---mysql

1,用於設定所select出來的數據是否容許出現重複行(徹底相同的數據行)sql

all:容許出現——默認不寫就是All(容許的)。spa

distinct:不容許出現——就是所謂的「消除重複行」3d

2,where:條件blog

3,group by:分組依據 後面加表的字段名,一般只進行一個字段的分組排序

mysql表查詢語法形式:select [all | distinct] 字段名或表達式 from 表名 [where] [group by] [having] [order by] [limit];it

練習題:共有下面四張表    學生表:student  教師表:teacher  課程表:course 成績表:scoreio

          

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

--操做表score,以cno分組而且cno是以3開頭,取出各個cno的總數,及與cno對應的degree的平均值date

select count(cno),avg(degree) from score where cno like '3%' group by cno;

 

--從上面的虛擬表中找到cno總數大於5的

select * from

(select count(cno) a,avg(degree) b from score where cno like '3%' group by cno) c

where a>5;

2,查詢全部學生的Sno、Cname和Degree列

--找到student的sno
 select sno from student;


 --找到score 的sno,degree,cno
 select sno,degree,cno from score;


 --找到course的cno
 select cno,cname from course;


 --組成新表cno sno degree
 select a.cno,b.sno,b.sname,a.degree from (select sno,degree,cno from score) a  join (select sno,sname from student) b on a.sno=b.sno;


 --組成有cname cno sn degree sname的表
 select d.cname,e.cno,e.sno,e.degree,e.sname
 from
   (select a.cno,b.sno,b.sname,a.degree from (select sno,degree,cno from score) a  join (select sno,sname from student) b on a.sno=b.sno) as e
  join
  (select cname,cno from course) as d
  on d.cno=e.cno;

3,查詢「95033」班學生的平均分

--從student取sno class,條件是class是95033的

select sno,class from student where class='95033';

--取出score中sno degree

select sno,degree from score;

--將上面兩張表組成一張,取degree的平均值

select avg(degree) from

(select sno,class from student where class='95033') a

join

(select sno,degree from score) b

on a.sno=b.sno;

4,查詢選修「3-105」課程的成績高於「109」號同窗成績的全部同窗的記錄

--將109號的成績取出

select degree from score where sno='109' and cno='3-105';

-- 得出最終表

select * from score

where

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

and cno='3-105';

5,查詢和學號爲108的同窗同年出生的全部學生的Sno、Sname和Sbirthday列

--找到student中sno爲108 的Sbirthday
select year(sbirthday) from student where sno='108';


-- 得出最終表
select sno,sname,sbirthday from student

where

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

 6.查詢「張旭「教師任課的學生成績(姓名)

--找到teacher中tname爲張旭的tno

select tno from teacher where tname='張旭';

--找到course中tno和teacher中tname爲張旭的tno相同的cno

select cno from course where tno=(select tno from teacher where tname='張旭');

--找到score中cno爲6-166的sno cno degree

select sno,cno,degree from score

where

cno=(select cno from course where tno=(select tno from teacher where tname='張旭'));

--找到student表中與上表sno相同的sname與上表組成新表

select a.sname,b.sno,b.cno,b.degree from

(select sno,sname from student) a

join

(select sno sno,cno,degree from score where cno=(select cno from course where tno=(select tno from teacher where tname='張旭'))) b

on a.sno=b.sno;

7,查詢考計算機導論的學生成績
--找到course中cname爲計算機導論的cno
select cno from course where cname='計算機導論';


--找到score中cno與上表中相同時的sno degree
select sno,degree from score where cno=(select cno from course where cname='計算機導論');


-- 得出最終表
select b.sname,a.sno,a.degree from

(select sno,degree from score where

cno=(select cno from course where cname='計算機導論')) a join (select sname,sno from student) b

on a.sno=b.sno;


8,查詢李誠老師教的課程名稱
select tno from teacher where tname='李誠';
select cname,cno from course where tno=(select tno from teacher  where tname='李誠');


9,查詢選修某課程的同窗人數多於5人的教師姓名
--score以cno分組,統計cno的數量
select count(cno) a,cno from score group by cno;


--找到上表中cno數量大於5的的cno
select cno from (select count(cno) a,cno from score group by cno) b where a>5;


--找到與上表cno一致的表course表中的tno
select tno from course where cno=(select cno from (select count(cno) a,cno from score group by cno) b where a>5);


--與上表tno一致的表teacher中的tname
select tname from teacher where tno=(select tno from course where cno=(select cno from (select count(cno) a,cno from score group by cno) b where a>5));


10,查詢95033班和95031班全體學生的記錄
--找到表score中的sno和cno 和degree
select sno,cno,degree from score;
--將上表和student組合
select a.sno,a.sname,a.ssex,a.sbirthday,a.class,b.cno,b.degree from

(select * from student) a join (select cno,degree,sno from score) b

on a.sno=b.sno;


11,查詢存在有85分以上成績的課程Cno
select cno,degree from score where degree>85;


12,查詢出「計算機系「教師所教課程的成績表
--找到teacher中的tno tname
select tno,tname from teacher where dapart='計算機系';
--找到course中tno和上表相同的cno
select a.cno,b.tno,b.tname from (select tno,cno from course) a join (select tno,tname from teacher where dapart='計算機系') b on a.tno=b.tno;
--找到score中cno與上表相同的degree
select c.sno,c.cno,c.degree,d.tname from (select sno,cno,degree from score) c join (select a.cno,b.tno,b.tname from (select tno,cno from course) a join (select tno,tname from teacher where dapart='計算機系') b on a.tno=b.tno) d on c.cno=d.cno;


13,查詢選修編號爲「3-105「課程且成績至少高於選修編號爲「3-245」的同窗    的Cno、Sno和Degree,並按Degree從高到低次序排序
--score中找到cno=3-245的degree,取出最小值
select min(degree) from score where cno='3-245';
--score中找到cno=3-105的cno sno degree,而且degree大於上表的degree
select cno,sno,degree from

score where cno='3-105'

and degree>(select min(degree) from score where cno='3-245')

order by degree desc;


14,查詢選修編號爲「3-105」且成績高於選修編號爲「3-245」課程的同窗的    Cno、Sno和Degree.
--找到score中cno=3-245中的degree的最大值
select max(degree) from score where cno='3-245';
--找到score中cno=3-105的cno sno degree
select cno,sno,degree from

score where

degree>(select max(degree) from score where cno='3-245') order by degree asc;


15,查詢全部教師和同窗的name、sex和birthday
select tname,tsex,tbirthday from teacher
union
select sname,ssex,sbirthday from student;
16,查詢全部「女」教師和「女」同窗的name、sex和birthday
select tname,tsex,tbirthday from teacher where tsex='女'
union
select sname,ssex,sbirthday from student where ssex='女';


17,查詢成績比該課程平均成績低的同窗的成績表
select avg(degree) from score where cno='3-105';
select avg(degree) from score where cno='3-245';
select avg(degree) from score where cno='6-166';
select * from score where cno='3-105' and degree<(select avg(degree) from score where cno='3-105')
union
select * from score where cno='3-245' and degree<(select avg(degree) from score where cno='3-245')
union
select * from score where cno='6-166' and degree<(select avg(degree) from score where cno='6-166');


18,查詢全部任課教師的Tname和Depart
select a.tname,a.dapart from
(select tno,tname,dapart from teacher) a join
(select tno from course) b
on a.tno=b.tno;


19,查詢全部未講課的教師的Tname和Depart
select a.tname,a.dapart from
(select tno,tname,dapart from teacher) a join
(select tno from course) b
on a.tno=b.tno;


20,查詢至少有2名男生的班號
select count(ssex),class from student where ssex='男' group by class;


select b.class from (select count(ssex) a,class from student where ssex='男' group by class) b where a>=2;


21,查詢Student表中不姓「王」的同窗記錄
select * from student where sname not like '王%';


22,查詢Student表中每一個學生的姓名和年齡
select  sname,year(now())-year(sbirthday) from student;


23,查詢Student表中最大和最小的Sbirthday日期值
select min(day(sbirthday)),max(day(sbirthday)) from student;


24,以班號和年齡從大到小的順序查詢Student表中的所有記錄
select * from student order by class desc,date(sbirthday) asc;


25,查詢「男」教師及其所上的課程
select tno from teacher where tsex='男';
select a.tno,b.cname,a.tname from (select tno,tname from teacher where tsex='男') a join (select * from course) b on a.tno=b.tno;

 

相關文章
相關標籤/搜索