显示where子句中不匹配的记录

时间:2016-11-16 18:29:12

标签: oracle11g

我需要显示与where子句不匹配的记录。

示例 - select * from citytable where city in ('aa','bb','cc', 'dd','ee');

表格中只有aabbccdd&表中没有ee。但是,我仍然需要显示dd& ee

3 个答案:

答案 0 :(得分:0)

我不确定您希望输出看起来如何。如果没有city ='ee'的数据,你想要展示什么?像这样的东西?

SELECT * FROM
(SELECT key AS A_key, one AS A_one, two AS A_two FROM cityTable WHERE one='aa') AS A
JOIN
(SELECT key AS E_key, one AS E_one, two AS E_two FROM cityTable WHERE one='ee') AS E
ON A_key=E_key 
...etc.  

编辑:或者就是这样: SELECT city FROM (SELECT city, count(*) AS c FROM cityTable GROUP BY city) WHERE c = 0

答案 1 :(得分:0)

据我了解,你说'dd' amd 'ee'不在表中,但你仍然需要它,所以你可以使用union all来实现它。但要记住“dd'”的列。并且' ee'由于您的citytable

中没有记录,因此行始终为空
SELECT ct.col1 AS city, ct.col2.....<all columns of your table>
    from citytable ct
    where city in ('aa','bb','cc')
    UNION ALL
    select 'dd' as city,null ,null.....<nulls as many times as the number of columns of your table>
    from citytable ct1
    UNION ALL
    select 'ee' as city,null ,null.....<nulls as many times as the number of columns of your table>
    from citytable ct2

答案 2 :(得分:0)

你可能正在寻找这样的东西。 IN条件与包含IN列表中唯一(不同)值的表的内部联接相同。你想要的是一个外连接。您需要有一个表而不是IN列表。在下面的解决方案中,我展示了如何动态创建这个“帮助者”表;还有其他几种方法,这只是展示了这个想法。

select deptno, ename from emp where deptno in (10, 50, 80);

DEPTNO  ENAME
------  ------
10      CLARK
10      KING
10      MILLER


with h ( deptno ) as (
  select 10 from dual union all
  select 50 from dual union all
  select 80 from dual
)
select h.deptno, e.ename
from h left outer join emp e
on h.deptno = e.deptno
;

DEPTNO  ENAME
------  ------
10      CLARK
10      KING
10      MILLER
50  
80