我如何在sqlite中使用浮点数?

时间:2013-11-14 10:55:35

标签: sql sqlite floating-point numbers precision

我的sqlite数据库中存在以下问题,存储了值,出于某些目的,我需要删除条目。 但在我需要获取我想删除的行的id之前。要获取id,请尝试使用以下查询:

select commonid from TestData where coordinate_x=5.707523 and coordinate_y=6.693941;

结果为零,因为coordinate_y的值为6.693940。因此,我正在考虑使用以获得正确的结果。没关系,因为值只有逗号后面的前两位数没有区别。我试过了:

select commonid from TestData where coordinate_x=5.707523 and coordinate_y LIKE "6.69394%";

这也失败了,结果为零。

我在网上发现我可以使用*而不是%。

select commonid from TestData where coordinate_x=5.707523 and coordinate_y LIKE "6.69394*";

不幸的是,这也不起作用。

那么如何使用like或其他命令来解决这个问题呢?

3 个答案:

答案 0 :(得分:3)

这对我有用:

sqlite> .schema t
CREATE TABLE t (f double);
sqlite> select * from t;
1.23456789
2.23456789
3.23456789
sqlite> select * from t where f = 1.23456789;
1.23456789
sqlite> select * from t where f like '1.23456789';
1.23456789
sqlite> select * from t where f like '1.234%';
1.23456789
sqlite> select * from t where f like '_.23456789';
1.23456789
2.23456789
3.23456789

答案 1 :(得分:1)

你可以,但它会更聪明(也可能更快)

SELECT commonid FROM TestData WHERE coordinate_x=5.707523 AND
    ABS(coordinate_y-6.693945)<0.000005;

SELECT commonid FROM TestData WHERE coordinate_x=5.707523 AND
    coordinate_y BETWEEN 6.69394 AND 6.69395;

答案 2 :(得分:0)

尝试将其视为角色

select commonid from TestData 
where coordinate_x=5.707523 
and Cast(coordinate_y as Char(100)) LIKE "6.69394%";