使用Codeigniter进行复杂查询连接

问题描述 投票:0回答:2

我有一个查询,它在phpmyadmin中生成结果,但在codeigniter中没有。

$sql = "SELECT express_interests.*, 
       cl_to  .User_Name AS ToClient, 
       cl_from.User_Name AS FromClient,
       cl_from.Member_Id AS FromMid,
       cl_to.Member_Id AS ToMid

FROM express_interests  
    INNER JOIN users AS cl_to ON cl_to.User_Id = express_interests.To_Id
    INNER JOIN users AS cl_from ON cl_from.User_Id = express_interests.User_Id";

我想在codeigniter中使用相同的查询。这是我用过的

$this->db->select('express_interests.*, 
       cl_to  .User_Name AS ToClient, 
       cl_from.User_Name AS FromClient,
       cl_from.Member_Id AS FromMid,
       cl_to.Member_Id AS ToMid
');

$this->db->from('express_interests');


$this->db->join('users AS cl_to', 'cl_to.User_Id = express_interests.To_Id');
$this->db->join('users AS cl_from', 'cl_from.User_Id = express_interests.User_Id');

当我用它时,它说

'字段列表'中的未知列'cl_to .User_Name'

在codeigniter中使用上述查询的正确方法是什么。

php mysql codeigniter codeigniter-3 codeigniter-2
2个回答
0
投票
$this->db->select('express_interests.*, 
       cl_to  .User_Name AS ToClient, 
       cl_from.User_Name AS FromClient,
       cl_from.Member_Id AS FromMid,
       cl_to.Member_Id AS ToMid
');

替换为

$this->db->select('express_interests.*, 
       cl_to.User_Name AS ToClient, 
       cl_from.User_Name AS FromClient,
       cl_from.Member_Id AS FromMid,
       cl_to.Member_Id AS ToMid
');

表别名和列名称之间的不需要的间距


0
投票

更换

cl_to  .User_Name AS ToClient,

cl_to  .User_Name AS ToClient,

或者你可以直接在codeigniter中运行sql语句

$sql = "SELECT express_interests.*, 
   cl_to  .User_Name AS ToClient, 
   cl_from.User_Name AS FromClient,
   cl_from.Member_Id AS FromMid,
   cl_to.Member_Id AS ToMid

FROM express_interests  
INNER JOIN users AS cl_to ON cl_to.User_Id = express_interests.To_Id
INNER JOIN users AS cl_from ON cl_from.User_Id = express_interests.User_Id"; 

$result=$this->db->query($sql);

print_r($result->result());

die;
© www.soinside.com 2019 - 2024. All rights reserved.