I am working on customized shopping cart application.
It have a hits table:
id int
product_id int
hit_time datetime
session_id varchar
Now when someone views a product, I record a hit.
I need a query such that, record should be inserted only if hit_time has a difference of 30 mins (for same product_id, and session_id)
For example:
When recording first hit
id product_id hit_time session_id
------------------------------------------------------------------------
1 1 2011-01-01 06:30:00 abcxyzmno
2 5 2011-01-01 06:32:00 abcxyzmno
Now, if visitor with same session_id, visit product page with product_id =1 in less than 30 mins, then data should not be inserted
So below row should not be inserted (because there is difference of 5 mins only)
3 1 2011-01-01 06:35:00 abcxyzmno
But if visitor visit after 30 mins, for same product, it should be recorded
3 1 2011-01-01 07:01:15 abcxyzmno
I want single insert query
I DON’T want 2 query, first to select max-time from table, and then insert it
Any help?
Are you looking for something like this?
Where
$product_idand$session_idare the product and session IDs.The
NOT EXISTSpart takes care of the case where (product_id,session_id) hasn’t been recorded yet and the second part takes care of the case where (product_id,session_id) is there but it was longer than 30 minutes ago. And, of course, theLIMIT 1in case (product_id,session_id) is there and older than 30 minutes ago and appears multiple times.