I’m needing to pull 3 rows from a table for a featured section on my site. I first retrieve a list of 3 items that are sold the most from an orders table. Then I need to check to see if there are any left. If so, pull their details from the inventory table. The problem is, I always need 3. If there are not 3 items from the orders table that are still in stock, i need enough to make 3, selected from random. How can I set WHERE id IN (1, 2) and then still pull 1 random record?
I’m needing to pull 3 rows from a table for a featured section on
Share
You can use a UNION to join the records
WHERE id IN (1, 2)and then the second query is your random record.If you provide more details about your query, then I can provide a more detailed answer.
Edit:
Based on your comment you should be able to do something this like:
If you want to be sure you always get 3 results: