Php Codeigniter联接返回错误的行

Php Codeigniter联接返回错误的行,php,sql,activerecord,join,codeigniter-2,Php,Sql,Activerecord,Join,Codeigniter 2,几天来我一直想弄清楚问题出在哪里,但我做不到 我有这个功能: function gets($status = NULL, $bought = NULL) { $this->db->select('leads.*, clips.id AS clip_id, clips.lead_id AS clip_lead_id, clips.partner_id AS clip_partner_id, clips.type AS clip_type, clips.clip AS clip_

几天来我一直想弄清楚问题出在哪里,但我做不到

我有这个功能:

function gets($status = NULL, $bought = NULL)
{
    $this->db->select('leads.*, clips.id AS clip_id, clips.lead_id AS clip_lead_id, clips.partner_id AS clip_partner_id, clips.type AS clip_type, clips.clip AS clip_clip, clips.price AS clip_price, clips.created_at AS clip_created_at, clips.updated_at AS clip_updated_at, clips.ip_address AS clip_ip_address');
    if ($status != FALSE)
    {
        $this->db->where('leads.status', $status);
    }
    $this->db->from('leads');
    if ($bought != FALSE)
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips', 'clips.lead_id = leads.id');
    }
    else
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips', 'clips.lead_id = leads.id', 'left outer');
    }
    $query = $this->db->get();

    if ($query->num_rows() != 0)
    {
        return $query->result();
    }
}
如果我像这样调用函数$this->get('approved');它应该返回leads表中的所有行,其中partner_id等于当前会话且type为负数的表剪辑中没有找到匹配项

如果我像这样调用函数$this->get(“”,'bunded');它应该返回与上面相同条件下找到匹配的Lead中的所有行

我希望你能理解我的代码并帮助我

如果您有任何问题,请随时提问


谢谢

我想应该是这样的

function gets($status = false, $bought = false)
{
    $this->db->select('leads.*, clips.id AS clip_id, clips.lead_id AS clip_lead_id, clips.partner_id AS clip_partner_id, clips.type AS clip_type, clips.clip AS clip_clip, clips.price AS clip_price, clips.created_at AS clip_created_at, clips.updated_at AS clip_updated_at, clips.ip_address AS clip_ip_address');
    if ($status)
    {
        $this->db->where('leads.status', $status);
    }
    if ($bought)
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips clips', 'clips.lead_id = leads.id');
    }
    else
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips clips', 'clips.lead_id = leads.id', 'left outer');
    }
    $query = $this->db->get('leads leads');

    if ($query->num_rows() != 0)
    {
        return $query->result();
    }
}

我重写了代码,这就是我要做的

function gets($status = NULL, $bought = NULL)
{
    // Get the rows where the partner has used clips
    $this->db->where('partner_id', $this->session->userdata('partner_id'));
    $this->db->where('type', '-');
    $query = $this->db->get('clips');

    if ($status != FALSE)
    {
        if(is_array($status))
        {
            $first = TRUE;
            foreach($status as $row)
            {
                if ($first == TRUE)
                {
                    $this->db->where('leads.status', $row);
                    $first = FALSE;
                }
                else
                {
                    $this->db->or_where('leads.status', $row);
                }
            }
        }
        else
        {
            $this->db->where('leads.status', $status);
        }
    }

    $bought_array[] = '';
    if ($query->num_rows() != 0)
    {
        foreach($query->result() as $row2)
        {
                    // Add the ID's of the bought leads to array
            $bought_array[] = $row2->lead_id;
        }
    }

    // If bought leads should not be shown in the results
    if ($bought == FALSE)
    {
        $this->db->where_not_in('id', $bought_array);
    }
    else // If they should...
    {
        $this->db->where_in('id', $bought_array);
    }

    $query2 = $this->db->get('leads');

    if ($query2->num_rows() != 0)
    {
        return $query2->result();
    }
}

不。我有三排线索。它应该返回其中的两个,但它只返回不应该返回的一个。您的联接表名称在哪里,您从clips表中获取所有信息并联接到clips,我认为您需要联接leads表齿表名称,任务/Lead表名为“leads”,定义用户是否购买了Lead的表名为“clips”。我犯了一个错误,说$this->db->from('clips');它应该是$this->db->from('leads');。但它仍然不起作用,不起作用。表名是“leads”和“clips”。对不起。不,我没有得到一个错误。但是它仍然只返回不应该返回的行。你知道它为什么不返回正确的结果吗?试试像$this->get(false,'bunded')这样的方法