Codeigniter结合了where_in和类似的活动记录查询

时间:2014-01-28 19:20:50

标签: php mysql codeigniter activerecord where-in

我有以下有效记录查询:

$this->db->select('id, email, first_name, last_name, current_location_state, current_location, avatar, avatar_fb');
          $this->db->from('users');
          $this->db->like('first_name', $search);
          $this->db->or_like('last_name', $search);
          $this->db->or_like("CONCAT(first_name, ' ', last_name)", $search);
          $this->db->or_like('email', $search);
          $this->db->where_in('id', $ids);

这个函数有一个数组$ ids,它有我朋友的ids。我想搜索匹配“喜欢”查询的朋友,但只有$ ids变量中的一个id。

我很确定我需要将where_in和所有类似的语句结合起来,这样就像(WHERE_IN $ ids&& Like Statements)。

我在mysql上并不擅长,所以任何帮助都会受到赞赏。

谢谢!

function narrow_connections($search) {

       //First get all this users connections...
       $connections = $this->get_connections($this->session->userdata('user_id'), 0, 0);

       if(empty($connections)) {
          return array();
       }else {

          //Need to get an array of id's
          $ids = array();
          foreach($connections as $con) {
             array_push($ids, $con['id']);
          }

          //Now that we have an array of ID's, find all users that have one of the ids (our connections), AND match a search term to narrow down
          //the results. 

          $this->db->select('id, email, first_name, last_name, current_location_state, current_location, avatar, avatar_fb');
          $this->db->from('users');
          $this->db->like('first_name', $search);
          $this->db->or_like('last_name', $search);
          $this->db->or_like("CONCAT(first_name, ' ', last_name)", $search);
          $this->db->or_like('email', $search);
          $this->db->where_in('id', $ids);
          $query = $this->db->get();
          $data = array();

          foreach ($query->result() as $row) {
          $data[] = array(
            'id' => $row->id,
            'email' => $row->email,
            'first_name' => $row->first_name,
            'last_name' => $row->last_name,
            'current_location_state' => $row->current_location_state,
            'current_location' => $row->current_location,
            'avatar' => $row->avatar,
            'avatar_fb' => $row->avatar_fb,
          );
          }
          return $data;

       }
     }

2 个答案:

答案 0 :(得分:2)

你想找到所有的朋友吗?如果只有一个ID,那么您不需要like部分,因为您已经找到了您的朋友。另一方面,如果您不确定朋友的id,并且只想找到符合您喜欢条件的所有朋友,则可以删除where_in部分。

这会找到你的朋友:

$this->db->select('id, email, first_name, last_name, current_location_state, current_location, avatar, avatar_fb');
$this->db->from('users');
$this->db->like('first_name', $search);
$this->db->or_like('last_name', $search);
$this->db->or_like("CONCAT(first_name, ' ', last_name)", $search);
$this->db->or_like('email', $search);

考虑到只有一个id,这样的查询只能找到一个朋友:

$this->db->select('id, email, first_name, last_name, current_location_state, current_location, avatar, avatar_fb');
$this->db->from('users');
$this->db->where_in('id', $ids);

修改

有时,使用codeigniter,最好使用原始查询:

$this->db->query('
  SELECT `id`, `email`, `first_name`, `last_name`, `current_location_state`, `current_location`, `avatar`, `avatar_fb`
  FROM `users`
  WHERE (
    `first_name` like ?
    or `last_name` like ? 
    or concat(`first_name`, ' ', `last_name`) like ? 
    or `email` like ?)
  AND `id` in('
    .join(',',
    array_map(function($e) { return (int) $e; }, $ids))
    .')',
  "%$search%", "%$search%", "%$search%", "$search")->result();

答案 1 :(得分:0)

已经有一段时间了,但我遇到了同样的问题,只需重新排序过滤器即可解决问题。这意味着:首先必须执行'where'statetemnt(而不是'where_in')

这看起来像是:

$this->db->select('id, email, first_name, last_name, current_location_state, current_location, avatar, avatar_fb');

      $this->db->where('id', $ids);
      $this->db->from('users');
      $this->db->like('first_name', $search);
      $this->db->or_like('last_name', $search);
      $this->db->or_like("CONCAT(first_name, ' ', last_name)", $search);
      $this->db->or_like('email', $search);