数据库中的集合运算

  1. 并集:UNION/UNION ALL

    union:返回两个集合去掉重复元素后的所有记录
    	SELECT E.* FROM EMP E,SALGRADE S WHERE S.GRADE = 1 AND E.SAL BETWEEN S.LOSAL AND S.HISAL
    	UNION
    	SELECT E.* FROM EMP E,SALGRADE S WHERE S.GRADE = 2 AND E.SAL BETWEEN S.LOSAL AND S.HISAL;
    
    union all:返回两个集合的所有记录,包括重复的
    	SELECT E.* FROM EMP E,SALGRADE S WHERE S.GRADE = 1 AND E.SAL BETWEEN S.LOSAL AND S.HISAL
    	UNION ALL
    	SELECT E.* FROM EMP E,SALGRADE S WHERE S.GRADE = 2 AND E.SAL BETWEEN S.LOSAL AND S.HISAL;
    
  2. 交集:INTERSECT

    intersect:返回同时属于两个集合的记录
    		SELECT E.* FROM EMP E,SALGRADE S WHERE S.GRADE = 1 AND E.SAL BETWEEN S.LOSAL AND S.HISAL
    		INTERSECT
    		SELECT E.* FROM EMP E,SALGRADE S WHERE S.GRADE = 2 AND E.SAL BETWEEN S.LOSAL AND S.HISAL;
    
  3. 差集:MINUX

    minux:返回属于第一个集合,但不属于第二个集合的记录
    	SELECT E.* FROM EMP E,SALGRADE S WHERE S.GRADE = 1 AND E.SAL BETWEEN S.LOSAL AND S.HISAL
    		MINUX
    	SELECT E.* FROM EMP E,SALGRADE S WHERE S.GRADE = 2 AND E.SAL BETWEEN S.LOSAL AND S.HISAL;
    
  4. 注意事项

    SELECT语句中参数类型和个数要一致
    可以使用括号改变集合使用的顺序
    如果有 ORDER BY 语句,必须放到最后一句查询语句中
    集合运算采用第一个语句的表头作为表头

你可能感兴趣的:(【数据库】Oracle)