I have a simple select statement like:
SELECT *
FROM [customer] AS [c]
INNER JOIN [customertype] AS [ct] ON [c].[CustTypeKey] = [ct].[CustTypeKey]
INNER JOIN [ProjectCustomer] AS [pc] ON [c].[CustomerKey] = [pc].[CustomerKey]
INNER JOIN [Project] AS [p] ON [pc].[ProjectKey] = [p].[ProjectKey]
INNER JOIN [Address] AS [A] ON [P].ProjectGuid = [A]. [AddressGuid]
WHERE [ct].[CustTypeKey] = 7
AND [c].[Name] = 'Customer'
AND [A].[RegionKey] = 2
OR [A].[RegionKey] = 3
For some reason the WHERE clause is not working correctly. I filter by [A].[RegionKey] = 2 and [A].[RegionKey] = 3. So I want to get all items who have RegionKey 2 and 3, but it only get values with RegionKey 3, and I’m sure I have items with RegionKey 2 too.
What am I doing wrong with this WHERE clause?
Advertisement
Answer
Change this:
AND [A].[RegionKey] = 2 OR [A].[RegionKey] = 3
to this:
AND ([A].[RegionKey] = 2 OR [A].[RegionKey] = 3)
or this:
AND [A].[RegionKey] IN (2,3)
Then make sure the other conditions in the WHERE clause are correct, as it’s likely at least one of them is over-filtering.