//内连接
SELECT r.`studentno`,`studentname`,`subjectno`,`studentresult`
FROM `student` AS s
INNER JOIN `result` AS r
ON s.studentno = r.studentno
//右连接
SELECT s.`studentno`,`studentname`,`subjectno`,`studentresult`
FROM `student` s
RIGHT JOIN `result` r
ON s.studentno = r.studentno
//左连接
SELECT s.`studentno`,`studentname`,`subjectno`,`studentresult`
FROM `student` s
LEFT JOIN `result` r
ON s.studentno = r.studentno
SELECT s.studentno,studentname,subjectname,studentresult
FROM student s
RIGHT JOIN result r
ON s.studentno = r.studentno
INNER JOIN `subject` sub
ON r.subjectno = sub.subjectno
Inner join--如果表中至少有一个匹配,就返回行;
left join--会从左表中返回所有的值,即使右表中没有匹配;
right join--会从右表中返回所有的值,即使左表中没有匹配。
On和Where的区别
1. 先执行on,后执行where;on是建立关联关系在生成临时表的时候执行,where是在临时表生成后对数据进行筛选的。
2. 比如两个表进行left join连接查询结果,where会对查询的结果进行过滤, on会返回驱动表的所有数据。
自连接--自己的表和自己的表连接(一张表拆为两张一样的表)