【问题标题】:How to add where in 3 condition如何在 3 个条件下添加 where
【发布时间】:2019-10-07 15:52:30
【问题描述】:

我有一些这样的搜索模型,

public function get_products_by_keyword($keyword) {
    $this->db->select('*');
    $this->db->from('item');
    $this->db->like('item_name',$keyword);
    $this->db->where('item_product_type', 'watches');
    return $this->db->get()->result();
  }

item_product_type 列表:手表、建筑、aksesoris、眼镜、促销

在数据库中,我有 5 个item_product_type,但我只想显示 3 个类别,如何在 CodeIgniter 中做到这一点?

【问题讨论】:

    标签: php codeigniter search


    【解决方案1】:

    可以使用 where_in 传递三个类型列表

    $this->db->where_in('item_product_type', ["watches", "building", "aksesoris"]);
    

    官方doc.

    【讨论】:

    • 调用未定义的方法 CI_DB_mysqli_driver::whereIn()
    • @Misdan 更新了我的答案,一旦检查和官方文档链接也
    • @Misdan 乐于助人要将答案标记为已接受,请单击答案旁边的复选标记以将其从灰色切换为已填充。
    【解决方案2】:

    为此使用 where_in() 函数,

     $categories = array('watches','building','promo');
     public function get_products_by_keyword($keyword,$categories) {
        $this->db->select('*');
        $this->db->from('item');
        $this->db->like('item_name',$keyword);
        $this->db->where_in('item_product_type', $categories);
        return $this->db->get()->result();
      }
    

    【讨论】:

      【解决方案3】:

      使用 where_in()

       $this->db->where_in();
      

      生成一个 WHERE 字段 IN ('item', 'item') SQL 查询,如果合适,用 AND 连接

      $items = array('watches', 'building', 'aksesoris');
      $this->db->where_in('item_product_type', $items);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-26
        • 1970-01-01
        • 2012-07-26
        • 2017-03-25
        • 2012-09-28
        相关资源
        最近更新 更多