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

子查询 

标量子查询  

列子查询  

行子查询 

表级子查询



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