问题
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