Question : Combining these 2 queries MS SQL

hi experts.  here is my existing select statement:
SELECT   City, State, Email FROM         Main
WHERE       country = 'united states' and (Rejected = 'false') AND (Remove = 'false') AND (Notes <> 'Block')
ORDER BY  city, LastEmailed, state


I want to add another clause to the WHERE component.
I have another table named dbo.cities that also has columns city, state, and another column named Open.  I need each result in main to check against cities to see if there is a match, and then check to see if its open.  
Select City, State from Cities where Open > 0

So back to my original query, I need to add this logic to the WHERE clause:
If main.city IS FOUND IN cities.ciity and main.state IS FOUND IN cities.state and cities.open = 0 then exclude from the results.  (If cities.open is any other value than 0 then include in the results, also, if no match can be found in cities at all, then include in the results)

Hope that made sense!


 

Answer : Combining these 2 queries MS SQL

Don't look in the WHERE - look in a JOIN.  A LEFT JOIN, to be precise:

SELECT   Main.City, Main.State, Email
FROM         Main
LEFT JOIN cities
    ON Main.City = cities.city
    AND main.state = cities.state
    AND cities.Open = 0
WHERE       country = 'united states' and (Rejected = 'false') AND (Remove = 'false') AND (Notes <> 'Block')
    AND cities.city IS NULL
ORDER BY  city, LastEmailed, state
Random Solutions  
 
programming4us programming4us