欢迎光临
我们一直在努力

MySQL第七课 left join左连接

场景

        在查询学生信息中,允许学生宿舍信息为空,如果有获取,没有为空

select t.*, cs_sc_studenthostel.name as hostelname
from (
select cs_sc_student.id as student_id,
cs_sc_student.name as studentname,  
cs_sc_class.name as classname, 
cs_sc_student.sc_studenthostel_id  
from cs_sc_student, cs_sc_class 
where cs_sc_student.sc_class_id = cs_sc_class.id) t
left join cs_sc_studenthostel on t.sc_studenthostel_id = cs_sc_studenthostel.id

第一步 获取到学生的基础信息

(
select cs_sc_student.id as student_id,
cs_sc_student.name as studentname,  
cs_sc_class.name as classname, 
cs_sc_student.sc_studenthostel_id  
from cs_sc_student, cs_sc_class 
where cs_sc_student.sc_class_id = cs_sc_class.id) t


第二步 进行左连接

left join cs_sc_studenthostel on t.sc_studenthostel_id = cs_sc_studenthostel.id

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。