【问题标题】:MYSQL arrange users based on 2 columns from 2 different tablesMYSQL 根据来自 2 个不同表的 2 列排列用户
【发布时间】:2021-02-23 10:24:32
【问题描述】:

我正在寻找一种可能的解决方案,该解决方案将为我提供来自 users 表的前 10 位用户,这些用户按赚取的金额和他们从 2 个表中获得的推荐来排序。来自同一个 users 表的总推荐使用 inviter_id 列和来自另一个名为 transactions 的表的 amount 列。

这是userstransactions 表的表架构。

users

id     inviter_id
1      1
2      1
3      1
4      1
5      2
6      3
7      5
8      6
9      1
10     3
11     9
12     1
13     5
14     7
15     11

解释:id 代表用户的唯一 ID,inviter_id 代表邀请用户的 ID。

transactions

id     receiver_id amount
1      1           200
2      1           100
3      1           50
4      2           10
5      3           400
6      4           200
7      5           100
8      6           50
9      7           100
10     8           50
11     9           50
12     10          100 
13     11          400
14     1           200
15     2           100
16     1           50
17     1           10
18     4           500

这里receiver_idusers 表中的用户。

期望的输出:

user_id     referrals     earned
1           6             610
2           1             110
3           2             400
4           0             700
5           2             100
6           1             50
7           1             100
8           0             50
9           1             50
10          0             100
11          1             400
12          0             0
13          0             0
14          0             0
15          0             0

解释:我需要每个用户的推荐次数以及用户的收入。

奖励:我需要根据推荐和收入最高的人对输出表进行排序。

到目前为止我尝试了什么: 由于我使用的是 codeigniter 查询生成器,因此这是我的代码。

        $this->db->select('u.id, AS user_id, IF(COUNT(p.id) IS NULL, 0, COUNT(p.id)) AS referrals, IF(SUM(m.amount) IS NULL, 0, SUM(m.amount)) AS earned');
        $this->db->from('users u');
        $this->db->join('transactions m', "u.id = m.receiver_id",'LEFT');
        $this->db->join('users p', "u.id = p.inviter_id",'LEFT');

        $this->db->group_by('u.id');
        $this->db->limit(10);

        $this->db->order_by('referrals', 'DESC');
        $this->db->order_by('earned', 'DESC');
        $query = $this->db->get();
        $row = $query->result();

当我加入表格时,我得到了错误的推荐和收入值,COUNT 和 SUM 为我提供了多个加入的行。

【问题讨论】:

  • 如果你使用子查询而不是连接会怎样?
  • 能给个代码吗?不是mysql PRO。如果您可以为普通 sql 编写代码,我可以将其转换为 CI 构建器类。
  • select a.id, (select count(*) from trasactions where receiver_id = a.id ) 作为推荐,(select sum(amount) from trasactions where receiver_id = a.id) 从用户那里获得按赚到的顺序的订单
  • 我没有测试过,但请尝试
  • 别忘了加限制

标签: mysql sql codeigniter-3


【解决方案1】:

如果我理解正确,您需要汇总两个表。要保留所有 ID,请使用 left join 并在加入前聚合表:

select u.id, coalesce(ui.referrals, 0), coalesce(t.earned, 0) as earned
from users u left join
     (select u.inviter_id, count(*) as referrals
      from users u
      group by u.inviter_id
     ) ui 
     on ui.inviter_id = u.id left join
     (select receiver_id, sum(amount) as earned
      from transactions t
      group by t.receiver_id
     ) t
     on t.receiver_id = u.inviter_id
group by u.id
order by referrals desc, earnings desc;

【讨论】:

    猜你喜欢
    • 2020-05-14
    • 2012-10-06
    • 1970-01-01
    • 2019-11-16
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    相关资源
    最近更新 更多