Is there a "NOT HAVING" syntax like "WHERE XXX NOT IN"?

Drewneedshelp picture Drewneedshelp · Mar 9, 2011 · Viewed 25.9k times · Source
  1. I have a few queries get the ID numbers of rows that will be deleted in the future.
  2. The row numbers are put into a string and placed in the query below (where you see "2").
  3. I want the results to ignore the rows (as though they have already been deleted).

    SELECT MAX(T1.id) AS MAXid
    FROM transactions AS T1 
    WHERE id NOT IN ( 2 ) 
    GROUP BY T1.position 
    ORDER BY T1.position
    

My guess is that I need to replace the "WHERE" line with "HAVING", but I cannot find "NOT HAVING" syntax.

The way this query is currently written, it will not return a row for T1.position if the max id for the position is listed in the WHERE clause.

How do I get this query to give me the max ID for the T1.position while overlooking the rows with IDs listed in the WHERE clause?

Answer

geekosaur picture geekosaur · Mar 9, 2011

HAVING id NOT IN (2) should work; [NOT] IN isn't limited to WHERE clauses.