2 个数据库之间的简单 SELECT 查询不返回任何结果 - 继续显示"Executing query"



我有两个不太复杂的选择查询,这些查询在我在SSM中运行时不会返回任何输出。当我分别运行这两个查询时,我只是看到"执行查询",然后保持很长时间。

[DATABASE1]。[DBO]。[客户] 是存储所有客户数据的地方

[database2]。[邮件]。[emailsent] 是存储电子邮件发送日志的地方,基本上是哪个客户收到了哪个电子邮件。在此表中,仅当发送电子邮件给客户时才存在。如果未发送,则不应该有任何行。

我想做的 - 我试图为所有未收到特定emailIds的客户提供所有客户计数(& customent键(,因此我认为我必须必须2条条款中的2个条件,因为这些客户可能与其他电子邮件ID一起存在,否则它们可能根本不存在于电子邮件表中。

这是我的查询1:

-- query1 to get customer count
SELECT count(DISTINCT [customerkey]) AS My_Count
  FROM [database1].[dbo].[customer]  
  WHERE 
  [signupdate] BETWEEN '2019-01-01' AND '2019-05-05' AND [type] = 1
  AND
  (
  ([accountid] NOT IN (select distinct [accountid] from [database2].[mail].[emailsent]))
 OR
 (
 [accountid] NOT IN (select distinct [accountid] from [database2].[mail].[emailsent] where [id] IN (
'10',
'11',
'12',
'13',
'14',
'15',
'16') )
)
)

这是我的查询2

-- query2 to get all customers
SELECT DISTINCT [customerkey]
  FROM [database1].[dbo].[customer]  
  WHERE 
  [signupdate] BETWEEN '2019-01-01' AND '2019-05-05' AND [type] = 1
  AND
  (
  ([accountid] NOT IN (select distinct [accountid] from [database2].[mail].[emailsent]))
 OR
 (
 [accountid] NOT IN (select distinct [accountid] from [database2].[mail].[emailsent] where [id] IN (
'10',
'11',
'12',
'13',
'14',
'15',
'16') )
)
)

如果您的数据库在链接服务器上,则可能会有所帮助:

SELECT cust.[customerkey], COUNT(cust.[customerkey])
FROM [database1].[dbo].[customer] cust
LEFT JOIN  [database2].[mail].[emailsent] eml ON eml.[accountid] = cust.[accountid]
WHERE eml.[accountid] IS NULL
Group By cust.[customerkey]

最新更新