将表连接到4个列并显示另一个字段

时间:2017-06-05 07:34:15

标签: php mysql codeigniter join

我有一个新问题,作为我之前的问题的信息,我已经成功显示加入另一个表的4 coloumn,但我有另一个问题显示另一个coloumn /字段,更详细:

表格个人(tbl_personal)

+---+-----+------------+
|ID |Name |Skill       |
+---+-----+------------+
| 1 | Nia | Accountant |
| 2 | Tia | Banking    |
| 3 | Ria | Technicall |
| 4 | Dia | Admin      |
+---+-----+------------+

表大师(tbl_master)

+---+---------+---------+-----------+--------+----+
|ID |Employee1|Employee2|Departement|Division|Note|
+---+---------+---------+-----------+--------+----+
| 1 | 1       | 2       | MSO       | MWS    | etc|
| 2 | 2       | 1       | DSO       | SE     | etc|
| 3 | 3       | 4       | OSO       | GA     | etc|
+---+---------+---------+-----------+--------+----+

我想在codeigniter中显示上面的tbl_master网页,如下所示:

+---+-------+-----------+-------+-----------+-----------+--------+----+
|ID |Name 1 |Skill 1    |Name 2 |Skill 2    |Departement|Division|Note|
+---+-------+-----------+-------+-----------+-----------+--------+----+
| 1 | Nia   | Accountant| Tia   | Banking   | MSO       | MWS    | etc|
| 2 | Tia   | Banking   | Nia   | Accountant| DSO       | SE     | etc|
| 3 | Ria   | Technicall| Dia   | Admin     | OSO       | GA     | etc|
+---+---------+---------+-------+-----------+-----------+--------+----+

我已经有了解决方案,使用此代码显示名称1,技能1直到技能2:

select tp1.name name1,tp1.skill skill1,tp2.name name2,tp2.skill skill2 
from tbl_master tm
join tbl_personal tp1
on tm.Employee1 =tp1.ID
join tbl_personal tp2
on tm.Employee2 =tp2.ID

但我还有另一个问题是显示离开,分区和注释字段,如果有任何建议,请,谢谢......

1 个答案:

答案 0 :(得分:1)

您只需更改您的选择并添加这些列:

select tp1.name name1,tp1.skill skill1,tp2.name name2,tp2.skill skill2,tm.Departement, tm.Division, tm.Note
from tbl_master tm
join tbl_personal tp1
on tm.Employee1 =tp1.ID
join tbl_personal tp2
on tm.Employee2 =tp2.ID