SQLite - 获得最接近的价值 [英] SQLite - getting closest value

查看:21
本文介绍了SQLite - 获得最接近的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 SQLite 数据库,其中有double"类型的特定列.我想获得该列值最接近指定值的行.

I have SQLite database and I have in it certain column of type "double". I want to get a row that has in this column value closest to a specified one.

例如,在我的表中,我有:

For example, in my table I have:

id: 1; value: 47
id: 2; value: 56
id: 3; value: 51

我想得到一个值最接近 50 的行.所以我想接收 id: 3 (value = 51).

And I want to get a row that has its value closest to 50. So I want to receive id: 3 (value = 51).

我怎样才能实现这个目标?

How can I achieve this goal?

谢谢.

推荐答案

这应该有效:

SELECT * FROM table
ORDER BY ABS(? - value)
LIMIT 1

其中 ? 代表您要与之比较的值.

Where ? represents the value you want to compare against.

这篇关于SQLite - 获得最接近的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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