MySQL Forums
Forum List  »  Newbie

Re: Noob question - simplist way to store last 4 viewed products?
Posted by: Rick James
Date: November 19, 2009 10:14PM

You are trying to keep no more than 4 items in the table for each user?

Alas no OFFSET available on DELETE.

DELETE FROM last_viewed
    WHERE mem_id = '$mem_id'
      AND date <=
        ( SELECT date
             FROM last_viewed
             WHERE mem_id = '$mem_id'
             ORDER BY date DESC LIMIT 4,1
        );
I believe this technique has the following features/bugs:
* It will simply do nothing if there are 4 or fewer items.
* It will delete _all_ rows beyond the latest 4.
* It will screw up if you have duplicate dates. This might be resolvable if product_id is unique and in chronological order (replace date with product_id).
* It won't work on 4.0 because of the subquery. (There are workarounds.)

Options: ReplyQuote


Subject
Written By
Posted
Re: Noob question - simplist way to store last 4 viewed products?
November 19, 2009 10:14PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.