11

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 ?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Dev'Dev
  • 245
  • 1
  • 4
  • 9
  • 1
    Define "Inverse"? Give examples. – NoChance Oct 30 '13 at 12:57
  • 1
    It depends on what you mean by the inverse. One answer is change "IN" to "NOT IN". Another answer is to change "HAVING ... = 1" to "HAVING ... <> 1". Or do both. What do you want? – D Mac Oct 30 '13 at 12:57
  • This query returns the id of the Guide that correspond to my condition. I want to get all guide excepts the ones that are selected in this query – Dev'Dev Oct 30 '13 at 13:02

3 Answers3

23

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
      )
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

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
Hisham
  • 455
  • 4
  • 16
0

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
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135