sql - 从数据库 codeignter 获取用户数据
问题描述
我在一个数据库中有两个表,表一称为:ci_admin,表二称为 active_ingredient。
我想使用当前用户会话从 ci_admin 表中的 user_id 等于 admin_id 的 active_ingredient 表中获取用户数据。
这段代码,获取所有用户的数据,我只需要用户插入的数据:
$wh =array();
$SQL ='SELECT * FROM active_ingredient';
$wh[] = "SELECT 1
FROM ci_admin
WHERE ci_admin.admin_id = active_ingredient.user_id";
if(count($wh)>0)
{ $WHERE = implode(' and ',$wh);
return $this->datatable->LoadJson($SQL,$WHERE);
}
else
{
return $this->datatable->LoadJson($SQL);
}
解决方案
试试这个,它会帮助你。
$this->db->select('ai.*,ca.*');
$this->db->from('active_ingredient ai');
$this->db->join('ci_admin ca', 'ca.admin_id = ai.user_id', 'left');
$this->db->where($data); //pass the session data for exact filter
$query = $this->db->get();
return $query->result();
推荐阅读
- shell - 在 Mac 终端上的 sh -c 调用中转义单引号
- ios - 用于颤振的 iOS 模拟器
- reactjs - TypeError:无法读取未定义的属性(读取“目标”)(Typscript/React)
- c++ - 提升构建:缺少规则
- java - 附上 PDDoucment 的问题
- jenkins - 詹金斯的工作卡在队列中。如果工作人员离线,如何使其自动失败
- ruby-on-rails - HTTParty#lock 似乎覆盖了 ActiveRecord 的 #lock 方法
- java - 创建名为“springSecurityFilterChain API 端点的 bean”时出错
- java - 用 2 个道具交换列表中的 2 个对象是 String 和 Integer,其中一个 obj 具有 MIN 整数,一个具有 MAX 整数
- r - 从 terra 包中的图层中提取值