Delete table data when it reaches a certain number

Asked

Viewed 34 times

0

Fala galera!

I have a table called tb_chat with the id_message fields,name,message,date,time.

how do I delete data from this table when it reaches '6' records?

1 answer

0

Since it is not possible to run a delete using the same table in a subquery, you should do this in two steps:

First, you can select the first item of the tb_chat table based on the amount of records. This can be done in several ways, e.g.:

  1. SELECT MIN(id_message) FROM tb_chat HAVING COUNT(id_message) > 6
  2. SELECT id_message FROM tb_chat HAVING COUNT(id_message) > 6 ORDER BY id_message ASC LIMIT 1

After that, just run delete with the ID returned from the query (if the query does not return any record, there are no more than 6 items in the table)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.