首页 > 解决方案 > 从 SQL Server 中具有相等条件的表中选择

问题描述

在此处查看我的数据快照

我想实现以下目标:

select record 
where (`adslocationdescription` = "jawa timur" and `type` = 1) or     
      (`adslocationdescription` != 'jawa timur and `type` = 2)

但如果adscampaigncode有一个等于 ( adslocationdesc= "jawa timur" and type=2) 的记录,则不应选择任何数据。

标签: sql-server-2012

解决方案


类似于(代码未测试):

select * from myTable
where (AdsLocationDescription = 'Jawa Timur' AND (Type = 1 OR Type = 2))
AND AdsCampaignCode NOT IN
-- this subquery gets AdsCampaignCodes that 
-- DO have LocationDescription of Jawa Timur
(select distinct AdsCampaignCode
from myTable
where AdsLocationDescription = 'Jawa Timur')

推荐阅读