子查询范围内的数字

时间:2015-09-08 07:41:24

标签: sql

我正在尝试查询ID_NUMBERTBL_TRANSACTIONS所做的交易 并且要求确保ID_NUMBER中存在TBL_ID_NUMBER

SELECT *
FROM TBL_TRANSACTIONS
WHERE ID_NUMBER IN ( SELECT ??????
                     FROM TBL_ID_NUMBER)

问题是ID号是否在范围内,例如10-5051-9091-100 有没有办法可以查询这两列之间的数字?我们说ID_NUMBER是19?

TBL_ID_NUMBER (table)
ID_FROM (column1)   ID_TO (column2)
10                  50

1 个答案:

答案 0 :(得分:0)

我不知道你是不是这样,但是有一个建议;)

    SELECT *
FROM TBL_TRANSACTIONS
WHERE ID_NUMBER >= ( SELECT ID_FROM
                     FROM TBL_ID_NUMBER
                     WHERE 
                     ID_NUMBER = 19)
AND ID_NUMBER <= ( SELECT ID_FROM
                     FROM TBL_ID_NUMBER
                     WHERE 
                     ID_NUMBER = 19)
  编辑:我不知道您的数据库结构,所以这是另一个建议

SELECT *
    FROM TBL_TRANSACTIONS
    WHERE ID_NUMBER >= ( SELECT ID_FROM
                         FROM TBL_ID_NUMBER
                         WHERE 
                           ID_FROM <= 19
                         AND 
                           ID_TO >= 19)
    AND ID_NUMBER <= ( SELECT ID_TO
                         FROM TBL_ID_NUMBER
                         WHERE 
                           ID_FROM <= 19
                         AND 
                           ID_TO >= 19)