简体   繁体   English

MYSQL:基于第二张表的加入、计数和排名

[英]MYSQL: Join, Count & Rank based on 2nd table

I have two tables I'm working with in MySQL v5.5.33: members & member_points我在 MySQL v5.5.33 中有两个正在使用的表: members & member_points

Structured as follows:结构如下:

members :成员

  id  Name
  1   John    
  2   Jane      

member_points :会员积分

  id  id_from  id_to
  1      1       2
  2      2       1
  3      2       1

I was able to get this far, but unable to finish the last 3 columns and rank/sort properly我能够做到这一点,但无法完成最后 3 列并正确排名/排序

 id  name  total_sent  total_received  sent_minus_received  rank
 1   John      1                                
 2   Jane      2                               

This is my current code, which I was able to achieve the above.这是我当前的代码,我能够实现上述目标。

SELECT *
     , a.id
     , COUNT(m.id) AS id_from_count
  FROM members AS a
  LEFT 
  JOIN member_points AS m 
    ON a.id = m.id_from
 GROUP 
    BY a.id

However the result I'm looking to achieve, includes total received, the variance or difference between sent and received, and lastly ranked based on the difference.但是,我希望达到的结果包括接收总数、发送和接收之间的差异或差异,最后根据差异进行排名。

 id  name  total_sent  total_received  sent_minus_received  rank
 2   Jane      2            1                 1               1
 1   John      1            2                -1               2 

Any guidance would be much appreciated.任何指导将不胜感激。

Thank you for your time.感谢您的时间。

SELECT m.id, 
       m.name,
       SUM(m.id = mp.id_from) total_sent,
       SUM(m.id = mp.id_to) total_received,
       SUM(m.id = mp.id_from) - SUM(m.id = mp.id_to) `var(sent-received)`,
       DENSE_RANK() OVER ( ORDER BY {unclear from question text} ) `rank`
FROM members m
CROSS JOIN member_points mp
GROUP BY m.id, 
         m.name

Currently using Version 5... My apologies.目前使用的是第 5 版......我很抱歉。 localhost currently on 5.5.33本地主机目前在 5.5.33

SELECT subquery.*, 
       @rank := @rank + 1 `rank`
FROM (SELECT m.id, 
             m.name,
             SUM(m.id = mp.id_from) total_sent,
             SUM(m.id = mp.id_to) total_received,
             SUM(m.id = mp.id_from) - SUM(m.id = mp.id_to) `var(sent-received)`
      FROM members m
      CROSS JOIN member_points mp
      GROUP BY m.id, 
               m.name) subquery, 
     (SELECT @rank := 0) variable
ORDER BY `var(sent-received)` DESC;

fiddle 小提琴

If you are running MySQL 8.0, you can join, aggregate, and rank with window functions:如果您正在运行 MySQL 8.0,您可以使用 window 函数加入、聚合和排名:

select
    m.id,
    m.name,
    sum(m.id = mp.id_from) total_sent,
    sum(m.id = mp.id_to) total_recieved,
    sum(m.id = mp.id_from) - sum(m.id = mp.id_to) diff,
    rank() over(order by sum(m.id = mp.id_from) - sum(m.id = mp.id_to)) rnk
from members as m
left join member_points mp as mp on m.id in (mp.id_from, mp.id_to)
group by m.id, m.name
order by diff desc

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

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