MYSQL 根据 2 个不同表格中的 2 列排列用户
MYSQL arrange users based on 2 columns from 2 different tables
我正在寻找一个可能的解决方案,它会给我来自 users
table 的前 10 名用户,这些用户是根据他们从 2 table 获得的收入和推荐来订购的.来自同一 users
table 使用 inviter_id
列和来自另一个 table 名为 transactions
.
的 amount
列的总推荐
所以这是我的 table 架构 users
和 transactions
table。
users
TABLE
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
TABLE
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_id
是users
table中的用户。
期望的输出:
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
说明:我需要每个用户的推荐次数以及该用户的收入。
奖金:我需要输出 table 根据推荐人和收入最高的人排序。
到目前为止我尝试了什么:
由于我使用的是 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();
我在加入 table 时得到了错误的推荐和收入值,COUNT 和 SUM 给了我加入的行的倍数。
如果我没理解错的话,你需要把这两个表聚合起来。要保留所有 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;
我正在寻找一个可能的解决方案,它会给我来自 users
table 的前 10 名用户,这些用户是根据他们从 2 table 获得的收入和推荐来订购的.来自同一 users
table 使用 inviter_id
列和来自另一个 table 名为 transactions
.
amount
列的总推荐
所以这是我的 table 架构 users
和 transactions
table。
users
TABLE
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
TABLE
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_id
是users
table中的用户。
期望的输出:
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
说明:我需要每个用户的推荐次数以及该用户的收入。
奖金:我需要输出 table 根据推荐人和收入最高的人排序。
到目前为止我尝试了什么: 由于我使用的是 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();
我在加入 table 时得到了错误的推荐和收入值,COUNT 和 SUM 给了我加入的行的倍数。
如果我没理解错的话,你需要把这两个表聚合起来。要保留所有 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;