Postgresql 在postgres中查询整数周围的范围

Postgresql 在postgres中查询整数周围的范围,postgresql,Postgresql,在postgresql中,我想确定一个已知整数是否在另一个整数的+/-范围内。此查询的函数是什么 示例:在我的数据集中,我有两个表: Table_1 ID integer 1 2000 2 3000 3 4000 对于每个ID对,我想查询表_1.integer是否为表_2.integer的+/-25 The answers would be: ID 1: TRUE ID 2: FALSE ID 3: FALSE 非常感谢您的帮助。一般来说,我对使用postg

在postgresql中,我想确定一个已知整数是否在另一个整数的+/-范围内。此查询的函数是什么

示例:在我的数据集中,我有两个表:

Table_1
ID   integer
 1     2000
 2     3000
 3     4000
对于每个ID对,我想查询表_1.integer是否为表_2.integer的+/-25

The answers would be:
ID 1: TRUE
ID 2: FALSE
ID 3: FALSE

非常感谢您的帮助。一般来说,我对使用postgresql和所有编程语言都不熟悉。

我们可以尝试检查两个
整数值之间的差值的绝对值,对于每个
ID

SELECT
    t1.ID,
    CASE WHEN ABS(t1.integer - t2.integer) <= 25 THEN 'TRUE' ELSE 'FALSE' END AS answer
FROM Table_1 t1
INNER JOIN Table_2 t2
    ON t1.ID = t2.ID
ORDER BY
    t1.ID;
选择
t1.ID,

CASE WHEN ABS(t1.integer-t2.integer)这几乎类似于@Tim的解决方案,但没有
CASE
表达式,如果您希望输出
布尔类型,这非常有用

SELECT t1.ID,ABS(t1.integer - t2.integer) <= 25 as res
FROM table_1 t1 JOIN table_2 t2
    ON t1.ID = t2.ID;

选择t1.ID,ABS(t1.integer-t2.integer)
当ABS(t1.integer-t2.integer)@a_horse_和_no_name时,我假设OP想要实际的文本字符串输出。除此之外,我同意你的建议。
SELECT
    t1.ID,
    ABS(t1.integer - t2.integer) <= 25 AS answer
FROM ...
SELECT t1.ID,ABS(t1.integer - t2.integer) <= 25 as res
FROM table_1 t1 JOIN table_2 t2
    ON t1.ID = t2.ID;