php - 如果数据存储为内爆,我们如何使用 where 条件
问题描述
这里我的桌子看起来像这样
我想获取所有部门,如果location_id
这3
意味着如果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;
}
请帮我解决我的问题
解决方案
恕我直言,正确的答案是
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 文档中找到它
推荐阅读
- selenium - 未创建会话:此版本的 ChromeDriver 仅支持 Chrome 版本 81
- wordpress - 使用 Laragon 为本地环境复制 wordpress 数据库
- c++ - 返回值优化和`std::move`之间的区别?
- botframework - 我可以在 Luis bot 框架中定义动词的同义词吗?
- view - 从 POSTGRESQL DB 获取原始视图定义
- javascript - JSON API 调用的可选链接在 React 中不起作用
- refresh - DevExtreme 刷新网格
- mysql - 如果mysql中的日期格式为(dd-mm-yyyy),如何从日期返回“年”
- flask - 无法通过入口访问我的 Flask 服务器
- javascript - 当我使用 Angular Material 和一些自定义它的代码时,如何解决 Angular 应用程序中的 $event 问题