Remaining minutes between field and dateAtual

Asked

Viewed 100 times

1

I am trying to create a direct query in mysql where I must return some value if the time interval of the current date and my datareg field is 02 minutes.

SELECT * FROM tabelax WHERE grade = 19 AND NOW() <= DATE_SUB(datareg, INTERVAL 02 MINUTE)

I tried to use now() with date_sub my datareg field, but I think I’m floating.

I also tried with DATEDIFF , but is not returning

SELECT * FROM tabelax WHERE grade = 19 AND DATEDIFF(NOW(), datareg) < 2

Any suggestions ?

2 answers

1


Try with this query

SELECT *
FROM tabelax
WHERE datareg > NOW() - INTERVAL 02 MINUTE

I await reply :)

Edit:

SELECT *
FROM tabelax
WHERE 
datareg >= NOW() - INTERVAL 02 MINUTE AND 
datareg <= NOW()
  • By the brother thanks for your attention, A boy almost worked here but on the contrary, he’s returning me when minutes are longer than 02 minutes. Explain better, assuming: datareg: 2019-10-10 09:40:00 current date: 2019-10-10 09:42:00 If the result of datareg-datatual > 0 and <=2 then it would return me result, otherwise not. Via php is good to do this, now straight into mysql I’m catching rsrs I’ve read and reread some of the documentation from the conditional part, but it’s still unclear.

  • And turning the sign? <

  • opa said wrong, datareg would be 2019-10-10 09:40:00 datatual would be 2019-10-10 09:38:00 , that is, subtracting 2 minutes left, I tried this but when the date is for example 09:41 is not to show result, only show as long as it is between 09:38 and 09:40 ... AND (final_status2 >= datatual - INTERVAL 2 MINUTE AND final_status2 <= datatual) , i.e., I am not able to condition this part

  • Get it, try the second query

  • Ufa worked, I added one more condition like you spoke, and reversed the current date of place. Valeuuuuuu too much there brother, broke a tree and helped me think rsrs. ... WHERE now() >= datahreg - INTERVAL 2 MINUTE AND now() <= datahreg All together and stay with God ;)

  • Nice to help you

Show 1 more comment

1

The DATEDIFF will not work for your case because, according to documentation, the result value is expressed in days, so it will not be possible to check the difference in minutes.

An alternative is to subtract the current date from the column datareg using the function TIMESTAMPDIFF which has a parameter for you to set the time unit (in the case MINUTE) and thus the result will be expressed in minutes:

SELECT *
  FROM tabelax
 WHERE TIMEDIFF(MINUTE, NOW(), datareg) <= 2;

TIMESTAMPDIFF

Returns datetime_expr2 − datetime_expr1, Where datetime_expr1 and datetime_expr2 are date or datetime Expressions.

In free translation:

Returns datetime_expr2 − datetime_expr1, where datetime_expr1 and datetime_expr2 are expressions of the kind date or datetime.

  • Hello Sorack thanks for the attention there too brother. face I even tried to use timestampdiff minute, but INTERVAL is what is getting me closer to the result I need. is that I have the datadatareg: 2019-10-10 09:40:00 and supposing that now would be: 2019-10-10 09:38:00 need that between 09:38 and 09:40 that has that 2 minute difference it returns me true if it is 09:37 or 09:41 it would not return anything. I tried the syntax ... AND (final_status2 >= datatual - INTERVAL 2 MINUTE AND final_status2 <= datatual) in this case it almost worked

  • What happens is that your registration date is longer than the current date (which would be impossible because in this case the record would be coming from the future), but in this scenario that you mentioned, just reverse the dates in the TIMEDIFF, placing the current date as the first parameter

  • yes yes true, mind was bitolada and did not pay attention to that rsrs. Poxa anyway was worth too much for the tips.

Browser other questions tagged

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