1

I am currently implementing a search feature in an appliction to take a user input and query the database for similar results. I have created the following database query:

String proQuery = "SELECT * FROM " + DATABASE_TABLE + " WHERE "
                + KEY_NAME  + " LIKE '%" + keyword +"%'";

Obviously this query has a number of limitations due to a very generic sql statement. For example, if I start typing something in the middle of the word, it will show up first in the list if it has a lower row number in the database.

Are there libraries that help make a more intelligent search feature than this very generic sql statement? I am not sure what keywords could describe this functionality what I'm looking for, is there some field of "search algorithms" for android?

Mark D
  • 163
  • 1
  • 15
  • 2
    Same question asked here: [best match for sql](http://stackoverflow.com/questions/18725941/mysql-order-by-best-match) – Mehmet Hanoğlu Jun 30 '16 at 16:35
  • Is there a third party library that handles this kind of logic? Or is this more of a "implement yourself and learn as you go" kind of situation? – Mark D Jun 30 '16 at 16:39

1 Answers1

0

Maybe UNION is what you're looking for:

SELECT * FROM (
  SELECT * FROM table_name WHERE keyname LIKE 'keyword%' ORDER BY keyname
) alias_t1

UNION ALL

SELECT * FROM (
  SELECT * FROM table_name WHERE keyname LIKE '%keyword%' AND keyname NOT LIKE 'keyword%' ORDER BY keyname
) alias_t2;
r3npi2
  • 119
  • 6
  • I didn't see @marzochi comment, but best answer in his question solves all the cases: http://stackoverflow.com/questions/18725941/mysql-order-by-best-match – r3npi2 Jun 30 '16 at 17:16