简体   繁体   English

codeigniter从同一表中选择两次联接

[英]codeigniter select join twice from same table

I have 3 tables, where is the first one for user. 我有3个表,哪里是用户的第一个表。 In another two I have id_user, for product who created or activation who created. 在另外两个中,我有id_user,用于创建产品的人或创建激活的人。 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. 我知道如何联接2个表,但是第三次​​我联接了两次,但我不知道如何联接。 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. 表激活的列user_a具有用户ID,产品的列user_p具有用户ID。 Sometimes is same user sometimes not. 有时是同一用户,有时不是。 Any helps? 有帮助吗?

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. 如果要在左右联接之间进行更改,只需在联接函数中添加“ left”或“ right”即可。

 $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. 这将优先使用您的用户表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM