0

Im using Android Sqlite and want to execute two queries and combine the result. Everything works fine if the sorting is done for the combined result. But i want the sort order for each query separate and then combine the result.

This is what i found from SQL: SELECT with UNION, ORDER BY and LIMIT and validated the query on http://sqlfiddle.com/#!5/98b6d/16 and works fine.

select * from (select * from supportContacts where (id=1) order by id asc)
UNION ALL
select * from (select * from supportContacts where (id!=1) order by RANDOM())

But how will i execute this query on android sqlite ContentResolver.query() or is there any way using rawQuery .

Note: I'm using this query to get all the audio files from Media Provider for MediaStore.EXTERNAL_CONTENT_URI , but i know the id of a audio file , so my first query will get only single audio and second query will get all the audio excluding the known audio id.

Community
  • 1
  • 1
Libin
  • 16,967
  • 7
  • 61
  • 83

1 Answers1

0

You can use buildUnionQuery method from SQLiteQueryBuilder.

For more details, check the following link https://developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html#buildUnionQuery(java.lang.String[], java.lang.String, java.lang.String)

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Ani
  • 1,041
  • 6
  • 15