Subtract MYSQL Dates

Asked

Viewed 708 times

0

I want to delete from the table users with the same name, but only those that the difference of the database date and the current date (at the time of select) are for example, less than 5 minutes.

The structure of my database is :

Login | Nome  |        Hora 

    0       joao    2015-05-30 12:05:06
    1       joao    2015-05-30 12:13:06
    2       joao    2015-05-30 12:07:06

As the subtraction of the dates between Login 0 and Login 2 of 2 minutes, it would delete the most recent one, which is login 2.

I am using the following Query to delete equal names :

DELETE a FROM tabela AS a, tabela AS b WHERE a.nome=b.nome AND a.login < b.login 

I am using MYSQL ,thank you !

  • If that’s your chart, what would be the result you want to get?

  • so,I want it to delete the same names, only if the current date - the date in the database is less than 5 minutes. if they have 5 equal names, but one of them has 2 minutes to the other, delete it. the difference between dates has to be greater than 5 minutes

  • What about Login1? Why isn’t it deleted? It’s newer than Login2?

1 answer

1

Try it like this

delete t1 
from tabela t1 
inner join (
   select nome, count(nome)
   from tabela
   group by nome
   having (count(nome) >= 2) 
) as x
on x.nome = t1.nome
where date_add(t1.hora,interval 5 minute) > now();

This query deletes records with the same name from the table if the difference between the date in the table (Time column) and the current date (like()) is less than 5 minutes.

  • Hello, he’s deleting where it’s less than 5 minutes, but he’s setting the time for the same as the current one. For example : 2015-05-30 21:58:20 2015-05-30 21:30

  • Then you need to, after running delete, run an update statement to update the time of the records that have not been deleted.

  • I’ll explain what I need: I have a java program with a biometric reader, and every time someone puts their finger on it, it sends the name of the person and the time to the database. , however, it sends several times in a row.Since I couldn’t solve it in java, I will correct it at the time of insertion. then, when he sent the name and time, if he had the same name and an hour within seconds, he deleted the oldest and left the new one. He wouldn’t just delete if the time he did the insertion was more than 5 minutes from the current one, so he would.Thanks for continuing to help me out !

  • The insertion and delete: http://pastebin.com/SyVN9fBpcode link

  • But isn’t that what the query does? You only need to limit so that only records are deleted for the user who just put his finger on the reader. delete.... Where date_add(t1.hora,interval 5 minute) > now() and name = $nomee; Then you can do a normal Insert like these in your example http://pastebin.com/SyVN9fBp

  • 1

    I love you Cara, !!

  • I’m glad it worked :)

Show 2 more comments

Browser other questions tagged

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