Totally Zenned
- Join Date:
- Aug 2004
- Posts:
- 732
- Plugin Contributions:
- 0
Quick help with MySQL Query - Selecting OLD Product at Random
Quick question. Since my MySQL coding can get quite sloppy, I am hoping for some nice person to let me know how to do this particular query the most efficient way.
My Goal: I am going to select a product at random, and I want to know when the last time it was sold. Essentially, I am trying to randomly select an old product that is sitting around, so that I can dynamically put it on sale.
I just need to know the best MySQL select statement for finding that product.
Like, would this work and be the best way to call it?
DISTINCT p.products_id, o.date_purchased, o.orders_id, op.orders_id
FROM zen_products p, zen_orders_products op, zen_orders o
WHERE p.products_status = '1'
AND p.products_ordered >0
AND (
o.date_purchased ><= DATE_SUB( CURDATE( ) , INTERVAL 90
DAY )
AND op.products_id = p.products_id
AND op.orders_id = o.orders_id
)
ORDER BY COUNT( op.products_id ) DESC
LIMIT 1
Would that be the best way to accomplish this? Should there be some call to a random number function between 1 and NumProducts in our database, etc?