SQL is needed to sort the table based on another array list

I need to get a list of items from my table, but in sorted order and the list should be sorted as follows:

Suppose I have fields named as "productid" and "productname" and the table is below:

1. milk 2. pizza 3. boll 

And I have another list in the array, for example

 boll pizza milk 

now I want to sort the list of tables based on the value of the array and get below the list "

 3. boll 2. pizza 1. mike 

Is there any SQl that can do what I need? or I need to do this manually using coding.

thanks

+4
source share
1 answer

You can do this if with the next bit of pseudocode, from your application code

 sqlexecuter.run( "CREATE TEMPORARY TABLE ORDERED_ENTRIES( ORDER_ENTRY NOT NULL AUTO_INCREMENT PRIMARY KEY, PRODUCT_NAME varchar(255) --Put the same amount size as your other table )") 

Then in your code paste them into this table, similar to this

 for(i:lenght(myProductArray)) sqlexecuter.run (" INSERT INTO ORDERED_ENTRIES VALUES("+myProductArray[i]+")" ) 

And then select this

 sqlexecuter.select( " SELECT OE.ORDER_ENTRY, PT.PRODUCT_NAME FROM PRODUCT_TABLE AS PT, ORDERED_ENTRIES AS OE WHERE OE.PRODUCT_NAME = PT.PRODUCT_NAME ORDER BY OE.ORDER_ENTRY "); 
+5
source

Source: https://habr.com/ru/post/1486402/


All Articles