doutuo1908 2018-08-11 01:28
浏览 84
已采纳

使用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 01: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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部