【问题标题】:Codeigniter join returning wrong row(s)Codeigniter 加入返回错误的行
【发布时间】:2014-03-31 17:32:51
【问题描述】:

我已经尝试了几天来找出问题所在,但我不能。

我有这个功能:

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->gets('approved');它应该返回潜在客户表中的所有行 WHERE 在 table clips 中没有找到匹配项,其中 partner_id 等于当前会话并且 type 为减号。

如果我像这样调用函数 $this->gets('', 'bought');它应该从与上述相同条件下找到匹配的线索返回所有行。

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

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

谢谢!

【问题讨论】:

  • 不。我有三排潜在客户。它应该返回其中两个,但它只返回不应该返回的一个。
  • 你加入的表名在哪里,你从剪辑表中获取所有内容并加入剪辑,我认为你也需要加入潜在客户表
  • 带有任务/潜在客户的表名为“leads”,定义用户是否购买了潜在客户的表名为“clips”。我犯了一个错误,说 $this->db->from('clips');它应该是 $this->db->from('leads');。但它仍然无法正常工作。

标签: php sql activerecord join codeigniter-2


【解决方案1】:

我觉得应该是这样的

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();
    }
}

【讨论】:

  • 不起作用。表名是“leads”和“clips”。
  • 对不起。不,我没有收到错误。但它仍然只是返回它不应该返回的行。
  • 你知道为什么它没有返回正确的结果吗?
  • 试试 $this->gets(false, 'bought')
【解决方案2】:

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

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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多