Mysql数据库_DML_多表连接.sql

来源:互联网 发布:剑三破军道姑捏脸数据 编辑:程序博客网 时间:2024/05/17 21:56
/*
//多表连接
*/


/*
//1.交叉连接/笛卡尔交集
*/
select count(*) from tb_emp;#17
select count(*) from tb_dept;#4
select * from tb_emp,tb_dept;#68=17*4;
select * from tb_emp cross join tb_dept; # --标准写法


/*
//2.内连接 连接条件就是主外键关联
*/
select * from tb_emp e,tb_dept d where e.deptno = d.deptno;
select * from tb_dept inner join tb_emp on tb_dept.deptno = tb_emp.deptno;


/*
//3.左外连接
左边的表为主表,坐标的表记录全部显示,如果没有找到记录则补NULL
*/
select * from tb_dept left join tb_emp on tb_dept.deptnp = tb_emp.deptno;


#oracle语法,左连接加号在右边
select * from t b_emp e,tb_dept d where e.deptno = d.deptno(+);


/*
//3.右外连接
右边的表为主表,坐标的表记录全部显示,如果没有找到记录则补NULL
*/
select * from tb_dept left join tb_emp on tb_dept.deptnp = tb_emp.deptno;


#oracle语法,左连接加号在右边
select * from t b_emp e,tb_dept d where e.deptno = d.deptno(+);


/*
//自连接
*/
select c.name '类别名',c2.name '父类别名'
from tb_course c,tb_course c2
where c.pid = c2.id;





































0 0
原创粉丝点击