This is a simple selection from a single table. The goal is to select four random products, one from each of the x categories, with a few "where" restrictions. I tried this:
SELECT pName,
pID
from products
WHERE pDisplay=1
AND pFeatured=1
GROUP BY pCategory
ORDER BY RAND()
LIMIT 0,4
This type of work, but it always returns the same product from any category. I want to change the displayed products while showing only one product for any category.
I also tried:
SELECT DISTINCT(pCategory)
pName,
pID
from products
WHERE pDisplay=1
AND pFeatured=1
ORDER BY RAND()
LIMIT 0,4
I think maybe he needs two choices - first, to get random 4 categories, to select a random row from each of them, but a. I'm not sure how to do this, and b. would prefer to use one request, if possible.
source
share