0

I am trying to structure a MYSQL query so that I update a row if a where condition is met else I perform an insert.

So I am collecting stats on a per hour basis, so if I already have a a row for the current hour I simply update the stats. If not I create a new row for the new hour.

I would normally do an initial query to check if a row exists, then either update or insert on the second query.

Is there a way to combine this into 1 query?

ORStudios
  • 3,115
  • 8
  • 38
  • 65
  • Assuming the timestamp is a unique index, use `INSERT INTO ... ON DUPLICATE KEY UPDATE ...` – Barmar Jul 10 '21 at 09:19

0 Answers0