Mysql根据另一个表的范围过滤数据

时间:2017-06-02 07:21:55

标签: mysql sql join

我有一个'amount'表,其中包含'amount'列.'transmission'表的结构如下所示

--------------------------------------------------------
| id | from_account_id | to_account_id | date | amount |
--------------------------------------------------------

实际上还有更多列,但上面的这些列与我的问题最相关

现在我有另一个表,让我们说'promo_dtl'表,显示当前用于交易的促销..表格看起来像这样

-------------------------------------------------------------------
| id | promo_id | min_amount_trx | max_amount_trx | cashback_type | cashback_value | max_cashback_value |

现在我要做的是,我想从'transfer'表中查询,从'promo_dtl'表中按min_amount_trxmax_amount_trx的范围过滤..

编辑: 例如,'promo_dtl'表中有2条记录,具有相同的promo_id值。第一行有min_amount_trx 100和max_amount_trx 1000 ..而第二行有{{1 5000和min_amount_trx 10000

所以我想找到交易金额在100到1000之间和5000到10000之间的记录

我尝试了这个简单的查询

max_amount_trx

但这是我从上面的查询中获得的

select * from transfers trf
where trf.amount 
    between (select min_amount_trx from promo_dtl where promo_id = 1)
    and  (select max_amount_trx from promo_dtl where promo_id = 1)
;

那么我怎样才能正确获取数据?我必须使用加入吗?如果是这样,任何人都可以帮助我吗?谢谢:D

2 个答案:

答案 0 :(得分:3)

问题在于其中之一:

select min_amount_trx from promo_dtl where promo_id = 1

select max_amount_trx from promo_dtl where promo_id = 1

返回更多的一行。哪个不行。你可以这样做:

select * from transfers trf
where trf.amount 
    between (select MIN(min_amount_trx) from promo_dtl where promo_id = 1)
    and  (select MAX(max_amount_trx) from promo_dtl where promo_id = 1)
;

您可以使用min和max

来聚合范围

<强>更新

即使它对我来说真的没有意义。 如果你喜欢做你说的话。您可以尝试使用和EXISTS。像这样:

SELECT
    *
FROM
    transfers
WHERE EXISTS
(
    SELECT NULL
    FROM promo_dtl
    WHERE promo_dtl.promo_id=1
    AND transfers.amount 
        BETWEEN promo_dtl.min_amount_trx 
        AND promo_dtl.max_amount_trx 
);

答案 1 :(得分:1)

我可以这样使用内部加入:

Select * from transfers
INNER JOIN promo_dtl 
on transfers.amount >= promo_dtl.min_amount_trx  
    AND dbo.transfers.amount <= promo_dtl.max_amount_trx 
    AND promo_id = 1

这应该可以为您提供所需的所有数据 我创建了一个小样本进行测试,结果如下:

id          from_account_id to_account_id date                    amount
----------- --------------- ------------- ----------------------- -----------
1           1               2             2017-06-02 11:02:11.937 75
2           3               4             2017-06-02 11:02:21.950 300
3           5               6             2017-06-02 11:02:31.777 750

(3 row(s) affected)

id          promo_id    min_amount_trx amx_amount_trx
----------- ----------- -------------- --------------
1           1           50             100
2           1           500            1000
3           2           250            400

(3 row(s) affected)

id          from_account_id to_account_id date                    amount      id          promo_id    min_amount_trx amx_amount_trx
----------- --------------- ------------- ----------------------- ----------- ----------- ----------- -------------- --------------
1           1               2             2017-06-02 11:02:11.937 75          1           1           50             100
3           5               6             2017-06-02 11:02:31.777 750         2           1           500            1000