Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to display exactly 6 random 'entertainment' entries, but with my current query it's getting a random number between 1 and 6, and displaying that number of entries. How do I update this query in order to make it display exactly 6 random entertainment entries from my Articles table? Also, I am trying to avoid ORDER BY RAND().

table Articles
 - id (int)
 - category (varchar)
 - title (varchar)
 - image (varchar)
 - link (varchar)
 - Counter (int)
 - dateStamp (datetime)


What I have tried:

PHP
 SELECT
    r1.*
FROM
    Articles AS r1
    INNER JOIN (SELECT(RAND() * (SELECT MAX(id) FROM Articles)) AS id) AS r2
WHERE
    r1.id >= r2.id
    AND r1.category = 'entertainment'
LIMIT 6;
Posted
Updated 1-Apr-16 18:25pm
Comments
PIEBALDconsult 2-Apr-16 0:05am    
ORDER BY RAND() ?

Use below code.
Order by newid()
 
Share this answer
 
SQL
Use below code.
<pre>Order by newid()</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900