网上找的七种Join查询图片
-- 查询参加考试的同学,包含的信息包括学号,姓名,科目编号,分数
/*
分析思路
1.分析需求,查询的字段涉及到哪些表
2.确定要哪些连接查询
3.确定交叉点,涉及到的表有哪些相同的字段
判断条件:学生表的studentno = 成绩表的studentno
*/
join on 连接查询
join where 等价查询
inner join 两表连接的相同数据
-- inner join
SELECT s.studentno,studentname,subjectno,studentresult
FROM student as s
INNER JOIN result as r
WHERE s.studentno=r.studentno
left join 以右表为基准查询
--left join
SELECT s.studentno,studentname,subjectno,studentresult
FROM student as s
LEFT JOIN result as r
ON s.studentno=r.studentno
right join 以右表为基准查询
--right join
SELECT s.studentno,studentname,subjectno,studentresult
FROM student as s
RIGHT JOIN result as r
ON s.studentno=r.studentno
查询没有参加考试的同学,包含的信息包括学号,姓名,科目编号,分数
SELECT s.studentno,studentname,subjectno,studentresult
FROM student as s
LEFT JOIN result as r
ON s.studentno=r.studentno
WHERE `subjectno` is NULL
查询没有参加考试的同学,包含的信息包括学号,姓名,科目,分数
SELECT s.studentno,studentname,subjectname,studentresult
FROM student as s
RIGHT JOIN result as r
ON s.studentno=r.studentno
inner JOIN `subject` sub
on r.subjectno=sub.subjectno
查询学生所属的年级(学号,姓名,年级名称)
SELECT studentno,studentname,gradename
from student s
INNER JOIN grade g
on s.gradeid=g.gradeid
查询科目对应的年级(科目名称,年级名称)
SELECT subjectname,gradename
from `subject`
INNER JOIN grade
on `subject`.gradeid=grade.gradeid
查询参加高等数学-1考试的同学,包含的信息包括学号,姓名,科目名,分数
SELECT s.studentno,studentname,subjectname,studentresult
FROM student s
INNER JOIN result r
ON s.studentno=r.studentno
LEFT JOIN `subject` sub
on r.subjectno=sub.subjectno
WHERE sub.subjectname='高等数学-1'
思路分析
-
分析需求,查询的字段设计到哪些表
-
确定要哪种连接
-
判断交叉点,作为条件
语法:
查询字段 from 表1 left join 表2 on 交叉条件
查询字段 from 表1 right join 表2 on 交叉条件
标签:JOIN,join,查询,studentno,subjectno,联表,MySQL,Join,SELECT From: https://www.cnblogs.com/yzx-sir/p/17398147.html自连接:将一个表拆分成两张表,之后进行连接