2

我是 SQL Server 的新手,并尝试使用 SQL Server 为以下场景创建查询。

我有一个名为的表,其中包含可以使用GenericLabels的所有标签 () 的列表。varchar(30)它们用于名为 的表中UserDeviceStatus。我想要给定的标签列表,其中包含GenericLabels但未使用的标签。UserDeviceStatusUserId

我创建了以下查询

select label 
from GenericLabels 
where not exists (select customlabel from UserDeviceStatus where userid = 40)

此查询返回空结果

这是带有单个查询的输出。

select label from GenericLabels

返回

Aux1
Aux2
Aux3
Aux4
Aux5
Aux6

select customlabel from userdevicestatus where userid = 40 

返回

Aux2
Aux3

我想要以下结果

Aux1
Aux4
Aux5 
Aux6
4

3 回答 3

4

您必须链接标签和自定义标签:

select label from GenericLabels 
where not exists (
    select 1 from UserDeviceStatus 
    where customlabel = label 
    and userid = 40
)
于 2013-08-08T23:37:34.467 回答
2

试试这个:

 select label from GenericLabels where label not in (select customlabel from UserDeviceStatus where userid = 40)
于 2013-08-08T23:37:48.480 回答
-1
SELECT customlabel from userdevicesstatus where userid != 40

这应该会给你想要的结果。

于 2013-08-08T23:39:56.803 回答