gpt4 book ai didi

php - 在 codeigniter 中加入两个表

转载 作者:行者123 更新时间:2023-11-28 23:23:59 25 4
gpt4 key购买 nike

我有 2 个表,我想加入并显示用户角色的名称。情况是这样的

我的 2 个表是 users_mlh 和 user_roles_mlh

在 users_mlh 表的角色列中,我存储了用户角色的 ID,user_roles_mlh 包含用户角色的名称和 ID。我想要做的是在我的 View 中显示用户角色的名称。

我的表格如下。

Screen1

我已经在我的模型中尝试过了

$this->db->select('*');
$this->db->from('user_roles_mlh');
$this->db->join('users_mlh', 'users_mlh.role = user_roles_mlh.id');
$this->db->where('users_mlh.role = user_roles_mlh.id');

$query = $this->db->get();

return $query->result_array();

但是从上面我得到了这样的东西

https://s4.postimg.org/ot5nyr1hp/Untitled_122.jpg

目前它列出了所有用户级别而不是每个用户的角色

最佳答案

情况 1:如果您不使用 where 条件直接访问所有数据

  $this->db->select( "*" );
$this->db->from( 'users_mlh' );
$this->db->join('user_roles_mlh', 'user_roles_mlh.id = users_mlh.role');

案例 2:使用 Where 条件和特定列数据

   $this->db->select( 'user_roles_mlh.role_type,users_mlh.name' );
$this->db->from( 'users_mlh' );
$this->db->join('user_roles_mlh', 'user_roles_mlh.id = users_mlh.role');
$this->db->where('users_mlh.id =',1);

最后得到结果

  $query = $this->db->get();
return $query->result();

关于php - 在 codeigniter 中加入两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40226739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com