简体   繁体   English

如何以条件行值联接表?

[英]How to join table with condition on row values?

Below are my table data 以下是我的表格数据

Table A 表A

| amount | range   | id  |
+--------+---------+-----+
|    720 |       1 | 115 |
|    360 |       2 | 115 |
|    180 |       3 | 115 |
|    180 |       4 | 115 |
|    135 |       5 | 115 |
|    135 |      10 | 115 |

Table B 表B

+------+-----+
| rank | a_id|
+------+-----+
|    1 | 115 |
|    2 | 115 |
|    3 | 115 |
|    4 | 115 |
|    5 | 115 |
|    6 | 115 |
|    7 | 115 |
|    8 | 115 |
|    9 | 115 |
|   10 | 115 |

I need to get the following result by joining these two tables. 我需要通过连接这两个表来获得以下结果。

+------+-----+--------+
| rank | a_id| amount |
+------+-----+--------+
|    1 | 115 |720
|    2 | 115 |360
|    3 | 115 |180
|    4 | 115 |180
|    5 | 115 |135
|    6 | 115 |135
|    7 | 115 |135
|    8 | 115 |135
|    9 | 115 |135
|   10 | 115 |135

Since I have tried with the below query. 由于我尝试了以下查询。 But I am unable to get the exact result. 但是我无法获得确切的结果。 since its only matching with the rank. 因为它仅与等级匹配。 I have tried the queries with case also but I didn't get the exact result. 我也尝试过使用大小写查询,但没有得到确切的结果。 Any suggestions for how to solve it 关于如何解决的任何建议

select rank
     , sample_test.amount 
  from a 
  join b 
    on a.id = b.a_id 
    and rank = range 
 order 
    by rank

Join the tables and group by rank, a_id to get the min amount of each group: 加入表并按等级a_id分组以获得每个组的最小数量:

select
  b.rank, b.a_id, min(a.amount) amount
from tableb b inner join tablea a
on b.a_id = a.id and b.rank >= a.range
group by b.rank, b.a_id

See the demo . 参见演示
Depending on the intervals and the ranks, maybe the logic (which is not clear enough) could be to use the opposite sign in the ON clause and max instead of min: 根据时间间隔和等级,可能的逻辑(还不够清楚)可能是在ON子句中使用相反的符号,并使用max代替min:

select
  b.rank, b.a_id, max(a.amount) amount
from tableb b inner join tablea a
on b.a_id = a.id and b.rank <= a.range
group by b.rank, b.a_id

See the demo . 参见演示
Both queries (for this sample data) return the same results: 这两个查询(针对此样本数据)均返回相同的结果:

| rank | a_id | amount |
| ---- | ---- | ------ |
| 1    | 115  | 720    |
| 2    | 115  | 360    |
| 3    | 115  | 180    |
| 4    | 115  | 180    |
| 5    | 115  | 135    |
| 6    | 115  | 135    |
| 7    | 115  | 135    |
| 8    | 115  | 135    |
| 9    | 115  | 135    |
| 10   | 115  | 135    |

You can try this simple sub query option- 您可以尝试使用此简单的子查询选项-

SELECT *,
(
    SELECT TOP 1 amount 
    FROM TableA A 
    WHERE A.range <=  B.rank
    AND A.id = B.a_id
    ORDER BY A.range DESC
) Amount 
FROM TableB B

You can check DEMO HERE 您可以在此处检查演示

In MySQL, I would use a correlated subquery: 在MySQL中,我将使用相关子查询:

SELECT b.*,
       (SELECT a.amount 
        FROM TableA a
        WHERE a.range <= b.rank AND
              a.id = b.a_id
        ORDER BY a.range DESC
       ) as Amount 
FROM TableB b;

In particular, this can take advantage of an index on TableA(id, range desc) . 特别是,这可以利用TableA(id, range desc)上的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM