MySQL CodeIgniter:如何使用 WHERE 子句和 OR 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2975987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:11:42  来源:igfitidea点击:

CodeIgniter: How to use WHERE clause and OR clause

mysqlselectcodeigniterwhere-clause

提问by tarnfeld

I am using the following code to select from a MySQL database with a Code Igniter webapp:

我使用以下代码从带有 Code Igniter webapp 的 MySQL 数据库中进行选择:

$query = $this->db->get_where('mytable',array('id'=>10));

This works great! But I want to write the following MySQL statement using the CI library?

这很好用!但是我想使用 CI 库编写以下 MySQL 语句?

SELECT * FROM `mytable` WHERE `id`='10' OR `field`='value'

Any ideas? Thanks!

有任何想法吗?谢谢!

回答by Dylan

$where = "name='Joe' AND status='boss' OR status='active'";

$this->db->where($where);

回答by Rookwood

You can use or_where() for that - example from the CI docs:

您可以为此使用 or_where() - CI 文档中的示例:

$this->db->where('name !=', $name);

$this->db->or_where('id >', $id); 

// Produces: WHERE name != 'Joe' OR id > 50

回答by Wassim Sboui

You can use this :

你可以使用这个:

$this->db->select('*');
$this->db->from('mytable');
$this->db->where(name,'Joe');
$bind = array('boss', 'active');
$this->db->where_in('status', $bind);

回答by Raham

Active record method or_whereis to be used:

or_where将使用活动记录方法:

$this->db->select("*")
->from("table_name")
->where("first", $first)
->or_where("second", $second);

回答by Pullat Junaid

$where = "name='Joe' AND status='boss' OR status='active'";

$this->db->where($where);

Though I am 3/4 of a month late, you still execute the following after your where clauses are defined... $this->db->get("tbl_name");

尽管我迟到了 3/4 个月,但在定义 where 子句后,您仍然执行以下操作... $this->db->get("tbl_name");

回答by LOKENDRA

What worked for me :

什么对我有用:

  $where = '';
   /* $this->db->like('ust.title',$query_data['search'])
        ->or_like('usr.f_name',$query_data['search'])
        ->or_like('usr.l_name',$query_data['search']);*/
        $where .= "(ust.title like '%".$query_data['search']."%'";
        $where .= " or usr.f_name like '%".$query_data['search']."%'";
        $where .= "or usr.l_name like '%".$query_data['search']."%')";
        $this->db->where($where);



$datas = $this->db->join(TBL_USERS.' AS usr','ust.user_id=usr.id')
            ->where_in('ust.id', $blog_list) 
            ->select('ust.*,usr.f_name as f_name,usr.email as email,usr.avatar as avatar, usr.sex as sex')
            ->get_where(TBL_GURU_BLOG.' AS ust',[
                'ust.deleted_at'     =>  NULL,
                'ust.status'     =>  1,
            ]); 

I have to do this to create a query like this :

我必须这样做才能创建这样的查询:

SELECT `ust`.*, `usr`.`f_name` as `f_name`, `usr`.`email` as `email`, `usr`.`avatar` as `avatar`, `usr`.`sex` as `sex` FROM `blog` AS `ust` JOIN `users` AS `usr` ON `ust`.`user_id`=`usr`.`id` WHERE (`ust`.`title` LIKE '%mer%' ESCAPE '!' OR  `usr`.`f_name` LIKE '%lok%' ESCAPE '!' OR  `usr`.`l_name` LIKE '%mer%' ESCAPE '!') AND `ust`.`id` IN('36', '37', '38') AND `ust`.`deleted_at` IS NULL AND `ust`.`status` = 1 ;