如何将sql查询转换为codeigniter活动记录

时间:2018-02-06 10:05:28

标签: mysql sql codeigniter

如何将sql查询转换为codeigniter活动记录

查询:

 SELECT s2.hierarchy_users_id, 
           s2.commisions, 
           s1.given_amount 
    FROM  (SELECT hierarchy_users_id, 
                  Sum(commisions) AS commisions 
           FROM   commision 
           GROUP  BY hierarchy_users_id = '2') AS s2 
          LEFT OUTER JOIN(SELECT hierarchy_users_id, 
                                 Sum(given_amount)AS given_amount 
                          FROM   incentives 
                          GROUP  BY hierarchy_users_id = '2')AS s1 
                       ON s1.hierarchy_users_id = s2.hierarchy_users_id 
    LIMIT  1 

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此codeignitor活动记录查询。

查询: -

$this->db->select('s2.hierarchy_users_id, s2.commisions, s1.given_amount ',false);
$this->db->from('SELECT hierarchy_users_id, Sum(commisions) AS commisions FROM   commision WHERE hierarchy_users_id = "'.$ids[$o].'" GROUP  BY hierarchy_users_id) AS s2');
$this->db->join('SELECT hierarchy_users_id, Sum(given_amount)AS given_amount FROM   incentives WHERE hierarchy_users_id = "'.$ids[$o].'" GROUP  BY hierarchy_users_id) AS s1','s1.hierarchy_users_id = s2.hierarchy_users_id','left outer');
$this->db->limit('1');
$query_result = $this->db->get();
$result = $query_result->result();
return !empty($result) ? $result : '';

OR

$this->db->select('s2.hierarchy_users_id, s2.commisions, s1.given_amount ',false);
$this->db->from('SELECT hierarchy_users_id, Sum(commisions) AS commisions FROM   commision GROUP  BY hierarchy_users_id) AS s2');
$this->db->join('SELECT hierarchy_users_id, Sum(given_amount)AS given_amount FROM   incentives GROUP  BY hierarchy_users_id) AS s1','s1.hierarchy_users_id = s2.hierarchy_users_id','left outer');
$this->db->limit('1');
$query_result = $this->db->get();
$result = $query_result->result();
return !empty($result) ? $result : '';

您可以尝试使用第一个选项(hierarchy_users_id =" 2"),否则尝试第二个选项(所有hierarchy_users_id)

我希望它会有所帮助。