doutuo1908
2018-08-11 09:28
浏览 83
已采纳

使用Codeigniter进行复杂查询连接

I have a query which produce results in phpmyadmin but not in 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";

i want to use the same query in codeignitor. This is what i have used

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

when i use this it says

Unknown column 'cl_to .User_Name' in 'field list'

what is the proper way to use the above query in codeigniter.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutao6380 2018-08-11 09:36
    已采纳
    $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
    ');
    

    Replace this with

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

    Unwanted spacing in between table alias & column name

    点赞 打赏 评论
  • doucanshou6998 2018-08-17 06:32

    Replace

    cl_to  .User_Name AS ToClient,
    

    with

    cl_to  .User_Name AS ToClient,
    

    or you can directly run sql statement in codeigniter as

    $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;
    
    点赞 打赏 评论

相关推荐 更多相似问题