我有一张桌子如下所示
number unit Value
77 km ss
74 km aa
77 m ww
78 m ee
77 km ii
74 m pp
我正在编写一个范围查询来获取最近的值。我正在使用条件来测试数字。现在我需要修改此查询以检查单位。我的查询看起来像
cursor=db.rawQuery("SELECT * FROM "+TABLE_DOF+" WHERE ABS("+inputNumber+" - number)=(SELECT MIN(ABS("+inputNumber+" - number)) FROM "+TABLE_DOF+") AND ABS("+inputNumber+" - number<=5", null);
如果输入的数字是77,那么我得到三行
number unit Value
77 km ss
77 m ww
77 km ii
我还需要在这里检查单位。任何帮助将不胜感激。谢谢
答案 0 :(得分:0)
我强烈建议您使用query()而不是rawQuery(),尤其是如果您不是创建有效SQL语句的专家。也就是说,只需在WHERE子句中添加另一个AND。
String unit = "km";
cursor=db.rawQuery("SELECT * FROM "+TABLE_DOF+
" WHERE ABS("+inputNumber+" - number)=(SELECT MIN(ABS("+
inputNumber+" - number)) FROM "+TABLE_DOF+
") AND ABS("+inputNumber+" - number<=5) AND unit = " + unit, null)
答案 1 :(得分:0)
您好我们和您的WHERE子句,请尝试如下
String unit = "km";
String query="SELECT * FROM "+TABLE_DOF+
" WHERE ABS("+inputNumber+" - number)=(SELECT MIN(ABS("+
inputNumber+" - number)) FROM "+TABLE_DOF+
") AND ABS("+inputNumber+" - number<=5) AND unit = " + unit;
cursor=db.rawQuery(query,null);