4

I need to get list of item from my table but in sorted order and list must be sorted as below:

Assume I have fields named as "productid" and "productname" and table as below records

1. milk
2. pizza
3. boll

And I have another list in array such as

boll
pizza
milk

Now I want to sort table list based on array value and get below list"

3. boll
2. pizza
1. mike

Is there any SQL which can do what I need? Or I need to do it manually via coding.

1 Answer 1

5

You can do this if with the following bit of pseudo code, 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 insert them into that table, in a similar way to this

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

And then select like 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
");
Sign up to request clarification or add additional context in comments.

1 Comment

As an extra tip, you should do a batch insert if you can, to insert everything in one go. Similar to this one: stackoverflow.com/questions/10429348/batch-insert-sql-statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.