duanke9540 2016-03-16 17:05
浏览 20

codeigniter从同一个表中选择两次连接

I have 3 tables, where is the first one for user. In another two I have id_user, for product who created or activation who created. How I can show all data from these two tables with both username? I figure out how to join 2 tables, but third I have join twice and I don't know how. This is example for two tables:

        $query=$this->db->select('*')
                ->from('activation')
                ->join('products','products.id_pro = activation.id_pro')
                ->order_by('id_key','DESC')
                ->get();

Table activation has column user_a with user id, and product has column user_p with user id. Sometimes is same user sometimes not. Any helps?

  • 写回答

1条回答 默认 最新

  • dtwknzk3764 2016-03-16 20:29
    关注

    Heres how I do it using aliases. You can format it your way too.

     $this->db->select('tb1.*,tb2.*,u.*');
     $this->db->join('table1name tb1','tb1.id = u.tb1_id');
     $this->db->join('table2name tb2','tb2.id = u.tb2_id');
     $this->db->order_by('u.id_key','DESC');
     $result = $this->db->get('User u')->result();
    

    If you want to alter between left and right join just add 'left' or 'right' to the join function.

     $this->db->join('table1name pr1','pr1.id = u.tb1_id','right');
     $this->db->join('table2name tb2','tb2.id = u.tb2_id','right');
    

    This would preference your user table.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向