开发者

Android : Nearest match query from SQLite DB if input string value is bigger than any of the DB values?

开发者 https://www.devze.com 2023-02-14 12:33 出处:网络
I have DB entries: James Andy Bob David For input string \"Bobby\" i want t开发者_如何学编程o return the \'Bob\' row.

I have DB entries: James Andy Bob David

For input string "Bobby" i want t开发者_如何学编程o return the 'Bob' row. OR for input string "Candy" i want to return 'Andy' row.

I am using Android cursors but can also run the raw query.


You might want to try something like this:

SELECT name
FROM names
WHERE name LIKE '%andy%'
OR 'andy' LIKE ('%' || name || '%')
ORDER BY abs(length('andy') - length(name)), name
LIMIT 1

It will select the shortest match containing the string "andy" or the longest match that is contained within "andy", using alphabetical order as a tiebreaker. To get all rows remove the LIMIT clause.

0

精彩评论

暂无评论...
验证码 换一张
取 消