当前位置:Gxlcms > 数据库问题 > sql 多表查询

sql 多表查询

时间:2021-07-01 10:21:17 帮助过:17人阅读

--不等值连接 SQL> --查询员工信息: 员工号 姓名 月薪 工资级别 SQL> select * from salgrade; GRADE LOSAL HISAL ---------- ---------- ---------- 1 700 1200 2 1201 1400 3 1401 2000 4 2001 3000 5 3001 9999 SQL> select e.empno,e.ename,e.sal,s.grade 2 from emp e,salgrade s 3 where e.sal between s.losal and s.hisal; EMPNO ENAME SAL GRADE ---------- ---------- ---------- ---------- 7369 SMITH 800 1 7900 JAMES 950 1 7876 ADAMS 1100 1 7521 WARD 1250 2 7654 MARTIN 1250 2 7934 MILLER 1300 2 7844 TURNER 1500 3 7499 ALLEN 1600 3 7782 CLARK 2450 4 7698 BLAKE 2850 4 7566 JONES 2975 4 EMPNO ENAME SAL GRADE ---------- ---------- ---------- ---------- 7788 SCOTT 3000 4 7902 FORD 3000 4 7839 KING 5000 5 View Code

 3.外链接

--外连接
SQL> --按部门统计员工人数:部门号 部门名称 人数
SQL> select d.deptno 部门号,d.dname 部门名称,count(e.empno) 人数
  2  from emp e,dept d
  3  where e.deptno=d.deptno
  4  group by d.deptno,d.dname;(没有出现在 分组函数中)

    部门号 部门名称             人数                                            
---------- -------------- ----------                                            
        10 ACCOUNTING              3                                            
        20 RESEARCH                5                                            
        30 SALES                   6                                            

SQL> select * from dept;

    DEPTNO DNAME          LOC                                                   
---------- -------------- -------------                                         
        10 ACCOUNTING     NEW YORK                                              
        20 RESEARCH       DALLAS                                                
        30 SALES          CHICAGO                                               
        40 OPERATIONS     BOSTON                                                

SQL> select * from emp where deptno=40;

未选定行

SQL> /*
SQL> 希望: 对于某些不成立的记录,任然希望包含在最后的结果中
SQL> 左外连接:当where e.deptno=d.deptno不成立的时候,等号左边的表任然被包含
SQL>     写法:where e.deptno=d.deptno(+)
SQL> 右外连接:当where e.deptno=d.deptno不成立的时候,等号右边的表任然被包含
SQL>     写法: where e.deptno(+)=d.deptno
SQL> */
SQL> select d.deptno 部门号,d.dname 部门名称,count(e.empno) 人数
  2  from emp e,dept d
  3  where e.deptno(+)=d.deptno
  4  group by d.deptno,d.dname;

    部门号 部门名称             人数                                            
---------- -------------- ----------                                            
        10 ACCOUNTING              3                                            
        40 OPERATIONS              0                                            
        20 RESEARCH                5                                            
        30 SALES                   6                                            

 

sql 多表查询

标签:account   dal   osal   左外连接   one   his   dna   div   where   

人气教程排行