简体   繁体   English

将基于另一个表值修改的表与 MySQL 中的表连接起来

[英]Joining a table modified based on another table values with that table in MySQL

I need some help with the MySQL query for the following problem.对于以下问题,我需要 MySQL 查询方面的帮助。

I have two tables A (id, ref_id) and B (ref_id (primary key), class, pulse) where A.ref_id is present in B.ref_id.我有两个表 A(id,ref_id)和 B(ref_id(主键),class,pulse),其中 A.ref_id 存在于 B.ref_id 中。 ref_id has integer values starting from 1 and is the P_k of B. ref_id 具有从 1 开始的 integer 值,是 B 的 P_k。

A.id | A.ref_id
  1  |  2
  2  |  3
B.ref_id | B.pulse | B.class
     1   |  100    | X
     2   |  50     | Y
     3   |  100    | X

What I want to do is create table which has the sum of all pulse values corresponding to all B.ref_id less than the A.ref_id.我想要做的是创建一个表,其中所有脉冲值的总和对应于所有 B.ref_id 小于 A.ref_id。 Also, only those pulse should be considered whose B.class is the same as the B.class of the B.ref_id referred from A.ref_id.此外,仅应考虑其 B.class 与从 A.ref_id 引用的 B.ref_id 的 B.class 相同的那些脉冲。 For example,例如,

A.ref_id | sum
  2      | 50
  3      | 200

In this example, A.ref_id having value 2 refers to the corresponding B.ref_id and thus has B.class Y. Since I want sum of pulse values for all B.ref_id<=2 and there are no other rows with B.class=Y, the value of sum is 50. However, for A.ref_id=3, the corresponding B.class is X and I want the sum of all B.pulse that have occured till now with the same class.在此示例中,值为 2 的 A.ref_id 指的是相应的 B.ref_id,因此具有 B.class Y。因为我想要所有 B.ref_id<=2 的脉冲值总和并且没有其他行具有 B.class =Y,总和的值为 50。但是,对于 A.ref_id=3,对应的 B.class 是 X,我想要迄今为止发生的所有 B.pulse 的总和与相同的 class。 Thus the value of sum would be 100 (from B.ref_id=1) + 100 (from B.ref_id=A.ref_id=3) = 200.因此 sum 的值将是 100(来自 B.ref_id=1)+ 100(来自 B.ref_id=A.ref_id=3)= 200。

Can someone please help me out with this?有人可以帮我解决这个问题吗?

I need something in MySQL like:我需要 MySQL 中的一些东西,例如:

SELECT A.ref_id, sum 
FROM A
JOIN (SELECT class, SUM(B.pulse) as sum 
      FROM B WHERE B.ref_id<=A.ref_id 
      GROUP BY B.class) t
ON t.class= (SELECT class FROM B WHERE B.ref_id=A.ref_id)

The above doesn't work because I can't refer A.ref_id inside a sub-query (t).以上不起作用,因为我无法在子查询(t)中引用 A.ref_id。 And joining without that condition doesn't help me with what I want to do.没有这个条件加入并不能帮助我做我想做的事。

Thank you!!谢谢!!

You can do it by joining 2 copies of B to A:您可以通过将 B 的 2 个副本连接到 A 来做到这一点:

SELECT a.ref_id, 
       SUM(b2.pulse) `sum`
FROM TableA a 
INNER JOIN TableB b1 ON b1.ref_id = a.ref_id
INNER JOIN TableB b2 ON b2.ref_id <= b1.ref_id AND b2.class = b1.class
GROUP BY a.ref_id

See the demo .演示
Results:结果:

ref_id ref_id sum
2 2 50 50
3 3 200 200

You can use a correlated subquery:您可以使用相关子查询:

select a.*,
       (select sum(b2.pulse)
        from b2
        where b2.class = b.class and
              b2.ref_id <= a.ref_id
       )
from a join
     b
     on a.ref_id = b.ref_id;

Or, use window functions with the join :或者,将 window 函数与join一起使用:

select a.*, b.pulse_sum           
from a join
     (select b.*, 
             sum(pulse) over (partition by class order by ref_id) as pulse_sum
      from b
     ) b
     on a.ref_id = b.ref_id;

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

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