【问题标题】:Codeigniter combine where_in and like active record queriesCodeigniter 结合 where_in 和 like 活动记录查询
【发布时间】:2014-01-28 19:20:50
【问题描述】:

我有以下活动记录查询:

$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 的函数,其中包含我朋友的 id。我想搜索与我的“喜欢”查询匹配的朋友,但在 $ids 变量中只有一个 id。

我很确定我需要结合 where_in 和所有类似的语句,所以它类似于 (WHERE_IN $ids && Like 语句)。

我不擅长 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;

       }
     }

【问题讨论】:

  • 在 codeigniter 中,使用旧的原始 sql 而不是 active record 更容易编写这样的查询

标签: php mysql codeigniter activerecord where-in


【解决方案1】:

你想找到所有的朋友吗?如果只有一个 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();

【讨论】:

  • 请原谅我应该指定我们正在尝试在此函数中按名称缩小我们的朋友的范围。所以我们和我们的“朋友”有一个联系表。所以我们在 $ids 中获取我们的朋友 id。然后我们试图说,在所有拥有我们朋友 ID 的用户中,按搜索词缩小范围。我会在上面发布更多内容。
  • @DanielWhite 那么问题出在哪里?您的代码似乎是正确的。它做错了什么?
  • 它基本上可以很好地获取我的连接的ID,但是“$this->db->like”语句超过了“where_in”并且只返回了与like语句匹配的所有用户。我想返回与类似语句匹配并且是我的连接之一的所有用户。所以 (where_in $ids AND (like statements))。我只是不知道如何为其编写 SQL / Active 记录。
【解决方案2】:

已经有一段时间了,但我遇到了同样的问题,只需重新排序过滤器即可解决。这意味着:您首先必须执行 'where' 语句(而不是 '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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多