Question with query results ORDERING

I am currently creating an array of identifiers and requesting this array using implode as follows:

$sql = "SELECT * FROM item_bank_tb WHERE item_id IN(" . implode(',', $ids) . ")";

the $ids array is constructed so that the identifiers are in a specific order. However, the results of this query are not in that order. I assume that they are all in the same query, the results are displayed in the order in which they were located (ascending).

Is there any way around this? (except for including a field that I can ORDER BY)

thank you very much.

+4
source share
5 answers

Take a look at this example. You must use the field () function.

 SELECT * FROM item_bank_tb WHERE item_id IN(1,3,2) order by field(item_id,1,3,2) 

so you can receive your goods in the desired order.

+8
source

Seriously, the problem is with adding ORDER BY item_id

0
source

If you want SQL query strings to be returned in a specific order, you must include ORDER BY in the query.

0
source

Use ORDER BY to sort the results. You can have ORDER BY DESC (descending values ​​from higher to lower) or ORDER BY ASC (ascending from lower to upper).

For instance:

 SELECT colName,colEmail FROM tblUsers ORDER BY id ASC 

More details here:

http://www.w3schools.com/sql/sql_orderby.asp

0
source

you can build a temporary table "orderer" (if it does not have too much data) instead of an id array, you can insert two values ​​into the temp table: ID, order
and then join in and then request "order in order"

0
source

All Articles