比如Table1中的字段Field1和Table2中的字段Field2,如下所示:
Table1.Field1
11111111111
22222222222
33333333333
55555555555
66666666666
77777777777
88888888888
99999999999
Table2.Field2
11111111111
22222222222
33333333333
44444444444
55555555555
66666666666
77777777777
88888888888
请问SQL怎么写才能把44444444444和99999999999找出来?
------解决方案--------------------
三个操作MINUS UNION INTERSECT
--方法1
WITH T AS (
SELECT 11111111111 AS A, 11111111111 AS B FROM DUAL UNION ALL
SELECT 22222222222 AS A, 22222222222 AS B FROM DUAL UNION ALL
SELECT 33333333333 AS A, 33333333333 AS B FROM DUAL UNION ALL
SELECT 55555555555 AS A, 44444444444 AS B FROM DUAL UNION ALL
SELECT 66666666666 AS A, 55555555555 AS B FROM DUAL UNION ALL
SELECT 77777777777 AS A, 66666666666 AS B FROM DUAL UNION ALL
SELECT 88888888888 AS A, 77777777777 AS B FROM DUAL UNION ALL
SELECT 99999999999 AS A, 88888888888 AS B FROM DUAL
)
(
SELECT T.A FROM T
UNION
SELECT T.B FROM T
)
MINUS
(
SELECT T.A FROM T
INTERSECT
SELECT T.B FROM T
)
--方法2
WITH T AS (
SELECT 11111111111 AS A, 11111111111 AS B FROM DUAL UNION ALL
SELECT 22222222222 AS A, 22222222222 AS B FROM DUAL UNION ALL
SELECT 33333333333 AS A, 33333333333 AS B FROM DUAL UNION ALL
SELECT 55555555555 AS A, 44444444444 AS B FROM DUAL UNION ALL
SELECT 66666666666 AS A, 55555555555 AS B FROM DUAL UNION ALL
SELECT 77777777777 AS A, 66666666666 AS B FROM DUAL UNION ALL
SELECT 88888888888 AS A, 77777777777 AS B FROM DUAL UNION ALL
SELECT 99999999999 AS A, 88888888888 AS B FROM DUAL
)
SELECT T.A FROM T
MINUS
SELECT T.B FROM T
UNION
(
SELECT T.B FROM T
MINUS
SELECT T.A FROM T
)
------解决方案--------------------
想找一个里面有而另一个没有的话
直接以一个表里面的field值为数据集查询另一个表里面field值不在这个集合里面的数据
select * from table where field1 not in (select field2 from table2)