Search code examples
phphtmlmysqlrandomsequential

mysql query ordering by different column depending on value of another column


enter image description here

I have tried this code but it is not working.

$select=$conn->query("select * from tbl_testquestion ORDER BY SUBSTRING( type_id=0 qu_id RAND() ) , type_id=1 qu_id ASC");

please suggest edit if question not reached the standards


Solution

  • you'll want to use a case statement

    SELECT *      
    FROM tbl_testquestion 
    ORDER BY 
        CASE 
            WHEN type_id=0 THEN RAND() 
            WHEN type_id=1 THEN qu_id 
        END ASC