-3

I am trying to make a coupon system, but I got stuck with something. I want to make a function that gets all data from coupon_session where the time (datetime) is older than 30 minutes. For each of the results, I want to fetch the "code" from the row, and after that I want it to delete the row.

I've already figured out how to retrieve the data, but the rest is kinda hard for me. Could you guys help a bit?

This is my code for fetching all > 30 min old rows:

mysql_query("GET * FROM `coupon_session` WHERE TIMESTAMPDIFF(MINUTE,time,NOW()) > 30");
itoctopus
  • 4,133
  • 4
  • 32
  • 44

1 Answers1

0

The code to fetch what you want is:

select cs.code
from coupon_session cs
where time < date_sub(now(), interval 30 minute);

Deleting is another problem, because the value of now() changes. You have several choices, such as:

  • Get the list of ids to delete in your application and delete those (probably the safest method).
  • Calculate "now" in your application and use the same value for the select and delete (also safe).
  • Calculate the maximum time returned by the select and use that for the subsequent delete.
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Doesn't that question return times that is at most 30 minutes old? OP wanted "older than 30 minutes"... or am I missing something fundamental? – Erk Sep 14 '16 at 17:17