Mysql order by values in array

Codes here !

Moderators: egami, macek, gesf

Post Reply
jigga
New php-forum User
New php-forum User
Posts: 2
Joined: Sun Feb 14, 2021 12:53 am

I want to manually sort items from a table. The background is that I have different items listed grouped on item type and then sorted by the id so the article numbers are presented as

Code: Select all

Item type 1 - 4109
Item type 1 - 1342
Item type 1 - 3193
Item type 2 - 6971
Item type 2 - 0484
The sql string is shown below.

Code: Select all

$sql = "SELECT * FROM '".$table."' WHERE model = '".$model."' ORDER BY item, id ASC";
Instead of sorting the article numbers based on id ASC I want to sort them by a defined array like

Code: Select all

$sortItem1 = [1342, 4109, 3193];
$sortItem2 = [0484, 6971];
The items shall be listed grouped on item and the article numbers shall be sorted as the arrays $sortItem1 and $sortItem2. The result should then be

Code: Select all

Item type 1 - 1342
Item type 1 - 4109
Item type 1 - 3193
Item type 2 - 0484
Item type 2 - 6971
I don't think the code below is possible but I want it to be sorted like

Code: Select all

ORDER BY item, $sortItem1, $sortItem2
What is the best solution for this?
Post Reply