Home > Net >  Selecting random rows and order by ID
Selecting random rows and order by ID

Time:12-10

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.

CodePudding user response:

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 ;
  • Related