SQL查找/联接值范围内的一个值

时间:2018-06-20 02:43:30

标签: sql ms-access

input tables 这些是我的桌子。我想使用查询将前两个表合并以获取第三个表,如下所示 output of join

4 个答案:

答案 0 :(得分:2)

本着学习SQL的精神-尝试以下操作:

SELECT * FROM rates JOIN shipments ON (weight >= floor);

那不能使您一直获得正确的答案-但我认为这说明了如何实现目标。

答案 1 :(得分:2)

以下查询应为您提供所需的结果,

SELECT s.shipmentid, s.weight, r.floor, r.ceiling, r.rate 
FROM rates r 
JOIN shipments s ON s.weight <= r.ceiling and s.weight >= floor

答案 2 :(得分:2)

尝试一下:

select
  shipments.*, 
  (select floor from rates where shipments.weight between rates.floor and rates.ceiling) as floor,
  (select ceiling from rates where shipments.weight between rates.floor and rates.ceiling) as ceiling,
  (select rate from rates where shipments.weight between rates.floor and rates.ceiling) as rate
from shipments

我已经在Access 2013上对此进行了测试。

答案 3 :(得分:0)

在MS Access中,我认为正确的语法是:

SELECT s.shipmentid, s.weight, r.floor, r.ceiling, r.rate 
FROM shipments as s LEFT JOIN
     rates as r 
     ON s.weight >= r.floor AND s.weight <= r.ceiling;