WHERE items ARE in Category_1 OR THEN in Category_2??

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

WHERE items ARE in Category_1 OR THEN in Category_2??

Post by simonmlewis »

Hi.
We have a load of products and want to show best sellers from the category a product is it.

ie. if you sell cars, and you are hundreds of Fords. You want to show a particular Ford page, but then show all your best selling fords.

What if you run out of Fords? You want to then also maybe show Jaguar's or ANY OTHER cars in your Lot.. So your query is a randomly selected query for about 16 items, firstly looking for those Fords in the system, but if you only have 4 Fords, it then finds ANY (*) other products randomly.

Code: Select all

$result = mysql_query ("SELECT * FROM products WHERE best = 'yes'  AND pause = 'off' AND photoprimary IS NOT NULL ORDER BY RAND() LIMIT 0, 12");
Is there something I can add to this script that will do that?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: WHERE items ARE in Category_1 OR THEN in Category_2??

Post by califdon »

I don't think I would do that. If they are sorted randomly, the primary items might not make the Limit cut, which I would think would defeat the whole purpose. I would probably use a UNION query, the first part of which recovers the items you know you want, then the second part could be a random selection of others, excluding the criteria used in the the first part. In case you are not familiar with UNION queries, a search on Google will yield tons of references.
Post Reply