Code Igniter JOIN中的问题

时间:2013-12-03 09:57:58

标签: mysql codeigniter-2

我在CI活动记录表单中有此查询

$this->db->select('companies.*,comp_permissions.user_id');
$this->db->from('companies');
$this->db->join('comp_permissions', 'companies.id = comp_permissions.comp_id');
$query = $this->db->get();

它会像这样生成查询

SELECT `hs_companies`.*, `hs_comp_permissions`.`user_id`
FROM (`hs_companies`)
INNER JOIN `hs_comp_permissions` ON `hs_companies`.`id` = `hs_comp_permissions`.`comp_id`

但我的原始查询是

Select cb.*,cbp.user_id
from hs_companies cb
INNER JOIN hs_comp_permissions cbp ON 
(cb.id = cbp.comp_id AND cbp.status=2 AND cbp.user_id = 72)

所以现在如何在JOIN函数中放置多个列?

更新

当我尝试这样的时候

$this->db->select('companies.*,comp_permissions.user_id');
$this->db->from('companies');
$this->db->join('comp_permissions', '(companieas.id = comp_permissions.comp_id AND comp_permissions.status = 2 AND comp_permissions.user_id = 72)','inner');
$this->db->get()->result();

生成以下结果

SELECT `hs_companies`.*, `hs_comp_permissions`.`user_id` 
FROM (`hs_companies`) 
INNER JOIN `hs_comp_permissions` ON 
(`hs_companies`.`id` = `hs_comp_permissions`.`comp_id` AND comp_permissions.status = 2 AND comp_permissions.user_id = 72)

它不会添加hs_我的数据库前缀

2 个答案:

答案 0 :(得分:1)

你能用WHERE caluses设置吗?

$this->db->select('companies.*,comp_permissions.user_id');
$this->db->from('companies');
$this->db->join('comp_permissions', 'companies.id = comp_permissions.comp_id');
$this->db->where('comp_permissions.status', '2');
$this->db->where('comp_permissions.user_id', '72');
$query = $this->db->get();

或者您可以手动设置数据库前缀:

$this->db->join('comp_permissions', '(companies.id = comp_permissions.comp_id 
                                     AND hs_comp_permissions.status=2 
                                     AND hs_comp_permissions.user_id = 72)');

答案 1 :(得分:0)

你可以这样做:

$this->db->select('companies.*,comp_permissions.user_id');
$this->db->from('companies');
$this->db->join('comp_permissions', '(companies.id = comp_permissions.comp_id 
                                     AND cbp.status=2 
                                     AND cbp.user_id = 72)');
$query = $this->db->get();