codeigniter选择从同一个表中连接两次 [英] codeigniter select join twice from same table

查看:105
本文介绍了codeigniter选择从同一个表中连接两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个表,其中是第一个为用户。在另外两个我有id_user,对于创建或激活谁创建的产品。如何用这两个用户名显示这两个表的所有数据?我想出如何加入2表,但第三我加入两次,我不知道如何。这是两个表的示例:

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();

表激活具有带用户ID的列user_a,产品具有带用户ID的列user_p。有时是相同的用户有时不是。任何帮助?

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?

推荐答案

我如何使用别名。

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

如果要在左右连接之间进行改变,只需将'left'或'right'连接函数。

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.

这篇关于codeigniter选择从同一个表中连接两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆