SQL查詢(連接查詢)

1、等值連接

-- 查詢成績信息及課程信息

select * from 

courses,scores

where courses.courseNo=scores.courseNo


-- 查詢成績信息及課程信息 

select 

c.courseNo as 課程表課程號,

c.name 課程名, 

s.courseNo 成績表課程號,

s.score 成績

from courses as c, scores as s 

where c.courseNo=s.courseNo


-- 查詢學生信息及學生的成績

select   stu.name, sc.score 

from  students as stu ,scores as sc 

where stu.studentNo=sc.studentNo


內連接

-- 查詢學生信息及學生的成績 

select sc.score,cs.name 

from scores sc

inner join courses cs on sc.courseNo=cs.courseNo


--  查詢學生信息及學生的課程對應的成績

select stu.name 姓名,sc.score 成績,cs.name 課程名

from students stu,scores sc,courses cs 

where stu.studentNo=sc.studentNo and sc.courseNo=cs.courseNo



select stu.name,sc.score,cs.name 

from students stu 

inner join scores sc on stu.studentNo=sc.studentNo

inner join courses cs on sc.courseNo=cs.courseNo


-- 查詢王昭君的成績,要求顯示姓名、課程號、成績

select stu.name,sc.score,cs.name 

from students stu

inner join scores sc on stu.studentNo=sc.studentNo

inner join courses cs on sc.courseNo=cs.courseNo

where stu.name='王昭君'



-- 查詢王昭君的數據庫成績,要求顯示姓名、課程名、成績

select stu.name,sc.score,cs.name

from students stu 

inner join scores sc on stu.studentNo=sc.studentNo

inner join courses cs on sc.courseNo=cs.courseNo

where stu.name='王昭君' and cs.name='數據庫'

-- 查詢所有學生的數據庫成績,要求顯示姓名、課程名、成績

select stu.name,sc.score,cs.name 

from students stu

inner join scores sc on stu.studentNo=sc.studentNo

inner join courses cs on sc.courseNo=cs.courseNo

where cs.name='數據庫'



-- 查詢男生中最高成績,要求顯示姓名、課程名、成績

select stu.sex,stu.name,sc.score,cs.name from students stu

inner join scores sc on stu.studentNo=sc.studentNo

inner join courses cs on sc.courseNo=cs.courseNo

where sex='男'

order by sc.score desc      / 倒序

limit 1                                /顯示一行



select stu.sex,stu.name,sc.score,cs.name 

from students stu,scores sc,courses cs

where stu.studentNo=sc.studentNo and sc.courseNo=cs.courseNo and sex='男'

order by sc.score desc

limit 1



2、左連接

-- 查詢所有學生的成績,包括沒有成績的學生

select stu.name,sc.score from students stu

left join scores sc on stu.studentNo=sc.studentNo


-- 查詢所有學生的成績,包括沒有成績的學生,需要顯示課程名

select stu.name,sc.score,cs.name from students stu

left join scores sc on stu.studentNo=sc.studentNo

left join courses cs on sc.courseNo=cs.courseNo


3、右連接

-- 課程表插入一些數據

insert into courses 

values (0, '語文'),(0, '數學');


-- 例1:查詢所有課程的成績,包括沒有成績的課程

select sc.score,cs.name from scores sc 

right join courses cs on sc.courseNo=cs.courseNo



-- 例2:查詢所有課程的成績,包括沒有成績的課程,包含學生

select sc.score,cs.name,stu.name from scores sc 

right join courses cs on sc.courseNo=cs.courseNo

left join students stu on stu.studentNo=sc.studentNo





4、自關聯


工具表創建

create table areas(
aid int primary key,      
//本級編號
atitle varchar(20),      
//地名
pid int                  
//上級編號
);

insert into areas
values ('130000', '河北省', NULL),
('130100', '石家莊市', '130000'),
('130400', '邯鄲市', '130000'),
('130600', '保定市', '130000'),
('130700', '張家口市', '130000'),
('130800', '承德市', '130000'),
('410000', '河南省', NULL),
('410100', '鄭州市', '410000'),
('410300', '洛陽市', '410000'),
('410500', '安陽市', '410000'),
('410700', '新鄉市', '410000'),














('410800', '焦作市', '410000');

('410101', '中原區', '410100'),

('410102', '二七區', '410100'),

('410103', '金水區', '410100');

-- 查詢一共有多少個省

select count(*) from areas where pid is null


-- 查詢河南省的所有城市

select * from areas p,areas c

where p.aid=c.pid and p.atitle='河南省'  

 //本級編號與上級編號相同,且地名爲“河南省”時輸出


- 查詢鄭州市的所有區縣(兩級兩個表自相關)

select * from areas p,areas c


where p.aid=c.pid and p.atitle='鄭州市'

select * from areas p


inner join areas c on p.aid=c.pid


where p.atitle='鄭州市'

 


-- 例3:查詢河南省的所有區縣(三級三個表自相關)

select * from areas p


inner join areas c on p.aid=c.pid


inner join areas q on c.aid=q.pid


where p.atitle='河南省'

注意:只能查詢出有區縣的市


select * from areas p

inner join areas c on p.aid=c.pid   

left join areas q on c.aid=q.pid     

where p.atitle='河南省'

注意:有無區縣都可以查詢出


5、子查詢

5.1標量子查詢:子查詢返回的結果時一行一列


-- 查詢班級學生的平均年齡

select avg(age) from students


-- 查詢大於平均年齡的學生 21.4167

select * from students where age>21.4167



select * from students 

where age>(select avg(age) from students)


-- 查詢王昭君的成績,要求顯示成績

-- 先查詢王昭君的學號

select studentNo from students where name='王昭君'


select * from scores 

where studentNo=(select studentNo from students where name='王昭君')


5.2列子查詢:子查詢返回的結果時一列多行



-- 查詢18歲的學生的成績,要求顯示成績


-- 先查詢18歲的學生的學號

-- select studentNo from students where age=18


--


-- select * from scores where studentNo='0' or studentno='2'


-- select * from scores where studentNo in('002','006')




select * from scores where studentNo in(select studentNo from students where age=18)



5.3行級子查詢:子查詢返回的結果時一行多列:行級子查詢


-- 查詢男生中年齡最大的學生信息

select * from students where 

age = (select max(age) from students where sex='男')

//標量子查詢


select * from students where 

(sex,age)=(select sex,age from students where sex='男' order by age desc limit 1)

//行級子查詢


5.4表級子查詢:子查詢返回的結果時多行多列

-- 例5:查詢數據庫和系統測試的課程成績

select * from scores sc

 inner join courses cs on sc.courseNo=cs.courseNo

where cs.name in ('數據庫','系統測試')

//連接查詢


-- select * from courses where name in ('數據庫','系統測試')

select * from scores sc

inner join (select * from courses where name in ('數據庫','系統測試')) c

on sc.courseNo=c.courseNo

//表級子查詢



查詢學習目錄

條件查詢

 where  字段 <,>,=,!=

                     and ,or, not

                     in,between and 

                      like , % , _


排序 

order by asc,desc

聚合函數 

sum max min avg count(*)

分組 

group by

分頁

limit 0,3 

遊標 索引 角標

連接查詢 

等值連接(from 表1,表2   inner join) 

左連接 left join  

右連接 right join

子查詢 

標量子查詢  

列子查詢  

行子查詢 

表級子查詢



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章