samedi 4 avril 2015

How to give priority to one table in Sqlite

I have two tables. In those tables, my stop id might be in one of them or both of them. If my stop id doesnt exist in B, it should pick from A. And if it doesnt exist in A, it should pick from B. And if stop id exist in both tables then I want to give priority to B and retrieve the row from B. Below query always returns the value from B. Can you help me out to fix this?



String selectQuery = "SELECT "
+ stop_id + ","
+ name + ","
+ latitude + ","
+ longitude + ","

+"1 as "+priority
+" FROM "+ A

+" UNION SELECT "
+ stop_id + ","
+ name + ","
+ latitude + ","
+ longitude + ","
+"2 as "+priority
+" FROM "+ B
+" WHERE " + stop_id + " =? "
+" ORDER BY "+priority
+" LIMIT 1 ";


Cursor cursor = db.rawQuery(selectQuery, new String[] {id});

Aucun commentaire:

Enregistrer un commentaire