<p>Assume a table with the following columns:</p>

<p><code>pri_id</code>, <code>item_id</code>, <code>comment</code>, <code>date</code></p>

<p>What I want to have is a SQL query that will delete any records, for a specific <code>item_id</code> that are older than a given date, BUT only as long as there are more than 15 rows for that <code>item_id</code>.</p>

<p>This will be used to purge out comment records older than 1 year for the items but I still want to keep at least 15 records at any given time.  This way if I had one comment for 10 years it would never get deleted but if I had 100 comments over the last 5 days I'd only keep the newest 15 records.  These are of course arbitrary record counts and date timeframes for this example.</p>

<p>I'd like to find a very generic way of doing this that would work in mysql, oracle, postgres etc.  I'm using phps adodb library for DB abstraction so I'd like it to work well with that if possible.</p>

Something like this should work for you:
```
```
delete
from
  MyTable
where
  item_id in
  (
    select
      item_id
    from
      MyTable
    group by
      item_id
    having
      count(item_id) > 15
  )
  and
    Date < @tDate
```
```