2

I coder for iOS and I use SQLite for my data storage.

For now i have two tables in my database. This is table_1 and table_2 (it's easier for understanding)

About struct:

table_1 contain id field (int type) and some_value field (text type)

table_2 contain id field (int type) and table_1_id field (int type)

My question is next: how to select all row from table_1 via array table_1_id field table_2, and this is must SELECT with ORDER BY. I mean order by array.

For example:

if table_2 -> table_1_id contain value 5, 1, 10, 3, 15, 2

the result of output must be in this order.

For now my query is simple:

SELECT * FROM table_1 WHERE id IN (5, 1, 10, 3, 15, 2)

but the select not return order for this query it return data order by 1, 2, 3, 5, 10, 15.

I think I make relation between my table and make SELECT from table_2 uses ORDER BY, but I don't know how to do this correct.

Thanks in advance!

(Very important for me return this order - 5, 1, 10, 3, 15, 2)

1 Answer 1

1

I find some result in stackoverflow

link

This is work for me:

SELECT * FROM "comments" WHERE ("comments"."id" IN (1,3,2,4)) ORDER BY id=1 DESC, ID=3 DESC, id=2 DESC, ID=4 DESC

This is hard code but I create this query string programmatically in cycle and this work great.

Sign up to request clarification or add additional context in comments.

Comments

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.