数据库里有两个表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里的没有出现
表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里的没有出现
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.
MINUS
SELECT product_id FROM order_items;
select id from tablea
minus
select pid from tableb
minus
select pid from tablec
left join tableC c on a.id=c.pid
where b.pid is null and c.pid is null
and not exists (select 1 from c where a.id=c.pid)
not in (select pid as id from b union all
select pid as id from c)