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;
//多表连接
*/
/*
//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;
相关文章推荐
- Mysql数据库_DML_常用函数.sql
- Mysql数据库_DML.sql
- Mysql数据库_DDL.sql
- Mysql数据库_ 视图.sql
- MySQL 无法连接
- ORDER BY RAND()
- mysql判断字段是否存在的方法
- Mysql for MacOSX 安装和基本操作
- 修改mysql编码《转》
- Mysql导入导出.sql文件《转》
- MySQL 获得当前日期时间(以及时间的转换)
- mysql时间和日期转换的一些例子
- mysql 数据备份和还原
- MySql 密码忘记了解决方法
- mysql中lower_case_tables_names说明
- Mysql日期函数总结
- 原来mysql里面有这么高级的用法的…是我自己低级了吧…
- mysql启动和连接
- mysql InnoDB加锁实现原理及实例
- Host 'xxx' is not allowed to connect to this MySQL server的解决