Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 58923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:55:28+00:00 2026-05-10T17:55:28+00:00

Assume a table with the following columns: pri_id , item_id , comment , date

  • 0

Assume a table with the following columns:

pri_id, item_id, comment, date

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

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.

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.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:55:28+00:00Added an answer on May 10, 2026 at 5:55 pm

    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 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a city table that has two columns from_city to_city now lets assume
I have a table with 3 columns (containing Integer values that assume only values
Assume I have the following style table, col1 col2 and col3 have same value
Assume I have a table called table and I have 3 columns, a, b,
Let's assume that I have mytable in mySQL CREATE TABLE `mytable` ( `gender` enum('MALE','FEMALE','UNISEX')
I have a basic recursive table setup in SQL 2008 with the following structure:
I have an SQL table with the following structure: Code1 - int Code2 -
I have following query select * from table1 if table1 contains text type column/columns
I need to synchronize two tables. Let's assume that the tables contain following columns:
I have a table with, let's say, the following columns: Name , Parent Name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.