如何写一个范围查询SQLite中的Andr​​oid? [英] How to write A Range Query in Sqlite Android?

查看:273
本文介绍了如何写一个范围查询SQLite中的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有2个字段的表:编号说明,我想从一个描述号我请求。

例如:我请求22号但是在数据库中,有没有行匹配值22

我需要编写一个查询来获取值 +5的范围和数量-5 获取最近的数据 $ P $数据库psent之间。也就是说,我想获得RTY作为说明,当我要求22。

 表民|说明
-------- + -----------
25 | ASD
18 |邂逅相遇,
21 | RTY


解决方案

这应该理想工作。将返回一个光标与所有从表中的结果。

  db.rawQuery(SELECT起价TABLE_NAME其中num BETWEEN+(inputNumber范围)+和+(inputNumber +范围)+ORDER BY(Num-+ inputNumber +),NULL);

编辑:我很高兴上述查询适用于you.But我觉得这个查询是正确答案给你具体问的问题

  db.rawQuery(SELECT起价TABLE_NAME其中num BETWEEN+(inputNumber范围)+和+(inputNumber +范围)+ORDER BY ABS(Num- + inputNumber +),NULL);

以绝对值避免负面差异

Suppose I have a table with 2 fields: Number and Description, I want to get the description from a number I request.

For example: I request the number 22. But in the database, there is no row matching the value 22.

I need to write a query to get the values ranges between +5 and -5 of that number and getting the nearest data present in the database. That is, I want to get "rty" as the description when I request 22.

Table

Num     |Description
--------+-----------
25      |ASD
18      |wert
21      |rty

解决方案

This should ideally work. Will return a cursor with all the results from the table.

db.rawQuery("SELECT Description FROM Table_Name WHERE Num BETWEEN "+(inputNumber-Range)+" AND "+(inputNumber+Range) +"ORDER BY (Num- "+inputNumber+")", null);

Edit: I am glad the above query works for you.But I think this query is the correct answer to the question you specifically asked

db.rawQuery("SELECT Description FROM Table_Name WHERE Num BETWEEN "+(inputNumber-Range)+" AND "+(inputNumber+Range) +"ORDER BY ABS(Num- "+inputNumber+")", null);

Taking the absolute value to avoid negative differences

这篇关于如何写一个范围查询SQLite中的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆