您的位置:首页 > 数据库 > MySQL

Mysql数据库_DML_多表连接.sql

2014-01-23 18:49 239 查看
/*

//多表连接

*/

/*

//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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: