SQL Statement - SELECT the inverse of this query

可紊 提交于 2019-11-30 03:28:16

问题


I have this query and i want to select the inverse of what that select.

SELECT Guide.id FROM Guide
INNER JOIN GuideAvailability ON Guide.id = GuideAvailability.guideId
WHERE GuideAvailability.startDate IN (1377946800)
GROUP BY GuideAvailability.guideId
HAVING COUNT(DISTINCT GuideAvailability.id) = 1

Is there a easy way to solve my problem ?


回答1:


Simply use this query:

SELECT * 
     FROM Guide where id NOT IN (
       SELECT Guide.id 
         FROM Guide
         INNER JOIN GuideAvailability ON Guide.id = GuideAvailability.guideId
         WHERE GuideAvailability.startDate IN (1377946800) 
         GROUP BY GuideAvailability.guideId 
         HAVING COUNT(DISTINCT GuideAvailability.id) = 1
      )



回答2:


MAy be you are looking for this

SELECT Guide.id FROM Guide
INNER JOIN GuideAvailability ON Guide.id = GuideAvailability.guideId
WHERE GuideAvailability.startDate NOT IN (1377946800) GROUP BY GuideAvailability.guideId HAVING COUNT(DISTINCT GuideAvailability.id) <> 1



回答3:


You can flip the bool. ;)

I do this when I have a large amount of where filters and there is a specific exclusion set of filters. I write them inside the case and do =1 to exclude and =0 to find what I excluded.

SELECT Guide.id FROM Guide
INNER JOIN GuideAvailability ON Guide.id = GuideAvailability.guideId
WHERE ( case when GuideAvailability.startDate IN (1377946800) then 1 else 0 end) = 0
GROUP BY GuideAvailability.guideId
HAVING COUNT(DISTINCT GuideAvailability.id) = 1


来源:https://stackoverflow.com/questions/19682816/sql-statement-select-the-inverse-of-this-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!