2011-03-17 97 views
1

我无法找到查询。SQL查询Where子句问题

我有一个表'NotificationExcludes',其中包含1列'名称'。

我有一个表'NotificationEvents',其中包含列:Id,说明。

现在我需要选择所有NotificationEvents,其中描述不是以'NotificationExcludes'中包含的值开始的。

小例子:

NotificationExcludes包含:

Name 
The instance was terminated by 

NotificationEvents:

ID Description 
1 There was a failure on .. 
2 The instance was terminated by administrator 

现在我需要除非描述与被保存在一个数值开始选择一切“ NotificationExcludes'。

我已经试过

Select Id, Description 
from NotificationEvent 
WHERE Description NOT IN (select Name form NotificationExcludes) 

但2个问题与:

1 The query obviously fails because 'select Name form NotificationExcludes' returns more than 1 record 
2 The statement should contain a Like statement where I can use the '%' key. So where description not like 'The instance was terminated by%' can be used somehow 
+0

我想你会需要这个光标。您的代码是否在存储过程中? – 2011-03-17 13:31:16

回答

4
SELECT Id,Description 
FROM NotificationEvents ne 
WHERE NOT EXISTS(SELECT * 
       FROM NotificationExcludes nx 
       WHERE ne.Description 
       LIKE nx.name + '%') /*<--Might need Concat or || here 
              dependant upon RDBMS*/ 
+0

好的,谢谢:) – 2011-03-17 14:30:07

2

一种解决方案是JOINLIKE条款上的名字,与%追加。

SELECT * 
FROM NotificationEvents ne 
     LEFT OUTER JOIN NotificationExcludes nex 
      ON ne.Description LIKE nex.Name + '%' 
WHERE nex.Name IS NULL