其他分享
首页 > 其他分享> > 联表查询Join On

联表查询Join On

作者:互联网

-- ===============================联表查询=============================

-- 查询参加了考试的同学(学号,姓名,科目编号,分数)
SELECT * FROM student
SELECT * FROM result

思路:

 

操作描述
inner join 如果表中至少有一个匹配,就返回值
left join 会从左表中返回所有的值,即使右表中没有匹配
right join 会从右表中返回所有的值,即使左表中没有匹配

 

-- inner join
 -- Student表,result表 Student表   result表     result表            
SELECT s.`StudentNo`,`StudentName`,`subjectno`,`studentresult`
FROM student AS s
INNER JOIN result AS r
WHERE s.`StudentNo`=r.`StudentNo`

-- right join
SELECT s.`StudentNo`,`StudentName`,`subjectno`,`studentresult`
FROM student AS s
RIGHT JOIN result AS r
ON s.`StudentNo`=r.`StudentNo`

-- left join
SELECT s.`StudentNo`,`StudentName`,`subjectno`,`studentresult`
FROM student AS s
LEFT JOIN result AS r
ON s.`StudentNo`=r.`StudentNo`

-- 多张表连接                       subject表  
SELECT s.`StudentNo`,`StudentName`,`subjectName`,`studentresult`
FROM student AS s
RIGHT JOIN result AS r
ON r.`StudentNo`=s.`StudentNo`
INNER JOIN `subject` AS sub
ON r.`subjectno`=sub.`subjectno`

 

总结:

例如:from a left join b

from a left join b

标签:Join,--,查询,StudentNo,subjectno,result,联表,join
来源: https://www.cnblogs.com/zypdbk/p/15314911.html