mysql> select * from emp,dept where emp.deptno=dept.deptno;//where筛选;多个表的话用.来描述特列 如果只mysql> select * from emp,dept ;错误输出,他会列出count(emp)*count(dept)行信息,相当于emp的每一行与dept的每一行匹配完后继续下一行在匹配,会有不需要的和错误的信息
**方法1:筛选出名字和部门(where):**mysql> select emp.ename,dept.dname from emp,dept where emp.deptno=dept.deptno;//两表合并,筛选出正确数据 mysql> select ename,dname from emp,dept where emp.deptno=dept.deptno and ename='SMITH';
方法2:筛选出名字和部门(内连接):
mysql> select * from emp inner join dept on emp.deptno=dept.deptno; mysql> select ename,dname from emp inner join dept on emp.deptno=dept.deptno; mysql> select ename,dname from emp inner join dept on emp.deptno=dept.deptno where ename='SMITH';//用where筛选出特定的
mysql> select ename,dname from emp inner join dept on emp.deptno=dept.deptno and ename='SMITH';//用and也行
mysql> select * from emp right join dept on emp.deptno=dept.deptno order by dept.deptno;//采用右连接 mysql> select * from dept left join emp on emp.deptno=dept.deptno order by dept.deptno;//采用左连接