55-左连接,右连接,内连接,如何编写SQL,他们的区别是什么?

左连接:以左表为主

select a.,b. from a left join b on a.b_id = b.id;

右连接:以右表为主

select a.,b. from a right join b on a.b_id = b.id;

内连接:只列出两张表关联查询符合条件的记录

select a.,b. from a inner join b on a.b_id = b.id;

案例:

select t.id t_id,t.name t_name,c.id c_id,c.name c_name

from t_teacher t LEFT JOIN t_class c on t.id=c.t_id; #4条,以老师表为主

select t.id t_id,t.name t_name,c.id c_id,c.name c_name

from t_teacher t RIGHT JOIN t_class c on t.id=c.t_id; #4条,以班级表为主

select t.id t_id,t.name t_name,c.id c_id,c.name c_name

from t_teacher t INNER JOIN t_class c on t.id=c.t_id; #3条,只展示匹配条件的记录