简体   繁体   English

DB2中的EXCEPT SELECT SQL查询

[英]EXCEPT SELECT SQL Query in DB2

I'm new to DB2 and couldn't find anything relating to EXCEPT queries [except] quite simple examples. 我是DB2的新手,无法找到与EXCEPT查询有关的任何信息[非常简单的示例除外]。 I'm trying to compare values across two tables in order to return values which appear in TABLE_A but not in TABLE_B . 我试图在两个表之间比较值,以便返回出现在TABLE_A而不是TABLE_B The problem is that the columns being compared do not key exactly, but require some manipulation first. 问题是被比较的列不能精确地键,但是首先需要进行一些操作。

ID_1 field from TABLE_A looks like this: 000 999 TABLE_A ID_1字段如下所示: 000 999

ID_2 field from TABLE_B looks like this: 111-000999 TABLE_B ID_2字段如下所示: 111-000999

So by massaging the data from each ID we get the key value of 000999 for this sample row. 因此,通过对每个ID的数据进行按摩,我们获得了该示例行的键值000999 This should result in this row being excluded from the query result as the data is present in both tables. 这将导致该行从查询结果中排除,因为两个表中都存在数据。

SELECT REPLACE (ID_1, ' ','') AS ID_1_TRIM
FROM MYDB.TABLE_A
EXCEPT 
SELECT SUBSTRING(ID_2,5,10) 
FROM MYDB.TABLE_B

This syntax doesn't work, it appears to return all the values in TABLE_A , even though the massaged key values also appear in TABLE_B . 该语法不起作用,即使返回的键值也出现在TABLE_B ,它似乎返回TABLE_A所有值。

have you try to trim may be 你尝试修剪可能是

 SELECT trim(REPLACE (ID_1, ' ','')) AS ID_1_TRIM
 FROM MYDB.TABLE_A
 EXCEPT 
 SELECT trim(SUBSTRING(ID_2,5,10)) FROM MYDB.TABLE_B

or cast 或演员

 SELECT cast(trim(REPLACE (ID_1, ' ','')) as varchar(15)) AS ID_1_TRIM
 FROM MYDB.TABLE_A
 EXCEPT 
 SELECT cast(trim(SUBSTRING(ID_2,5,10)) as varchar(15))  FROM MYDB.TABLE_B

In my iseries your query work, are you sur it's space characteres in ID_1 ? 在我的岛上,您的查询工作是ID_1中的空格字符吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM