Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
tSQL不在查询中_Sql_Sql Server_Tsql_Notin - Fatal编程技术网

tSQL不在查询中

tSQL不在查询中,sql,sql-server,tsql,notin,Sql,Sql Server,Tsql,Notin,我想获取[interactions]表的ID,但这些ID不能等于[EmailOUT]表。我无法编写查询 Select ID from EmailOut where ID NOT IN (select ID from [172.28.101.120].[GenesysIS].dbo.interactions where media_type = 'email' and type = 'Outbound') 类似的东西。我希望在交

我想获取[interactions]表的ID,但这些ID不能等于[EmailOUT]表。我无法编写查询

Select ID from EmailOut         
where ID NOT IN         
   (select ID from
    [172.28.101.120].[GenesysIS].dbo.interactions 
    where media_type = 'email'
    and type = 'Outbound')

类似的东西。我希望在交互表中显示出站电子邮件,但这些电子邮件可能存在于EmailOut表中。我想把它们去掉。出站电子邮件计数约为300,但此查询结果应小于300。如果要获取[interactions]表的ID,您似乎应该反向查询:

select ID from
[172.28.101.120].[GenesysIS].dbo.interactions 
where media_type = 'email'
and type = 'Outbound'
AND ID NOT IN (SELECT ID FROM EmailOut)
那怎么办

select ID from [172.28.101.120].[GenesysIS].dbo.interactions 
where media_type = 'email'
and type = 'Outbound' 
minus
select ID from EmailOut
试试这个-

SELECT t2.*
FROM [172.28.101.120].[GenesysIS].dbo.interactions t2
WHERE t2.media_type = 'email'
    AND t2.[type] = 'Outbound'
    AND NOT EXISTS (
            SELECT 1 
            FROM dbo.EmailOut t 
            WHERE t.id = t2.id
        ) 

外键和主键的名称是否与前面提到的完全相同?Interactions.ID vs EmailOut.ID?。。您也可以使用NOT-IN而不选择,比如这个示例snip,在这里您可以检查整数:NOT-IN(119、138、158、165),这只是一种测试运算符的方法