如何在Oracle SQL中找到记录时返回where值。?

如何在Oracle SQL中找到记录时返回where值。?,sql,oracle,Sql,Oracle,我有一张顾客桌 我想要的是当我运行查询时,像下面这样的事情 SELECT (<what should be the query here?>) WHERE CustomerID in (2,3,13,18); Customer ID CustomerName ContactName Address 13 null null null 18 null null

我有一张顾客桌

我想要的是当我运行查询时,像下面这样的事情

SELECT (<what should be the query here?>)
WHERE CustomerID  in (2,3,13,18);
Customer ID   CustomerName   ContactName Address
13           null            null        null
18            null            null        null
您可以使用左联接,但需要一个带有值的表。以下是构造该表的一种方法:

select *
from (select 2 as customerid from dual union all
      select 3 as customerid from dual union all
      select 13 as customerid from dual union all
      select 18 as customerid from dual
     ) c left join
     customers c
     using (customerid)
where c.customerid is null;
  

使用表集合表达式并将其左外联接到表:

选择t列值作为客户id, c、 客户名称, c、 联系人姓名, c、 地址 来自表SYS.ODCINUMBERLIST 2、3、13、18 t 左外连接c 在t.COLUMN\u VALUE=c.Customer\u ID上 其中c.Customer_ID为空; 因此,对于样本数据:

创建表Customers客户id、customername、contactname、address作为 选择级别, “客户”级别, “联系人”级别, “地址”级别 来自双重
按级别连接您将需要创建一个临时表,然后对其和客户表执行左连接。这意味着您将取回每个ID记录,并在找不到该记录的地方返回空值。@Harishfysx,您在下面的评论中说我不想对它们全部进行硬编码,但您必须提及in子句的值。对吗?如果有数百个where值呢?我不想全部硬编码?您是直接编写SQL,还是有生成SQL的软件代表您执行此SQL?例如实体框架或JHibernate或其他东西。@Harishfysx。您可以使用电子表格或类似工具构造查询。@Gorodon Linoff-不幸的是,数据在数据库中。它有数千个records@Harishfysx . . . 然后,派生表应该是一个返回您关心的值的查询。无论如何,这更简单。 CUSTOMER_ID | CUSTOMERNAME | CONTACTNAME | ADDRESS ----------: | :----------- | :---------- | :------ 13 | null | null | null 18 | null | null | null