Reputation: 4253
I have a visit table with id, user, visit, date fields
. To show sellers the last consumers that visited some product.
INSERT into visits (user, visit, data) VALUES (?,?,now());
for example, user='marie'
visit a product from user 'josh'
.
user=marie
visit=josh
data=datetime
My problem is if user come to this page many times it will insert the same thing many times. marie, josh, date.
I'd like to insert only if it has past 5 minutes from the last equal insert (marie, josh). if any user is diferent it can insert normally for the first time, after this, count 5 minutes to insert the same thing again.
marie - josh - 5:00:00
marie - chloe - 5:00:10
chloe - josh - 5:00:20
marie - josh - 5:05:00 //will only insert because it has past 5 minutes from last marie-josh.
any ideas?
Upvotes: 0
Views: 119
Reputation: 781350
Use INSERT ... SELECT
with a WHERE
clause that tests whether the time has expired.
INSERT INTO visits (user, visit, data)
SELECT ?, ?, NOW()
WHERE NOT EXISTS (
SELECT 1
FROM visits
WHERE user = ? AND visit = ? AND data >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)
)
Upvotes: 2