(SELECT word, description
FROM dictionary
WHERE word LIKE 'xxxx%') AS a
UNION
(SELECT word, description
FROM dictionary
WHERE word LIKE '%xxxx%' AND word NOT IN a.word)
note: the “xxxxx” is the keyword.
I’m working a dictionary application on Android which search through keyword.
The requirement is to put those result starting with the keyword on top.
I come up with the idea of union by sql but I’m not a SQL expert so can someone help me figure out what I have done wrong with the above query.
FYI: I’m using rawQuery() which process the raw SQL statement.
Thanks to community.
There is no guarantee that the
UNIONoperator will order the rows from the two subqueries in the order you assume.UNION ALLdoes not try to find duplicates so it has more chances to work (as you hope to).So this may work (or it may not!) – depending on how
UNION ALLworks internally:It’s better to explicitedly specify the order with
ORDER BY. I think that SQLite supports this: