首页 > 解决方案 > 如果数据存储为内爆,我们如何使用 where 条件

问题描述

这里我的桌子看起来像这样

在此处输入图像描述

我想获取所有部门,如果location_id3意味着如果3使用位置 ID,我需要获取部门名称,cardiology这样hair我就完成了这样的代码

我的controller长相是这样的

public function get_location_departments()
{
   $d = $_POST['location'];
   $data['departments'] = $this->Hospital_model->get_location_departments($d);
   $this->load->view('frontend/ajax_get_departments',$data);
}

我的model长相是这样的

public function get_location_departments($d)
{
  $this->db->where_in('location_id',$d);
  $query=$this->db->get('department')->result();
  return $query;
}

请帮我解决我的问题

标签: phpcodeigniter-3

解决方案


恕我直言,正确的答案是

public function get_location_departments($d)
{
    $query = $this->db
        ->where('FIND_IN_SET('.$this->db->escape($d).', location_id)', NULL, false)
        ->get('department')
        ->result();
    return $query;
}

FIND_IN_SET为此目的调用了一个函数。您可以在https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set下的 mysql 文档中找到它


推荐阅读