Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting random rows and order by ID

Here is the code I tried to select 6 random entries from database and it works fine.

$sql="SELECT * 
      FROM tbl_all_ads 
      WHERE status='online' 
      AND brand='$brand' 
      ORDER BY RAND()  
      LIMIT 6";

But I need these randomly select values to order by id, ASC or DESC order. Here id, is auto increment integer.
Please help me to solve this problem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try:

SELECT * FROM ( 
                    SELECT * 
                    FROM tbl_all_ads 
                    WHERE status='online' 
                    AND brand='$brand' 
                    ORDER BY RAND()  
                    LIMIT 6
             ) as t1  ORDER BY id ASC ;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading