如何显示编号为Id的名称

时间:2018-11-15 11:29:29

标签: mysql phpmyadmin codeigniter-3

我有两个表Test1和Test2。

测试1

<div id='paper'>
</div>

Test2

p_id|imp_id | Name    |  Member_type  
1   |001    | A       |  1    
2   |002    | B       |  2     
3   |003    | C       |  1     
4   |004    | D       |  2

我正在使用join来获取设计输出。

查询

r_id|p_id |secondary_type  
1   |1    |2    
2   |3    |4    

我的输出是

SELECT * FROM `test1` JOIN `test2` ON `test2`.`secondary_type` = `test1`.`p_id` 

因此,我获得了p_id imp_id Name Member_type r_id p_id secondary_type 2 002 B 2 1 1 2 4 004 D 2 2 3 4 的价值。我必须显示ID值的名称。

例如p_id已连接到A。因此,我获得了A的ID,但我需要将ID设置为ID。

所以我的输出将

B

我尝试使用CodeIgniter。

p_id    imp_id  Name    Member_type     r_id |  p_id|   secondary_type  
2       002     B       2               1    |   A  |   2
4       004     D       2               2    |   C  |   4

1 个答案:

答案 0 :(得分:1)

尝试在选择语句中使用别名,例如

SELECT 
test1.p_id,test1.imp_id,test2.name,test1.member_type,test2.r_id,test1.name as 
p_id,test2.secondary_type 
FROM `test1` JOIN `test2` 
ON `test2`.`secondary_type` = `test1`.`p_id` 

结果将根据您的要求。