首页 > 其他 > 详细

左外连接的错误

时间:2020-06-10 17:13:14      阅读:33      评论:0      收藏:0      [点我收藏+]

题目:查询小尹老师底下所有的学生

create database test;
use test;
create table stu(
    id bigint,
    classID bigint,
    name varchar(10)
    );
create table class(
    id bigint,
    teacherID bigint
    );
create table teacher(
    id  bigint,
    name varchar(10)
    );
    
 insert into stu values(1,2,‘马哥‘);
 insert into class values(1,2);
 insert into teacher values(2,‘小尹‘);
 update class set id = 1 where id = 2;

错误的 因为左外连接 尽量保存左表的数据 on的条件相当于没有

SELECT S.* FROM `stu` S LEFT JOIN class C on S.classID = C.id
LEFT JOIN teacher T on C.teacherID = T.id and T.name = "小尹";

正确的

 SELECT S.* FROM `teacher` T LEFT JOIN class C on T.id = C.teacherID
LEFT JOIN stu S on S.classID = 2 and T.name = "小尹";

左外连接的错误

原文:https://www.cnblogs.com/ykpkris/p/13086501.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!