简体   繁体   English

如何通过输入字母排序SQLite查询的结果?

[英]How to order the result of SQLite query by input alphabet?

String sql = "SELECT * FROM " + TABLE_NAME +
                " WHERE " + ENGLISH + " LIKE ? ORDER BY " + ENGLISH + " LIMIT 100";

try {
            cursor = db.rawQuery(sql, new String[]{"%" + englishWord + "%"});

For example, in the column ENGLISH , there are "ice", "dice", "vice" etc. When I input the word "ice" in englishWord , I will get the result arranged firstly by "dice", followed by "ice" and "vice". 例如,在ENGLISH列中,有“ ice”,“ dice”,“ vice”等。当我在englishWord输入单词“ ice”时,首先得到的结果将是“ dice”,然后是“ ice” ”和“副”。 But I would like "ice" to be on the first order because it has the exact word. 但是我希望“ ice”处于一阶,因为它有确切的词。 How I can accomplish this? 我怎样才能做到这一点?

Use LENGTH Function to get Exact Matched Phrase , This Should be your Query 使用LENGTH函数获取精确匹配的短语,这应该是您的查询

String sql = "SELECT * FROM " + TABLE_NAME +
                " WHERE " + ENGLISH + " LIKE ? ORDER BY LENGTH(" + ENGLISH + ") LIMIT 100";

To order exact matches first, add another ORDER BY expression that returns a smaller number for exact matches: 要先对完全匹配进行排序,请添加另一个ORDER BY表达式,该表达式将为完全匹配返回较小的数字:

rawQuery("... ORDER BY "+ENGLISH+" != ?, "+ENGLISH+"...",
         new String[]{ "%"+englishWord+"%", englishWord });

English != ? is a boolean expression; 是布尔表达式; it returns either 0 or 1. 它返回0或1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM