mysql adjust value temporarily. Is this possible?
I have a module on my website which lists products by their popularity and the list changes quite regularly. I'd like to automatically discount the top 3 popular products, this is easily done through changing the discount column to 5 for the 3rd product 10 for the 2nd and 15 for the top. However, as the list is regularly changing I need this discount id to revert back to whatever it was before it was overwritten with the new discount id.
Would I have to create another column and call it something like old_discount and开发者_如何学Go transfer the old discount to that before changing the new one and then have it copy it back when it is not longer in the top 3?
I'm hoping someone has an easier way this might be done, any help at all would be appreciated. Thanks for your time.
There's probably no way to get around adding something to manage the top three discounts. As far as actual implementation, there's a couple of ways you could do this that come to mind:
- Create a table just to store the current list of top 3 products (or a one-row table with three fields for the top three). Refresh this table with the top 3 when you automatically check. Then in your code that calculates prices, reference this table first to see what is being discounted before doing the rest of the calculation.
- If you are going to add a column to the table, I would add a
temp_discount
instead ofold_discount
so you can always leave the original alone. Then your code should check iftemp_discount
is set, use that, otherwise use the normal discount column. Then you simply settemp_discount
to 0 before setting the next top 3 products every time you want to refresh.
精彩评论