【问题标题】:how can i use multiple where clause In codeigniter?如何在codeigniter中使用多个where子句?
【发布时间】:2014-02-01 04:30:39
【问题描述】:

对于我的数据库查询,我必须在 Codeigniter PHP 中使用多个 where 子句查询。我写的代码是这样的:

 $this->db->and_where_in('category_name,publication_status','home_headline_sub',1);

但此查询在浏览器中显示数据库查询错误。然后我写了这个查询:

$this->db->where('category_name,publication_status','home_headline_sub',1);

但它仍然给出错误。谁能帮我解决这个问题?提前致谢。

【问题讨论】:

  • 使用 Codeingiter 用户指南,一切尽在掌握
  • 如果您发现其中一个答案有帮助,请接受并标记为正确。

标签: php codeigniter


【解决方案1】:

你可以链接数据库子句,所以你可以写成

$this->db->where('category_name','case')->where('publication_status','case')->where('home_headline_sub','case');

这将生成查询的 WHERE 子句为

// WHERE category_name = 'case' AND publication_status = 'case' AND home_headline_sub = 'case'

这里的文档:http://ellislab.com/codeigniter/user-guide/database/active_record.html#chaining

【讨论】:

    【解决方案2】:

    你在其中使用数组。

    $this->db->where(array('category_name'=>case,'publication_status'=>case,'home_headline_sub'=>case));
    

    但我猜您想根据三列检查您的值。你可以使用

    $this->db->or_where(array('category_name'=>1,'publication_status'=>1,'home_headline_sub'=>1));
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:
      //The simple way
      $this->db->where('foo_field', 'foo_value')
               ->where('bar_field', 'bar_value')
               ->where('more_field', 'more_value');
      
      //using custom string
      //if your sql is really a complex one you can simply write like these
      
      $this->db->where("(foo_filed = 'foo_value') AND (bar_field = 'bar_value') AND (more_field = 'more_value')");
      
      //or may be with something more complex like this
      $this->db->where("(foo_filed = 'foo_value') AND ((bar_field = 'bar_value') OR (more_field = 'more_value'))");
      
      //while using a custom string make sure you put them all in the "double quotation marks" and use no ,commas. It is all a single line. The braces are not necessary always but I like to use them.  
      

      Documentation

      【讨论】:

      • 我如何将这个 where 子句与 'limit' 一起使用。假设我使用限制“2”。如何在模型中编写代码?
      • 就像这样 $this->db->where('foo', 'bar'); $this->db->limit(2);它们只需要在您的模型中使用相同的方法(函数)。
      • 如果我使用 2 个类别名称,都调用 where 子句以及如果我想要每个类别的 2 个内容(使用限制 2)..我该怎么做?我写了一个代码,但两个类别都显示了 2 个内容,但我想要 4 个内容..每个类别有 2 个内容..
      • 只要内容来自您的 mysql 数据库 limit(2) 的同一个表,就可以正常工作。像这样的东西 $this->db->select('foo, bar')->where('foo', 'f')->where('bar', 'b')->limit(2); $query=$this->db->get('table');那么你应该只得到你想要的 4 个内容。如果他们来自不同的表,你将不得不加入他们。在这种情况下,limit(2) 也可以正常工作。
      • 公共函数 select_all_content_2() { $this->db->select('category_name'); $this->db->where('category_name','home_headline_left') ->where('category_name', 'home_headline_right'); $this->db->order_by('date_add','DESC'); $this->db->limit ( 2 ); $query_result = $this->db->get('content'); $home_headline_left = $query_result->result();返回 $home_headline_left; }.....这是我在模型中编写的代码......但它也不起作用..写入此内容后不显示
      猜你喜欢
      • 2015-04-27
      • 2015-09-17
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多