简体   繁体   English

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

[英]Codeigniter combine where_in and like active record queries

I have the following active record query: 我有以下活动记录查询:

$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);

This is a function that has an array $ids, which has the ids of my friends. 这个函数有一个数组$ ids,它有我朋友的ids。 I want to search for friends that match my "like" queries, but only have one of the id's in the $ids variable. 我想搜索匹配“喜欢”查询的朋友,但只有$ ids变量中的一个id。

I'm pretty sure i need to combine where_in and all the like statements so its something like (WHERE_IN $ids && Like Statements). 我很确定我需要将where_in和所有类似的语句结合起来,所以它就像(WHERE_IN $ ids && Like Statements)。

I'm not great at mysql so any help here would be appreciated. 我在mysql上并不擅长,所以任何帮助都会受到赞赏。

Thanks! 谢谢!

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;

       }
     }

Do you want to find all the friends? 你想找到所有的朋友吗? If there are only one id, then you don't need like part, as you already found your friend. 如果只有一个id,那么你就不需要like部分了,因为你已经找到了你的朋友。 On the other part, if you not sure about id of your friends, and just want to find all friends matching your like criteria, you may remove where_in part. 另一方面,如果您不确定朋友的id ,并且只想找到符合您喜欢标准的所有朋友,您可以删除where_in部分。

This will find your friends: 这会找到你的朋友:

$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);

Considering there's only one id, such query will find only one friend: 考虑到只有一个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);

EDIT 编辑

Sometimes, with codeigniter, it is better to use a raw query: 有时,使用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();

It's been a while, but I had the same problem and got it solved by simply reorder the filters. 已经有一段时间了,但我遇到了同样的问题,只需重新排序过滤器即可解决问题。 This means: you first have to do the 'where' statetemnt (not 'where_in') 这意味着:你首先必须做'where'statetemnt(不是'where_in')

This would look like: 这看起来像:

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM