我使用以下代码从带有Code Igniter webapp的MySQL数据库中进行选择:
$query = $this->db->get_where('mytable',array('id'=>10));
这很棒!但我想使用CI库编写以下MySQL语句?
SELECT * FROM `mytable` WHERE `id`='10' OR `field`='value'
有什么想法吗? 谢谢!
答案 0 :(得分:38)
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);
答案 1 :(得分:30)
您可以使用or_where() - 例如来自CI文档:
$this->db->where('name !=', $name);
$this->db->or_where('id >', $id);
// Produces: WHERE name != 'Joe' OR id > 50
答案 2 :(得分:12)
您可以使用:
$this->db->select('*');
$this->db->from('mytable');
$this->db->where(name,'Joe');
$bind = array('boss', 'active');
$this->db->where_in('status', $bind);
答案 3 :(得分:6)
将使用活动记录方法or_where
:
$this->db->select("*")
->from("table_name")
->where("first", $first)
->or_where("second", $second);
答案 4 :(得分:2)
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);
虽然我迟到了一个月的3/4,但在定义了where子句之后仍然会执行以下操作... $this->db->get("tbl_name");
答案 5 :(得分:1)
对我有用的是什么:
$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,
]);
我必须这样做才能创建这样的查询:
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 ;