数据库里有两个表tableA,tableB,tableC
表A结构如下
id 
1
2
3
4
5
6
表B结构如下
 pid
  1
  2
  
表C结构如下
 pid
   3
   4
  
我现在想做如下查询,我想把tableA里的id在表tableB.tableC里的不存在id=pid的数据查询出来
我是这么写的正确结果应该返回 5,6 因为5,6在tableB,tableC里的没有出现

解决方案 »

  1.   

    The UNION [ALL], INTERSECT, MINUS Operators
    You can combine multiple queries using the set operators UNION, UNION ALL, INTERSECT, and MINUS. All set operators have equal precedence. If a SQL statement contains multiple set operators, then Oracle Database evaluates them from the left to right unless parentheses explicitly specify another order.The corresponding expressions in the select lists of the component queries of a compound query must match in number and must be in the same datatype group (such as numeric or character).If component queries select character data, then the datatype of the return values are determined as follows:If both queries select values of datatype CHAR, then the returned values have datatype CHAR.If either or both of the queries select values of datatype VARCHAR2, then the returned values have datatype VARCHAR2.If component queries select numeric data, then the datatype of the return values is determined by numeric precedence:If any query selects values of type BINARY_DOUBLE, then the returned values have datatype BINARY_DOUBLE.If no query selects values of type BINARY_DOUBLE but any query selects values of type BINARY_FLOAT, then the returned values have datatype BINARY_FLOAT.If all queries select values of type number, then the returned values have datatype NUMBER.In queries using set operators, Oracle does not perform implicit conversion across datatype groups. Therefore, if the corresponding expressions of component queries resolve to both character data and numeric data, Oracle returns an error.
    ExamplesThe following query is valid:SELECT 3 FROM DUAL
       INTERSECT
    SELECT 3f FROM DUAL;This is implicitly converted to the following compound query:SELECT TO_BINARY_FLOAT(3) FROM DUAL
       INTERSECT
    SELECT 3f FROM DUAL;The following query returns an error:SELECT '3' FROM DUAL
       INTERSECT
    SELECT 3f FROM DUAL;
    Restrictions on the Set Operators 
    The set operators are not valid on columns of type BLOB, CLOB, BFILE, VARRAY, or nested table.The UNION, INTERSECT, and MINUS operators are not valid on LONG columns.If the select list preceding the set operator contains an expression, then you must provide a column alias for the expression in order to refer to it in the order_by_clause.You cannot also specify the for_update_clause with the set operators.You cannot specify the order_by_clause in the subquery of these operators.You cannot use these operators in SELECT statements containing TABLE collection expressions.
      

  2.   

    MINUS ExampleThe following statement combines results with the MINUS operator, which returns only rows returned by the first query but not by the second:SELECT product_id FROM inventories
    MINUS
    SELECT product_id FROM order_items;
      

  3.   

    上面老大说的大概就是这样吧
    select id from tablea
    minus
    select pid from tableb
    minus
    select pid from tablec
      

  4.   

    select a.id from tableA a left join tableB b on a.id=b.pid
                              left join tableC c on a.id=c.pid
    where b.pid is null and c.pid is null
      

  5.   

    select * from a where not exists (select 1 from b where a.id=b.pid)
    and not exists (select 1 from c where a.id=c.pid)
      

  6.   

    select id from a
    not in (select pid as id from b union all 
            select pid as id from c)
      

  7.   

    支持 mqmmx(在c#和java中迷失) 的方法,用集合运算