S
S
strelkovandreyv2019-12-25 15:10:31
MySQL
strelkovandreyv, 2019-12-25 15:10:31

How to update an existing row in MySQL if its field has not changed or add a new one if it has?

Good afternoon, there are 2 identical tables (the first is a table, and the second is a view), consisting of the following columns:
id ; host; hash; title ; timestamp
In the view, information is often added and changed, and in the first one you need to copy it periodically (by a procedure called on a schedule),
such information comes to the second view
1 ; comp1 ; 231812; ACER ; 12345672
; comp2 ; 249735 ; Dell; 1234568
3; comp3 ; 457239 ; LG ; 1234569 The
information is different and it should get identically into the table, but then time passes,
and new data like these
4 appears in the view; comp1 ; 234814; ACER2; 1234589
You need to look at the host field to see if there is the same record with the value comp1 in the table, if it exists and its hash is different, it turns out that something new has arrived, you need to create a new row in the table with these values ​​...
but if line
4 has arrived ; comp1 ; 231812; ACER ; 1239093
i.e. the hash has not changed, then you need to find this line based on host (sorted by id) and the most recent entry (by id) update the values ​​of id and timestamp with new values, i.e. how to simply update the last id-shnik and the date of the information, how would
it be better to do this, and is it really possible to do this with a procedure?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2019-12-25
@strelkovandreyv

Unique composite key (`host`, `hash`) and INSERT ... ON DUPLICATE KEY UPDATE.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question