Question:
there is a table with the following structure:
-----------------------------
ID IP Timestamp
-----------------------------
1 127.0.0.1 1111111111
2 127.0.0.1 1111111112
You need to do the following: new records should not be written to the table, for which the IP
corresponds to the IP
in the table, but the Timestamp
in the table should only be updated , otherwise – a new record with a new IP
is added
-----------------------------
ID IP Timestamp
-----------------------------
1 127.0.0.1 1111111112
tried putting индекс->unique
, but then Timestamp
is not updated
how to implement this business without sending 2 requests?
Answer:
I propose to put a unique key on the ip
column, set AUTO_INCREMENT on id
( but it is better to remove this column altogether) and use a query like INSERT ... ON DUPLICATE KEY UPDATE
insert into `table_name` (`ip`, `last_modified`)
values ($ip, UNIX_TIMESTAMP())
on duplicate key update `last_modified`=UNIX_TIMESTAMP()