E
E
entermix2016-04-13 19:08:05
MySQL
entermix, 2016-04-13 19:08:05

How to get rid of duplicate entries?

There are sl. Structure:
page_statistics
id, page_id, date, visitors, ...
When a record is inserted into another table, a trigger is called to add/update (visitors + 1 if the record exists). The problem is that there are no unique fields in the structure, i.e. there may be different announcements for each day
For example:

1, 11, 10.04.2016, 117
2, 22, 10.04.2016, 172
3, 11, 12.04.2016, 56
4, 11, 13.04.2016, 32

That is, ON DUPLICATE KEY UPDATE is not suitable, how to be in this case? I understand correctly that you can't do without an additional select?
CREATE TRIGGER `after_insert_page_visits` AFTER INSERT ON `page_visits`
 FOR EACH ROW INSERT INTO `page_statistics` (page_id, date ,visitors, created) VALUES (NEW.page_id, UNIX_TIMESTAMP(CURDATE()), 1, UNIX_TIMESTAMP()) ON DUPLICATE KEY UPDATE visitors=visitors + 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2016-04-13
@entermix

Put a unique index on page_id + date

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question