使用codeigniter的php复杂查询连接

a6b3iqyw  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(396)

我有一个查询,它在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";

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

$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\u to.user\u name”
在codeigniter中使用上述查询的正确方法是什么。

hjzp0vay

hjzp0vay1#

$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
');

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

t98cgbkg

t98cgbkg2#

代替

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;

相关问题