【问题标题】:MYSQL Issue in fetching profit data from multiple table从多个表中获取利润数据的MYSQL问题
【发布时间】:2020-08-15 09:24:13
【问题描述】:

我有 3 个 MYSQL 表 User 表和 2 个 Transactions 表。

我的用户表如下。

id     inviter_id    active .... (other columns)
2      1             1
3      1             1
4      2             1
5      1             1
6      2             1

我的 txn1 表是

id     payer_id     receiver_id     amount     type
1      2            1               20         profit
2      3            1               30         profit
3      4            2               20         profit
4      3            2               50         profit
5      5            2               20         profit

我的 txn2 表是

id     payer_id     receiver_id     amount     txn_type
1      2            1               20         profit
2      3            2               30         profit
3      4            2               20         profit
4      3            1               50         profit
5      5            1               20         profit

我需要得到的是, 考虑我正在查询用户2。我需要使用inviter_id 列从他的每个下线或推荐人那里获得的利润。

例如,如果我想从 Txn1 和 Txn2 表中获取用户 #2 的利润,它应该获取用户 4 和用户 6 在 txn1 和 txn2 表中的 txns。

到目前为止,我尝试过的是,

$userID = 2;
$this->db->select('u.id as partner_id, SUM(txn1_profit.amount) AS t1Profit, SUM(txn2_profit.amount) AS t2Profit');
$this->db->from('users u');
$this->db->join('txn1 txn1_profit', "u.id = txn1_profit.payer_id AND $userID = txn1_profit.receiver_id AND 'profit' = txn1_profit.txn_type",'LEFT');
$this->db->join('txn2 txn2_profit', "u.id = txn2_profit.payer_id AND $userID = txn2_profit.receiver_id AND 'profit' = txn2_profit.txn_type",'LEFT');
        
        $this->db->where('u.inviter_id', $userID);      
        $this->db->group_by('u.id');

        $query = $this->db->get(); 
        $row = $query->result();
        
        if (empty($row))
            return FALSE;
        return $row;

这个查询的问题是我得到了一个巨大的总和值。

【问题讨论】:

  • '它应该得到用户 4 和用户 6 在 txn1 和 txn2 表中的 txns。这些表中没有名为 user 的列 - 您需要澄清这些表之间的关系并添加所需的输出

标签: php mysql codeigniter


【解决方案1】:

如果您的事务表架构相同,那么我建议您更改架构并使用一个事务表来存储此类信息。通过更改您当前的设计将帮助您构建简单的查询可能会减少没有。连接数。

对于您当前的架构,我可以想到 2 种可能的方法来解决您的利润价值问题。

从子句中的事务表中计算总和,然后与用户表进行联接

select u.inviter_id, 
       sum(t1.t1Profit), 
       sum(t2.t2Profit),
       sum(t1.t1Profit) + sum(t2.t2Profit) total
from users u
left join (
  select payer_id, sum(amount) t1Profit
  from tx1
  where 2 = receiver_id 
  and 'profit' = type
  group by payer_id
) t1 on u.id = t1.payer_id
left join (
  select payer_id, sum(amount) t2Profit
  from tx2
  where 2 = receiver_id 
  and 'profit' = txn_type
  group by payer_id
) t2  on u.id = t2.payer_id
where u.inviter_id = 2;

或使用union all 组合事务表的数据,然后与用户表进行联接

select u.inviter_id, 
       sum(t1.amount) total
from users u
left join (
  select payer_id, amount
  from tx1
  where 2 = receiver_id 
  and 'profit' = type
  union all 
  select payer_id, amount
  from tx2
  where 2 = receiver_id 
  and 'profit' = txn_type
) t1 on u.id = t1.payer_id
where u.inviter_id = 2;

DEMO

【讨论】:

  • 根据 CI 的代码修改了你的代码,一切都很好。用了第一种方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多