【问题标题】:CodeIgniter Active Record not equalCodeIgniter Active Record 不相等
【发布时间】:2011-07-10 17:18:59
【问题描述】:

在使用活动记录的 CodeIgniter 中,我如何在 $this->db->where() 中执行不等于。例如:

$this->db->where('emailsToCampaigns.campaignId', $campaignId);

会等于,但我不需要等于。我试过了:

$this->db->where('emailsToCampaigns.campaignId <> ', $campaignId);
$this->db->where('emailsToCampaigns.campaignId != ', $campaignId);
$this->db->where('emailsToCampaigns.campaignId', ' != ' . $campaignId);
$this->db->where('emailsToCampaigns.campaignId != ' . $campaignId);

一切都没有运气。想法?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    根据手册,这应该可以工作:

    自定义键/值方法:

    您可以在第一个参数中包含一个运算符以控制比较:

    $this->db->where('name !=', $name);
    $this->db->where('id <', $id);
    Produces: WHERE name != 'Joe' AND id < 45
    

    搜索 $this-&gt;db-&gt;where(); 并查看第 2 项。

    【讨论】:

    • 小注:字段名和运算符之间必须有空格(如上例所示)。我试图在字段名称和运算符之间不留空格。它不起作用。
    【解决方案2】:

    对我来说效果很好,

    $this->db->where("your_id !=",$your_id);
    

    或者试试这个,

    $this->db->where("your_id <>",$your_id);
    

    或者试试这个,

    $this->db->where("your_id IS NOT NULL");
    

    一切都会好起来的。

    【讨论】:

      【解决方案3】:

      试试这个代码。 这在我的情况下似乎有效。

      $this->db->where(array('id !='=> $id))
      

      【讨论】:

        【解决方案4】:
        $this->db->where('emailsToCampaigns.campaignId !=' , $campaignId);
        

        这应该有效(您已经尝试过)

        要进行调试,您可以在执行查询后放置此代码以查看它生成的确切 SQL,这可能会为您提供线索,您可以将其添加到问题中以获得进一步的帮助。

        $this->db->get();              // your query executing
        
        echo '<pre>';                  // to preserve formatting
        die($this->db->last_query());  // halt execution and print last ran query.
        

        【讨论】:

          【解决方案5】:

          这应该有效(您已经尝试过)

          $this->db->where_not_in('emailsToCampaigns.campaignId', $campaignId);
          

          【讨论】:

            【解决方案6】:
            $this->db->select('*')
            ->from($this->table_proposals)
            ->where('customer_id', $this->session->userdata('user')['customer_id'])
            ->where('status <>' , 'draft')
            ->get()->result();
            

            【讨论】:

            【解决方案7】:

            这是来自 codeigniter 文档的示例

            enter image description here

            【讨论】:

            • 请始终将文档部分作为文本(而非图像)粘贴到帖子中,以防将来无法访问该站点。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-03-24
            • 1970-01-01
            • 2013-02-18
            • 2013-06-14
            • 2012-02-03
            • 2012-07-24
            • 1970-01-01
            相关资源
            最近更新 更多