php - Codeigniter 结合 where_in 和 like 事件记录查询

标签 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 的函数,其中包含我 friend 的 ID。我想搜索与我的“喜欢”查询相匹配的 friend ,但 $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;

       }
     }

最佳答案

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

这会找到你的 friend :

$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,这样的查询只会找到一个 friend :

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

关于php - Codeigniter 结合 where_in 和 like 事件记录查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21414814/

相关文章:

php - 如何从 Codeigniter 中的 Controller 调用模型?

php - 处理 phalcon 中的全局异常

java - 使用 hibernate.xml 连接到 mysql 数据库时出现问题

php - 如何在 Codeigniter 中创建动态站点地图

php - 回显CodeIgniter文件上传类upload->data()数组

php - 为什么 DateTime 对象在我执行空操作之前不可用?

php - 如何在网页上显示用于预览的 HTML 电子邮件?

php - 为什么我的 19 位数字会四舍五入为 17 位数字

mysql - Entity Framework 对 MySql 无符号十进制的支持

php - 为什么我的 .htaccess 在部署时停止工作