赞
踩
总结对比连接的特征如下:
操作 | 关键字 | 描述 |
内连接 | JOIN 或者INNER JOIN | 用比较运算符比较要连接的列的值的连接,不匹配的行不会被显示 |
全外连接 | FULL JOIN | 所有的值都会进行显示,但是不匹配的会表示为NULL |
左外连接 | LEFT JOIN | 左表所有行列都显示,但右表不匹配的会表示为NULL |
右外连接 | RIGHT JOIN | 右表所有行列都显示,但左表不匹配的会表示为NULL |
- -- =========联表查询=========
- -- 1.先确定该查询需要用到哪些表
- -- 2.确定使用哪种连接查询,找出两张表中的共同属性
-
- -- 加入某课程的同学(学号、姓名、课程号、分数)
- -- ======inner join=======
- SELECT sc.studentNo,studentName,subjectNo,studentScore
- FROM student AS st
- INNER JOIN score AS sc
- WHERE st.studentNo = sc.studentNo
-
- -- =======left join=======
- SELECT sc.studentNo,studentName,subjectNo,studentScore
- FROM student AS st
- LEFT JOIN score AS sc
- ON st.studentNo = sc.studentNo
-
- -- =======right join=======
- SELECT sc.studentNo,studentName,subjectNo,studentScore
- FROM student AS st
- RIGHT JOIN score AS sc
- ON st.studentNo = sc.studentNo
-
- -- 查询缺考的同学
- SELECT sc.studentNo,studentName,subjectNo,studentScore
- FROM student AS st
- RIGHT JOIN score AS sc
- ON st.studentNo = sc.studentNo
- WHERE studentScore IS NULL
- -- ==========三表连接==========
- -- 学生表、课程表、分数表
- -- 查询参加考试的同学的信息
- SELECT st.studentNo,studentName,subjectName,studentScore
- FROM student AS st
- RIGHT JOIN score AS sc
- ON st.studentNo = sc.studentNo
- INNER JOIN `subject` AS su
- ON sc.subjectNo = su.subjectNo
以上表为例,每一个产品都以一个自己的id,并且还有一个parent_id。此时我们进行以下操作:查询父类对应的子类关系
- -- 查询父子信息
- SELECT f.cate_name AS father,s.cate_name AS son
- FROM tdb_cates AS f
- JOIN tdb_cates AS s
- WHERE f.id = s.parent_id
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。