zoukankan      html  css  js  c++  java
  • sql 多表查询

    1.等值连接 

      select e.empno d.dname   from  emp e,dept d where  e.deptno=d.deptno;

    2.不等值连接 

    SQL> --不等值连接
    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                                            

    4.自连接

      --自连接不适合操作大表
      自连接: 通过表的别名,将同一张表视为多张表

    5.层次查询

      

      

  • 相关阅读:
    关于上传组件
    二分查找的时间复杂度
    commander.js
    执行上下文
    谷歌插件开发
    网站性能
    日记
    <<人间失格>>阅读
    Node.js 应该用在什么地方
    浅谈前后端分离与实践 之 nodejs 中间层服务
  • 原文地址:https://www.cnblogs.com/jycjy/p/6718485.html
Copyright © 2011-2022 走看看