Jump to content

[SOLVED] QUERY withour ORDER


crashmaster

Recommended Posts

Hi there,

 

have 1 problem.

 

For.ex:

 

I have a $str = '11,23,5,15';

 

I use this $str in mysql SQL query:

 

mysql_query("
        SELECT
            *
        FROM
            tab_table
        WHERE
            id IN (".$str.")
    ");

 

but when I am echoing results, results are ordered by id asc (5,11,15,23)... Hot to make query like this without reordering IDs ??? I need ID order like in $str

 

Thank you for any help or suggestion

Link to comment
https://forums.phpfreaks.com/topic/150658-solved-query-withour-order/
Share on other sites

Try this and tell me if there is any errors:

 

mysql_query("SELECT * FROM tab_table WHERE id IN (".$str.") ORDER BY id") or die(mysql_error());

 

without any error.. Query accepted by sql engine without any problem.. by order is stil "BY id ASC" .. ((

Actually, having re-read the question.

 

mysql_query("
        SELECT
            *
        FROM
            tab_table
        WHERE
            id IN ($str) ORDER BY FIELD(id,$str);
    ");

 

should do it.

 

Works !! Thx a lot )

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.