I’m working with a pseudo sessions table in MySQL. The table looks like this currently:
id | key | value | metadata
The id is the user that the session belongs to and metadata is the IP Address of the user. The idea behind this is that each user can be logged in multiple times from different IP Addresses. I’m wondering if REPLACE INTO can Replace values only where id = userid, key = key, AND metadata = ip_address so ideally we can end up with something like this:
id | key | value | metadata
1 test avalue 127001
1 test bvalue 19216801
1 test cvalue 19215810
Is something like that possible?
If you have a
UNIQUEindex orPRIMARY KEYdefined across those three columns, then yes you canREPLACE INTOit. Add the index if you do not already have it:If you already have a PK defined, create a composite
UNIQUEindex across those columns:More on the MySQL
CREATE INDEXsyntaxHere’s a little demonstration.