我是SQL脚本的业余爱好者,我需要你的帮助。我有以下查询,它给了我4行。
SELECT A.Id,
A.InterfaceId,
I.DeviceId,
I.TypeId,
A.AnoType
FROM I
INNER JOIN A ON I.InterfaceId = A.InterfaceId
INNER JOIN D ON I.DeviceId = D.DeviceId
WHERE I.AccountId = '500'
AND D.UserId = '1000'
此查询的结果是:
Id InterfaceId DeviceId TypeId AnoType
----------------------------------------------------------------------------------------------------
1216046 | C4BEB5D8-8749-4831-92F4-070CF58F7C81 | B0E8C423-1B01-4AD5-A383-B0E584E6176C | 1207 | -40 |
1216047 | F00F87BD-F1B4-4DAD-801A-E022403831D5 | B0E8C423-1B01-4AD5-A383-B0E584E6176C | 1208 | -40 |
1216329 | F00F87BD-F1B4-4DAD-801A-E022403831D5 | B0E8C423-1B01-4AD5-A383-B0E584E6176C | 1208 | -3 |
1225762 | F00F87BD-F1B4-4DAD-801A-E022403831D5 | B0E8C423-1B01-4AD5-A383-B0E584E6176C | 1208 | -20 |
----------------------------------------------------------------------------------------------------
我的问题是我需要一个查询,它返回AnoType不是-3的行,而其InterfaceId在其他行中没有AnoType的-3值。例如,在第三行中,AnoType为-3,因此我的查询应仅返回第一行而不返回其他行。 任何帮助将不胜感激。
提前致谢,
吉安
答案 0 :(得分:1)
SELECT A.Id,
A.InterfaceId,
I.DeviceId,
I.TypeId,
A.AnoType
FROM I
INNER JOIN A ON I.InterfaceId = A.InterfaceId
INNER JOIN D ON I.DeviceId = D.DeviceId
WHERE I.AccountId = '500'
AND D.UserId = '1000'
AND A.AnoType <> -3
AND A.InterfaceId NOT IN (SELECT InterfaceId
FROM A
WHERE AnoType = -3)